Skip to content
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

Merged
merged 1 commit into from Jul 27, 2021

Conversation

addaleax
Copy link
Contributor

@addaleax addaleax commented Jul 23, 2021

Q                       A
Fixed Issues?
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

Variable declarations are ignored for completion records,
so they should be skipped.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 23, 2021

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:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@babel-bot
Copy link
Collaborator

babel-bot commented Jul 23, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/47525/

@JLHwung
Copy link
Contributor

JLHwung commented Jul 23, 2021

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:

// When we are in a context where `break` must not exist, we can skip linear
// search on statement lists and assume that the last statement determines
// the completion
completions = completions.concat(
_getCompletionRecords(paths[paths.length - 1], context),
);
}

@addaleax
Copy link
Contributor Author

@JLHwung Sure, done!

@addaleax addaleax force-pushed the skip-var-for-completion-record branch from 46b373e to 82a2254 Compare July 23, 2021 15:29
// 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()) {
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

@addaleax addaleax Jul 24, 2021

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?

Copy link
Contributor

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.

@JLHwung JLHwung added pkg: traverse PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Jul 23, 2021
Variable declarations are ignored for completion records,
so they should be skipped.
@addaleax addaleax force-pushed the skip-var-for-completion-record branch from 82a2254 to 0e512b4 Compare July 23, 2021 16:19
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@nicolo-ribaudo nicolo-ribaudo merged commit d1f9089 into babel:main Jul 27, 2021
@addaleax addaleax deleted the skip-var-for-completion-record branch July 28, 2021 09:19
nicolo-ribaudo pushed a commit to nicolo-ribaudo/babel that referenced this pull request Jul 30, 2021
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 28, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: traverse PR: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants