Skip to content

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

Conversation

chenjigeng
Copy link
Contributor

Fixes #41731

@typescript-bot typescript-bot added the For Backlog Bug PRs that fix a backlog bug label Dec 20, 2020
@chenjigeng chenjigeng closed this Dec 20, 2020
@chenjigeng chenjigeng reopened this Dec 20, 2020
Copy link
Member

@andrewbranch andrewbranch left a 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:

  1. This needs tests.
  2. 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.

Comment on lines 1541 to 1554
while (curNode && curNode.parent) {
if (isPropertyAssignment(curNode) && isObjectLiteralExpression(curNode.parent)) {
curNode = curNode.parent.parent;

if (isVariableDeclaration(curNode)) {
return curNode;
}
}
else {
return undefined;
}
}

return undefined;
Copy link
Member

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.

Copy link
Contributor Author

@chenjigeng chenjigeng Dec 22, 2020

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

@chenjigeng
Copy link
Contributor Author

chenjigeng commented Dec 22, 2020

Thanks for taking a look! Two big things to get started:

  1. This needs tests.
  2. 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.
  1. Added
  2. I find that in this case of the issue, filterObjectMembersList will not be executed and I don't know why. Can you give me a hand with that

@andrewbranch andrewbranch self-assigned this Dec 22, 2020
@andrewbranch
Copy link
Member

I find that in this case of the issue, filterObjectMembersList will not be executed and I don't know why

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.

Copy link
Member

@andrewbranch andrewbranch left a 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"].

@chenjigeng
Copy link
Contributor Author

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"].

Yes. It Sounds like good. I can try to do it. But I think this may need many test cases.

@chenjigeng
Copy link
Contributor Author

chenjigeng commented Dec 23, 2020

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"].

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

@sandersn
Copy link
Member

I'm going to close this PR since #42087 supersedes it.

@sandersn sandersn closed this Dec 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Object literal completions in declaration initializer include declared variable
4 participants