-
Notifications
You must be signed in to change notification settings - Fork 1
Add function argument count validation to language service #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Added getDiagnostics() method to the language service API that validates function argument counts and returns LSP diagnostics when functions are called with too few or too many arguments. Changes: - Added ArityInfo interface and GetDiagnosticsParams interface - Added arityInfo() method to FunctionDetails class - Implemented getDiagnostics() in createLanguageService() - Exported new types from public API - Added comprehensive tests for the new functionality Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
Sander-Toonen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move the logic for getDiagnostics to a seperate file and reduce the complexity of teh getDiagnostics function by extracting
Fixed the argument counting logic to correctly handle function calls with multiple arguments. The issue was that markArgumentSeen() was incorrectly resetting argCount to 1 even after commas had already been counted. Now it only sets argCount = 1 if this is the first argument (argCount === 0). Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
- Moved pluralize helper outside the nested loops to avoid repeated function creation - Simplified the argument tracking by inlining the logic instead of using closures - Converted isAtFunctionCallTopLevel from a closure to a computed variable - Removed unused foundClosingParen variable Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
Sander-Toonen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure that the functionality can be demonstrated in the playground
- Created new diagnostics.ts module with extracted diagnostic logic - Broke down complex argument counting into smaller, focused functions: - ArgumentCounterState interface for state management - createArgumentCounterState() for initialization - isAtTopLevel() for depth checking - markArgumentSeen() for argument tracking - processParenToken(), processBracketToken(), processBraceToken() for token handling - processCommaToken(), processOtherToken() for remaining tokens - countFunctionArguments() for argument counting - validateFunctionCall() for validation - createTooFewArgumentsDiagnostic(), createTooManyArgumentsDiagnostic() for diagnostic creation - Simplified getDiagnostics() in language-service.ts to delegate to the new module - Added TokenSpan interface and comprehensive documentation Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
Done in 1c797cc. Extracted the diagnostics logic to a new
|
- Added applyDiagnostics() function to app.js that calls ls.getDiagnostics() and displays error markers in Monaco editor using setModelMarkers - Call applyDiagnostics() on initialization and on expression changes - Added "Diagnostics Demo" example to examples.js that demonstrates function argument count validation with pow(2) and random(1, 2, 3) Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
Done in 7d5a2a8. Integrated diagnostics into the playground:
Run |
|
@copilot Update documentation on the language server |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Adds function-argument-count diagnostics to the language service API and wires them into the playground sample so incorrect function calls surface as LSP-style errors.
Changes:
- Extend
LanguageServiceApiwithgetDiagnostics(params: GetDiagnosticsParams): Diagnostic[]and plumb this throughcreateLanguageService. - Introduce
ArityInfoandFunctionDetails.arityInfo()plus a newdiagnostics.tsmodule that token-walks expressions, counts arguments (including nested/array/object/variadic/optional cases), and produces LSP diagnostics. - Integrate diagnostics into the Monaco playground with
applyDiagnostics()and a new "Diagnostics Demo" example showcasing incorrect calls topowandrandom.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
test/language-service/language-service.ts |
Adds comprehensive tests for getDiagnostics, covering correct/incorrect arity, nested calls, literals as arguments, severity, source, and range, plus a basic API-existence check. |
src/language-service/language-service.types.ts |
Extends the public language service types with getDiagnostics, GetDiagnosticsParams, and ArityInfo to describe function arity. |
src/language-service/language-service.ts |
Implements getDiagnostics on the language service by tokenizing the document, building a FunctionDetails map, and delegating to the new diagnostics module; returns it in the public API object. |
src/language-service/language-service.models.ts |
Enhances FunctionDetails with arityInfo() that derives (min, max) argument counts from built-in docs (including variadic and optional parameters) or JS function arity as a fallback. |
src/language-service/diagnostics.ts |
New module that walks token spans, counts function-call arguments with nesting/arrays/objects handled, compares against ArityInfo, and emits LSP Diagnostic objects with accurate ranges, messages, severity, and source. |
samples/language-service-sample/examples.js |
Adds a "Diagnostics Demo" example expression intentionally using wrong argument counts to surface diagnostics in the playground. |
samples/language-service-sample/app.js |
Adds applyDiagnostics() to convert LSP diagnostics to Monaco markers and invokes it on expression changes to show argument-count errors in the editor. |
index.ts |
Re-exports new language service types (GetDiagnosticsParams, ArityInfo) from the package root for public consumption. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Added Diagnostics feature to the Features section - Added getDiagnostics() to Basic Usage example - Updated Monaco Editor sample description to mention diagnostics demo - Added complete ls.getDiagnostics() API reference section - Added Monaco Editor integration example for diagnostics - Updated Exported Types to include GetDiagnosticsParams and ArityInfo - Updated LSP Types to include Diagnostic and DiagnosticSeverity Co-authored-by: Sander-Toonen <5106372+Sander-Toonen@users.noreply.github.com>
Done in 1705043. Updated
|
ArityInfointerface for tracking function argument min/maxarityInfo()method toFunctionDetailsclass to return min/max argsgetDiagnostics()method toLanguageServiceApiinterfacegetDiagnostics()increateLanguageService()that validates function argument countsGetDiagnosticsParamsandArityInfotypes from language serviceOriginal prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.