Skip to content

Disallow destructuring assignment in using declarations#16613

Merged
nicolo-ribaudo merged 4 commits into
babel:mainfrom
H0onnn:fix/#16604
Jul 8, 2024
Merged

Disallow destructuring assignment in using declarations#16613
nicolo-ribaudo merged 4 commits into
babel:mainfrom
H0onnn:fix/#16604

Conversation

@H0onnn
Copy link
Copy Markdown
Contributor

@H0onnn H0onnn commented Jul 5, 2024

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

A syntax error occurs when using destructuring assignment in a using declaration.

@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Jul 5, 2024

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

@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: parser Spec: Explicit Resource Management labels Jul 5, 2024
Copy link
Copy Markdown
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.

Thank you! This PR is already good as is, but it would be even better if we could give a good error also for this case:

using { x } = y;

by adapting the logic in https://github.com/H0onnn/babel/blob/3ae03e1b53c69eec8850071eca53908c09c1d6ed/packages/babel-parser/src/parser/statement.ts#L505-L513 so that if using is followed on the same line by { (other than by an identifier) we parse it as an using declaration, and then it will raise the error that you added in this PR.

Unfortunately we cannot do the same for [, because using [x] = y is valid code (setting the x property of the variable using).

@H0onnn
Copy link
Copy Markdown
Contributor Author

H0onnn commented Jul 5, 2024

@nicolo-ribaudo Hello! Thank you for the quick review. Added code to allow for 'ArrayPattern'. Will this work meet your requirements?

@nicolo-ribaudo
Copy link
Copy Markdown
Member

nicolo-ribaudo commented Jul 5, 2024

@H0onnn No, your original commit was correct :) We must report an error for using y = null, [] = null;.

My suggestion was to change the logic in https://github.com/H0onnn/babel/blob/3ae03e1b53c69eec8850071eca53908c09c1d6ed/packages/babel-parser/src/parser/statement.ts#L505-L513 to also check for { after using, so that we also get the nice error message for the using { x } = y case.

If you prefer, we can also merge this PR (with only the first commit, and not the second), and then do this suggestion in a separate PR.

@H0onnn
Copy link
Copy Markdown
Contributor Author

H0onnn commented Jul 5, 2024

@nicolo-ribaudo I understand. Please understand that my English skills are not good. 😅

I'll keep trying !

"start":0,"end":30,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":30}},
"errors": [
"SyntaxError: Using declaration cannot have destructuring patterns. (2:8)"
],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a valid case, when the placeholder plugin is enabled, using %%name%% = null is a valid production. Can we throw the error when id is either ArrayPattern or ObjectPattern?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hello @JLHwung! Thank you for your feedback. As you said, modified it to throw an error only in the case of ObjectPattern and ArrayPattern.

@H0onnn
Copy link
Copy Markdown
Contributor Author

H0onnn commented Jul 6, 2024

@nicolo-ribaudo I hope that the logic added this commit will satisfy your requirements!

Copy link
Copy Markdown
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.

I pushed a commit to improve the error message of using { (and added a test) -- thank you!

Copy link
Copy Markdown
Contributor

@JLHwung JLHwung 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 changed the title Fix error handling destructuring assignment of using declarations Disallow destructuring assignment in using declarations Jul 8, 2024
@nicolo-ribaudo nicolo-ribaudo merged commit 0cd31fc into babel:main Jul 8, 2024
@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 8, 2024
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Oct 8, 2024
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: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Explicit Resource Management

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: using declarations shoud disallow destructuring

4 participants