Skip to content

fix(generator): parenthesize string statements in directive position - #18528

Open
JLHwung wants to merge 2 commits into
babel:mainfrom
JLHwung:fix-generator-parenthesized-string-expression-statement
Open

JLHwung wants to merge 2 commits into
babel:mainfrom
JLHwung:fix-generator-parenthesized-string-expression-statement

Conversation

@JLHwung

@JLHwung JLHwung commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? Babel prints invalid JS "use string"; 010; for input ("use strict"); 010;
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
Need backport? 7.x
License MIT

In this PR we fix an issue reported by the Claude Fable: Babel generator strips the parentheses around the string expression statement which would render them into a directive prologue. Here we record the parenthesized prologue string in the generator state and keep the parentheses when we see such AST node.

…e position

`('use strict'); 010` (sourceType: "script", strictMode: false) was printed
as `'use strict';\n010;`. The parenthesized string is an ExpressionStatement,
not a Directive, but the printed form re-parses as a "use strict" directive,
so the output changes semantics and `010` becomes a SyntaxError. The same
happened at the start of a function body.

A StringLiteral with `extra.parenthesized` now keeps its parentheses when only
bare string statements precede it in a Program, function body or TS namespace
body. That includes a string that was first in the source and had a bare string
inserted before it. Strings after it do not need parentheses, because it already
ends the prologue, and plain blocks and static blocks have no prologue. Holes in
the body are skipped, as the printer skips them.

Strings that were not parenthesized in the source still print bare, so ASTs
built as ExpressionStatement(StringLiteral) keep printing as directives. Plugins
rely on this to insert directives such as "use client", and the TypeScript
namespace transform relies on it to keep "use strict" in the generated function.

Assisted-by: Claude Fable 5.1
@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories pkg: generator 7.x: needs backport labels Nov 5, 2026
@babel-bot

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: 48614a9

for (let i = 0; i < body.length; i++) {
const statement = body[i];
// printJoin skips holes, so they do not end the prologue
if (!statement) continue;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

When can we have a body with holes?

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.

Yes you are right, in general Babel parser would not generate such AST. However, the check here is mostly to align with the current printJoin behaviour:

for (let i = 0; i < len; i++) {
const node = nodes[i];
if (!node) continue;

If a Babel plugin manually set node.body[i] = null for a program, the current generator will skip them automatically. Without this check, this PR will break on the next statement.type access.

If we want to disallow such case, I can open a follow-up PR to remove such guard, but to remove it here would make the error very confusing while it should have been rejected before this function is called.

return null;
}
if (statement.expression.extra?.parenthesized) return statement.expression;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In which case does this loop do a second iteration, other than with the continue statement above? I'm not sure I understand why checking just the first statement is not enough.

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.

Good question. In Program and function body, it is sufficient to check just the first statement, however, for TSModuleBlock we have to check further because 1) TSModuleBlock does not have directive property and 2) transform-typescript converts the module block to a function body, which would render a directive-like string into a directive. For example

namespace N {
  "a";
  ("use strict");
  010;
}

If we only check a and exit early, we will overlook ("use strict"); and the transform result will be

(function (N) {
  "a";
  "use strict";
  010;
})(N || (N = {}))

which would throw syntax error.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

7.x: needs backport 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.

3 participants