Skip to content

Parenthesize let [-prefixed expression statements#469

Open
sarathfrancis90 wants to merge 1 commit into
estools:masterfrom
sarathfrancis90:fix-let-bracket-expression-statement
Open

Parenthesize let [-prefixed expression statements#469
sarathfrancis90 wants to merge 1 commit into
estools:masterfrom
sarathfrancis90:fix-let-bracket-expression-statement

Conversation

@sarathfrancis90

Copy link
Copy Markdown

An ExpressionStatement cannot begin with the token sequence let [, because the parser then treats it as the start of a lexical declaration. escodegen already guards the analogous {, function and class cases, but not this one, so it produced broken output for expressions whose leading identifier is let followed by a computed member access.

For example, given the AST for:

(let[a]) = b;
(let[a])();

escodegen generated:

let[a] = b;   // reparses as a `let [a] = b` destructuring declaration
let[a]();     // SyntaxError on reparse

The first silently changes an assignment into a declaration; the second is not valid JavaScript at all.

The fix adds a small isLetPrefixed check alongside the existing statement-start guards and wraps the expression in parentheses when its generated text starts with let [. Identifiers that merely start with let (e.g. lettuce[a]) and real let declarations are unaffected.

Tested with npm test (all existing tests still pass) plus a new round-trip fixture under test/compare-acorn-es2021/ that parses the affected forms, regenerates them and confirms the output reparses to an identical AST.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant