Parenthesize let [-prefixed expression statements#469
Open
sarathfrancis90 wants to merge 1 commit into
Open
Conversation
An ExpressionStatement whose generated text starts with the token
sequence `let [` is reparsed as a lexical declaration, so escodegen
emitted invalid or semantically different code for expressions such as
`(let[a])()` or `(let[a]) = b`. Handle it like the existing `{`,
`function` and `class` statement-start cases and wrap it in parentheses.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An
ExpressionStatementcannot begin with the token sequencelet [, because the parser then treats it as the start of a lexical declaration. escodegen already guards the analogous{,functionandclasscases, but not this one, so it produced broken output for expressions whose leading identifier isletfollowed by a computed member access.For example, given the AST for:
escodegen generated:
The first silently changes an assignment into a declaration; the second is not valid JavaScript at all.
The fix adds a small
isLetPrefixedcheck alongside the existing statement-start guards and wraps the expression in parentheses when its generated text starts withlet [. Identifiers that merely start withlet(e.g.lettuce[a]) and realletdeclarations are unaffected.Tested with
npm test(all existing tests still pass) plus a new round-trip fixture undertest/compare-acorn-es2021/that parses the affected forms, regenerates them and confirms the output reparses to an identical AST.