Narrow keyword completions for concise arrow expression bodies#3935
Open
DhineshPonnarasan wants to merge 1 commit into
Open
Narrow keyword completions for concise arrow expression bodies#3935DhineshPonnarasan wants to merge 1 commit into
DhineshPonnarasan wants to merge 1 commit into
Conversation
- Add KeywordCompletionFiltersArrowFunctionExpressionBodyKeywords filter - Implement isArrowFunctionExpressionBody() to detect non-block arrow bodies - Define isArrowFunctionExpressionBodyKeyword() with 23 allowed keywords - Update getTypescriptKeywordCompletions() to handle new filter - Wire arrow expression check into getGlobalCompletions() with priority - Port of JS implementation from microsoft/TypeScript#63485 This prevents statement keywords (return, break, etc.) from being offered in concise arrow function expression bodies, where only expression keywords are valid (class, new, function, etc.). For transparency: I used AI assistance while preparing this draft (investigation, wording, and implementation support), and I reviewed/validated the resulting changes and test runs locally.
Contributor
There was a problem hiding this comment.
Pull request overview
Ports the keyword-completion narrowing from microsoft/TypeScript#63485 so that, inside a concise (non-block) arrow function expression body, only keywords valid in expression position are suggested.
Changes:
- Adds
KeywordCompletionFiltersArrowFunctionExpressionBodyKeywordsand wires it intogetTypescriptKeywordCompletions. - Adds
isArrowFunctionExpressionBodyKeywordlisting the allowed expression keywords. - Adds
isArrowFunctionExpressionBodydetection and checks it before the function-like-body filter ingetGlobalCompletions.
Comment on lines
+3449
to
+3469
| func isArrowFunctionExpressionBody(contextToken *ast.Node) bool { | ||
| if contextToken == nil { | ||
| return false | ||
| } | ||
|
|
||
| var prev *ast.Node | ||
| result := ast.FindAncestorOrQuit(contextToken, func(node *ast.Node) ast.FindAncestorResult { | ||
| // Only arrow functions with non-block bodies | ||
| if node.Kind == ast.KindArrowFunction { | ||
| arrow := node.AsArrowFunction() | ||
| body := arrow.Body | ||
| // If body is not a block, and prev is the body (not part of parameters), we're in expression body | ||
| if body != nil && body.Kind != ast.KindBlock && prev == body { | ||
| return ast.FindAncestorTrue | ||
| } | ||
| } | ||
| prev = node | ||
| return ast.FindAncestorFalse | ||
| }) | ||
| return result != nil | ||
| } |
Comment on lines
+3454
to
+3468
| var prev *ast.Node | ||
| result := ast.FindAncestorOrQuit(contextToken, func(node *ast.Node) ast.FindAncestorResult { | ||
| // Only arrow functions with non-block bodies | ||
| if node.Kind == ast.KindArrowFunction { | ||
| arrow := node.AsArrowFunction() | ||
| body := arrow.Body | ||
| // If body is not a block, and prev is the body (not part of parameters), we're in expression body | ||
| if body != nil && body.Kind != ast.KindBlock && prev == body { | ||
| return ast.FindAncestorTrue | ||
| } | ||
| } | ||
| prev = node | ||
| return ast.FindAncestorFalse | ||
| }) | ||
| return result != nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR ports the arrow expression body keyword filtering fix from microsoft/TypeScript#63485 to the Go implementation.
Problem
The default keyword (and other statement keywords) were incorrectly offered in completions within concise arrow function expression bodies, where only expression keywords are valid.
Example:
\\ ypescript
const arrow = () => /default/ 42; // incorrect - 'default' is a statement keyword
\\
Solution
Keywords Filtered Out
Statement keywords: return, break, continue, default, etc.
Declaration keywords: var, let, const, function, class (at statement level), etc.
Keywords Still Available
Expression keywords: class (expr), function (expr), new, import, delete, typeof, await, as, satisfies, etc.
Block-body arrow functions preserve full statement keyword availability.
Testing
Go build validates the implementation compiles without errors.
References
defaultkeyword is offered in expression-bodied arrow function completions TypeScript#63463Transparency Notice: I used AI assistance while preparing this implementation (investigation, code generation, and validation support), and I reviewed and validated the resulting changes and test runs locally.