Skip to content

Print parentheses around identifier let where necessary#13269

Merged
nicolo-ribaudo merged 3 commits intobabel:mainfrom
Zalathar:forbidden-let
May 6, 2021
Merged

Print parentheses around identifier let where necessary#13269
nicolo-ribaudo merged 3 commits intobabel:mainfrom
Zalathar:forbidden-let

Conversation

@Zalathar
Copy link
Copy Markdown
Contributor

@Zalathar Zalathar commented May 6, 2021

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

If we detect that the identifier let would be printed in a location that forbids it (either unconditionally or when followed by [), print parentheses around the identifier so that it remains legal and correct.

The forbidden locations are:

  • ExpressionStatement (let [)
  • ForStatement (let [)
  • ForInStatement (let [)
  • ForOfStatement (let) (regular and await)

@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented May 6, 2021

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

@codesandbox-ci
Copy link
Copy Markdown

codesandbox-ci Bot commented May 6, 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 1aea906:

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

@nicolo-ribaudo nicolo-ribaudo added pkg: generator PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels May 6, 2021
@Zalathar
Copy link
Copy Markdown
Contributor Author

Zalathar commented May 6, 2021

Ah, I just remembered that OptionalMemberExpression can also print a left bracket if { computed: true, optional: false }.

I don't think this can occur in head position in a freshly-parsed AST, but it can occur if a transform rearranges nodes and also sets node.optional = false.

Adding the appropriate check is easy enough, but I'll need to write a non-fixture test to get coverage.

@nicolo-ribaudo
Copy link
Copy Markdown
Member

Ideally we should add some validation to ensure that optional: false can only happen if it has a (deep) child with optional: true, but adding the generator check is a good idea.

Comment thread packages/babel-generator/src/node/parentheses.ts
@nicolo-ribaudo nicolo-ribaudo merged commit 68bc4df into babel:main May 6, 2021
@nicolo-ribaudo
Copy link
Copy Markdown
Member

Thanks!

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: generator 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.

[Bug]: Generator prints the identifier let in some forbidden places

5 participants