Skip to content

Commit a5ab330

Browse files
fix(javascript): print from a SyntaxTree the directives, import() parentheses and dropped comments printEstree prints
The tree printer main gained in #22643 now spells an escaped directive as written (inline-script safe), parenthesizes an import() under new or around a sequence, and keeps the comments of an empty statement it drops. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4
1 parent 097e086 commit a5ab330

1 file changed

Lines changed: 39 additions & 18 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38298,6 +38298,23 @@ const printedNameOf = (node) => {
3829838298
/** @typedef {EstreeTable<void>} EstreeGenerators per ESTree type, what prints a node of it */
3829938299
/** @typedef {EstreeTable<boolean | undefined>} EstreeParensTests per ESTree type, whether a node of it is parenthesized where it is printed */
3830038300

38301+
/**
38302+
* A string's spelling made safe in an inline `<script>`, as `encode_string`
38303+
* makes a value: each escape added reads as what it replaces.
38304+
* @param {string} spelling the text between a string's quotes
38305+
* @returns {string} the same string, spelled so no `</script` or `<!--` shows
38306+
*/
38307+
const inlineScriptSpelling = (spelling) =>
38308+
spelling
38309+
.replace(/<\u002F(script)([>/\t\n\f\r ])/gi, "<\\/$1$2")
38310+
// An escaped `\<` gives its backslash up to the `\x3c` taking its place.
38311+
.replace(
38312+
/(\\*)\u003C!--/g,
38313+
(_match, slashes) =>
38314+
`${slashes.length % 2 === 1 ? slashes.slice(1) : slashes}\\x3c!--`
38315+
)
38316+
.replace(/--\u003E/g, "--\\x3e");
38317+
3830138318
/**
3830238319
* Installs `printEstree`, the printer over the compressed tree as it is held:
3830338320
* a code generator and a test for its parentheses per ESTree type, each
@@ -39639,23 +39656,6 @@ const installEstreePrint = (modules) => {
3963939656
else output.print_template_string_chars(text);
3964039657
};
3964139658

39642-
/**
39643-
* A string's spelling made safe in an inline `<script>`, as `encode_string`
39644-
* makes a value: each escape added reads as what it replaces.
39645-
* @param {string} spelling the text between a string's quotes
39646-
* @returns {string} the same string, spelled so no `</script` or `<!--` shows
39647-
*/
39648-
const inlineScriptSpelling = (spelling) =>
39649-
spelling
39650-
.replace(/<\u002F(script)([>/\t\n\f\r ])/gi, "<\\/$1$2")
39651-
// An escaped `\<` gives its backslash up to the `\x3c` taking its place.
39652-
.replace(
39653-
/(\\*)\u003C!--/g,
39654-
(_match, slashes) =>
39655-
`${slashes.length % 2 === 1 ? slashes.slice(1) : slashes}\\x3c!--`
39656-
)
39657-
.replace(/--\u003E/g, "--\\x3e");
39658-
3965939659
/** @type {EstreeGenerators} */
3966039660
const codegen = {
3966139661
Program: (node, output) => {
@@ -41761,6 +41761,8 @@ const installTreePrint = (modules) => {
4176141761
output.in_directive = false;
4176241762
}
4176341763
if (!isEmpty) printNode(statement);
41764+
// terser drops an empty statement's comments with it.
41765+
else output.prepend_comments(statement);
4176441766
if (output.in_directive === true && isStringStatement(statement)) {
4176541767
output.in_directive = false;
4176641768
}
@@ -42595,7 +42597,17 @@ const installTreePrint = (modules) => {
4259542597
const directive = tree.textOf(Part.directive, node);
4259642598
const expression = childOf(Field.expression, node);
4259742599
if (directive !== null) {
42598-
output.print_string(directive, quoteOf(expression));
42600+
const value = /** @type {string} */ (tree.value(expression));
42601+
// Printed as spelled where it holds escapes, as `printEstree` prints it.
42602+
if (directive !== value) {
42603+
const quote = quoteOf(expression);
42604+
const spelled = output.option("inline_script")
42605+
? inlineScriptSpelling(directive)
42606+
: directive;
42607+
output.print(`${quote}${spelled}${quote}`);
42608+
} else {
42609+
output.print_string(value, quoteOf(expression));
42610+
}
4259942611
} else {
4260042612
printNode(expression);
4260142613
}
@@ -43496,6 +43508,7 @@ const installTreePrint = (modules) => {
4349643508
(type === NodeType.ForOfStatement &&
4349743509
node === childOf(Field.right, parent)) ||
4349843510
type === NodeType.YieldExpression ||
43511+
type === NodeType.ImportExpression ||
4349943512
type === NodeType.ExportNamedDeclaration ||
4350043513
type === NodeType.ExportDefaultDeclaration ||
4350143514
type === NodeType.ExportAllDeclaration
@@ -43563,6 +43576,14 @@ const installTreePrint = (modules) => {
4356343576
childOf(Field.left, grandparent) === parent
4356443577
);
4356543578
};
43579+
// As `printEstree` keeps them: `new import(a)` does not parse.
43580+
parensTests[NodeType.ImportExpression] = (node) => {
43581+
const parent = parentAt(0);
43582+
return (
43583+
typeOf(parent) === NodeType.NewExpression &&
43584+
childOf(Field.callee, parent) === node
43585+
);
43586+
};
4356643587
parensTests[NodeType.NewExpression] = (node) => {
4356743588
const parent = parentAt(0);
4356843589
const type = typeOf(parent);

0 commit comments

Comments
 (0)