Repository navigation
Conversation
…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
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/63030 |
|
commit: |
| 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; |
There was a problem hiding this comment.
When can we have a body with holes?
There was a problem hiding this comment.
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:
babel/packages/babel-generator/src/printer.ts
Lines 958 to 960 in 3fc8c72
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; | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
"use string"; 010;for input("use strict"); 010;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.