Skip to content

Commit cd33959

Browse files
fix(javascript): read only ESTree's directives as directives
With corrections on and no source held, a parenthesized or escaped leading string given as ESTree no longer turns a body strict; the tree's own `directive` field decides. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4
1 parent 4fe2b8c commit cd33959

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11741,9 +11741,10 @@ const installEstree = (modules) => {
1174111741
/**
1174211742
* Turns the leading string statements of a body into directives.
1174311743
* @param {Node[]} body the statements
11744+
* @param {EXPECTED_ANY[]=} prologue the ESTree statements, whose `directive` says which are directives when no source is held
1174411745
* @returns {Node[]} the same array
1174511746
*/
11746-
const normalizeDirectives = (body) => {
11747+
const normalizeDirectives = (body, prologue) => {
1174711748
for (let i = 0; i < body.length; i++) {
1174811749
const statement = /** @type {EXPECTED_ANY} */ (body[i]);
1174911750
if (
@@ -11752,7 +11753,12 @@ const installEstree = (modules) => {
1175211753
) {
1175311754
return body;
1175411755
}
11755-
const raw = directiveSource(statement.expression);
11756+
let raw = directiveSource(statement.expression);
11757+
if (raw === null && prologue !== undefined) {
11758+
const { directive, expression } = prologue[i];
11759+
if (typeof directive !== "string") return body;
11760+
raw = expression.raw || JSON.stringify(directive);
11761+
}
1175611762
// A string the source does not open a prologue with ends it: one inside
1175711763
// parentheses is an expression, whatever it spells.
1175811764
if (raw === undefined) return body;
@@ -11781,14 +11787,22 @@ const installEstree = (modules) => {
1178111787
const correcting = () =>
1178211788
modules.corrections !== undefined && modules.corrections.enabled;
1178311789

11790+
/**
11791+
* @param {EXPECTED_ANY[]} statements a body given as ESTree
11792+
* @returns {EXPECTED_ANY[] | undefined} them, when their `directive` decides
11793+
* the prologue: terser's conversion reads any leading string as one
11794+
*/
11795+
const prologueFromEstree = (statements) =>
11796+
tokenAt === null && correcting() ? statements : undefined;
11797+
1178411798
/**
1178511799
* A function's block, its leading string statements read as directives.
1178611800
* @param {EXPECTED_ANY} estree the block
1178711801
* @returns {Node} terser's block
1178811802
*/
1178911803
const blockFromEstree = (estree) => {
1179011804
const block = fromEstree(estree);
11791-
normalizeDirectives(block.body);
11805+
normalizeDirectives(block.body, prologueFromEstree(estree.body));
1179211806
return block;
1179311807
};
1179411808

@@ -12176,7 +12190,10 @@ const installEstree = (modules) => {
1217612190
ToplevelNode({
1217712191
startToken: startToken(estree),
1217812192
endToken: endToken(estree),
12179-
body: normalizeDirectives(estree.body.map(fromEstree))
12193+
body: normalizeDirectives(
12194+
estree.body.map(fromEstree),
12195+
prologueFromEstree(estree.body)
12196+
)
1218012197
}),
1218112198
ArrayPattern: (estree) =>
1218212199
makeNode(DestructuringNode, undefined, {

‎test/unitCases/syntax-printer.unittest.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6218,6 +6218,33 @@ describe("syntax-printer", () => {
62186218
).toBe(reference.code);
62196219
});
62206220

6221+
it("should read only ESTree's directives as directives, where terser reads any leading string", async () => {
6222+
const { minify } = await load();
6223+
/** @type {EXPECTED_ANY} */
6224+
const options = { parse: { spidermonkey: true } };
6225+
/**
6226+
* @param {string} code a script
6227+
* @returns {EXPECTED_ANY} acorn's tree for it
6228+
*/
6229+
const script = (code) => acorn.parse(code, { ecmaVersion: "latest" });
6230+
for (const code of [
6231+
'(() => { ("use strict"); x = 1; })();',
6232+
'(() => { "use\\x20strict"; x = 1; })();',
6233+
'(function () { ("use strict"); x = 1; })();'
6234+
]) {
6235+
expect((await minify(script(code), options)).code).toBe("x=1;");
6236+
}
6237+
const code = '("use strict"); x = 1; function f() {}';
6238+
expect((await minify(script(code), options)).code).toBe(
6239+
"function f(){}x=1;"
6240+
);
6241+
const reference = await terserReference().minify(script(code), options);
6242+
expect(reference.code).toBe('"use strict";function f(){}x=1;');
6243+
expect(
6244+
(await uncorrected(() => minify(script(code), options))).code
6245+
).toBe(reference.code);
6246+
});
6247+
62216248
it("should give back a template inlined into another, which terser throws on", async () => {
62226249
const { minify } = await load();
62236250
const code = "console.log(`x${`y${a}`}z`, `p${`q`}r${b}`);";

0 commit comments

Comments
 (0)