fix: parenthesize non-directive string literals in a directive prologue#740
Open
spokodev wants to merge 1 commit into
Open
fix: parenthesize non-directive string literals in a directive prologue#740spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
A string-literal expression statement at the start of a program or
function body that is NOT a directive (parenthesized in the source, or
produced by a transform that didn't set `directive`) was generated
without parentheses, so re-parsing promoted it to a directive. This
silently changes semantics (e.g. enabling strict mode) and can even
produce code that no longer parses:
generate(parse('("use strict"); with ({}) {}'))
// => '"use strict"; with ({}) {}' -> SyntaxError on reparse
Track the directive prologue while writing a Program/BlockStatement body
and parenthesize a string literal there only when it is not a directive
(ESTree marks directives via `node.directive`). Real directives and
mid-body string statements are unaffected.
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.
Bug
A string-literal expression statement at the start of a program or function body that is not a directive — parenthesized in the source, or built by a transform that didn't set
directive— is generated without parentheses. Re-parsing then promotes it to a directive, which silently changes semantics (e.g. enabling strict mode) and can produce code that no longer parses:The same happens inside function bodies, and for things like
("use strict"); var x = 010;(octal becomes a strict-mode error).generate(parse(x))is no longer idempotent for these inputs.Cause
ExpressionStatementonly parenthesizes for object/function ambiguity and assignment-to-pattern. It doesn't guard a string literal that sits at a directive-prologue position, so a non-directive string is emitted bare and is reparsed as a directive.Fix
Track the directive prologue while writing a
Program/BlockStatementbody (it stays open across leading string-literal expression statements), and inExpressionStatementparenthesize a string literal there only when it is not a directive — ESTree marks directives vianode.directive, which acorn sets.node.directiveset) are emitted bare, exactly as before.debugger;\n"…";is unchanged.Tests
Added
src/tests/fixtures/syntax/directive.js(covered by the existing Syntax check exact round-trip test). It exercises a real directive (stays bare), a parenthesized prologue non-directive (kept parenthesized), a mid-body string (stays bare), and the same inside function bodies. Onmainthis fixture fails (the parens are dropped); with this change it round-trips.