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
Fix completion record for variable declarations #13596
Fix completion record for variable declarations #13596
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 0e512b4:
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/47525/ |
I think we should iterate statement list from end to start, if the completion record is a variable declaration, discard it and check the previous one: babel/packages/babel-traverse/src/path/family.ts Lines 195 to 201 in 844baeb
|
@JLHwung Sure, done! |
46b373e
to
82a2254
Compare
// search on statement lists and assume that the last | ||
// non-variable-declaration statement determines the completion. | ||
for (let i = paths.length - 1; i >= 0; i--) { | ||
if (!paths[i].isVariableDeclaration()) { |
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.
I think we should apply _getCompletionRecords
and check if the completion is VariableDeclaration
, e.g. The completion record of this snippet
1 + 2;
{
var a = 1;
}
is 1 + 2
.
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.
@JLHwung Sure, done, and added a test. I have to admit that I’m a bit unsure about the code here – is it valid to assume that if the list of completion records returned by _getCompletionRecords
contains a VariableDeclaration
, then that automatically implies that the list has length 1? It seems like something that should be true to me, and I’m not sure how to handle this if it isn’t.
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.
is it valid to assume that if the list of completion records returned by _getCompletionRecords contains a VariableDeclaration, then that automatically implies that the list has length 1
That's a very good question! AFAIK only IfStatement, SwitchStatement and TryStatement may produce multiple completion records, and if a VariableDeclaration is directly within these statements, it should be recursively handled here and as long as we don't return VariableDeclaration as completion records in getStatementListCompletion
, then the VariableDeclaration
must not come from these statements and thus the list here should contain only one completion record.
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.
Right – that’s what I’d be thinking. :) Should I add an assertion of some kind?
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.
an assertion of some kind
I think it's fine. The _getCompletionRecords
is mostly used in proposal-do-expression
, and the proposal now bans variable declaration ending in do block. So I guess few people will use it in that way.
Variable declarations are ignored for completion records, so they should be skipped.
82a2254
to
0e512b4
Compare
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!
Variable declarations are ignored for completion records,
so they should be skipped.