-
Notifications
You must be signed in to change notification settings - Fork 12.8k
feat: exclude declared variable when Object literal completions #42056
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
feat: exclude declared variable when Object literal completions #42056
Conversation
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.
Thanks for taking a look! Two big things to get started:
- This needs tests.
- I would expect zero extra work to be done if we’re not in object literal completions. Right now this is doing some amount of work for every completions request that returns any globals at all. Whatever work is going to be done should probably happen in
filterObjectMembersList
.
src/services/completions.ts
Outdated
while (curNode && curNode.parent) { | ||
if (isPropertyAssignment(curNode) && isObjectLiteralExpression(curNode.parent)) { | ||
curNode = curNode.parent.parent; | ||
|
||
if (isVariableDeclaration(curNode)) { | ||
return curNode; | ||
} | ||
} | ||
else { | ||
return undefined; | ||
} | ||
} | ||
|
||
return undefined; |
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.
Take a look at findAncenstor
and see if that lets you write this more concisely.
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.
Take a look at
findAncenstor
and see if that lets you write this more concisely.
I used findAncenstor at first, but I found that findAncenstor didn't do much of the work because I had to filter two nodes at a time, one for property assignments and one for object literal expression nodes
|
Oh, I totally wasn’t thinking. That’s for completions on object literal keys, not values, which could be anything. The test looks great and answers some of the questions I was going to ask next. I think this looks good overall, just going to leave some suggestions for names and code style. |
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.
Now that I’m thinking about this more, it’s really not specific to object literals at all. Variable declaration names should probably never show up in completions for their own initializers unless a function body boundary has been crossed:
const a = a;
const b = a && b;
const c = [{ prop: [c] }];
I suspect there wouldn’t be much performance cost to generalizing to that. You could walk up parent nodes and bail if you hit a FunctionBody
or an expression that was an ArrowFunction["body"]
, and return the variable declaration if you hit a VariableDeclaration["initializer"]
.
… feat/exclude-completions-of-declared-variable
Yes. It Sounds like good. I can try to do it. But I think this may need many test cases. |
I submit a new PR to do this (based on the current submission)�. #42087 I have modified the existing test case and added a new test case. Please help me to see if there is any problem here |
I'm going to close this PR since #42087 supersedes it. |
Fixes #41731