Skip to content

Commit 4944119

Browse files
fix(javascript): pass minify's own format options and stateful filters right
The node printer was handed `format` with the driver's own `code`, `ast` and `spidermonkey` keys, which the stream refuses; it now gets the options the stream gets. A global or sticky regular expression filter is tested ahead of printing, moving its `lastIndex`, so such a source prints through `printTree` instead. Also puts back the oxc and swc corpus commits the previous commit moved by mistake. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent 0a33707 commit 4944119

4 files changed

Lines changed: 29 additions & 15 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9708,19 +9708,22 @@ const installMinify = (modules) => {
97089708
let parsedTree = null;
97099709
// Where only comments before its first token print, that token.
97109710
let leadingToken = 0;
9711+
const { comments } = options.format;
9712+
// The node printer prints a source neither mangled nor mapped; it tests
9713+
// a comment ahead of printing it, which a `lastIndex` moved would change.
9714+
const printsNodes =
9715+
options.mangle === false &&
9716+
!options.sourceMap &&
9717+
!(comments instanceof RegExp && (comments.global || comments.sticky));
97119718
if (treeSource !== null) {
97129719
// A source map's sources read the sources as a list, as parsing has them.
97139720
if (typeof files === "string") files = [files];
97149721
options.parse.toplevel = null;
97159722
delete options.parse.spidermonkey;
97169723
options.parse.filename = treeSource.name;
9717-
// The node printer, which prints a source neither mangled nor mapped,
9718-
// reads the tokens only for the comments it keeps, and only the first
9719-
// where it keeps those before it alone, which the program prints.
9720-
const keptBy =
9721-
options.mangle === false && !options.sourceMap
9722-
? commentFilterOf(options.format)
9723-
: undefined;
9724+
// The node printer reads the tokens only for the comments it keeps, and
9725+
// only the first where it keeps those before it alone.
9726+
const keptBy = printsNodes ? commentFilterOf(options.format) : undefined;
97249727
parsedTree = parseModule.parseTree(
97259728
treeSource.text,
97269729
options.parse,
@@ -10027,15 +10030,11 @@ const installMinify = (modules) => {
1002710030
treeNames.text,
1002810031
treeNames.recorded
1002910032
);
10030-
} else if (
10031-
parsedTree !== null &&
10032-
treeNames === null &&
10033-
!options.sourceMap
10034-
) {
10033+
} else if (parsedTree !== null && printsNodes) {
1003510034
result.code = printThroughNodes(
1003610035
parsedTree,
1003710036
/** @type {{ text: string }} */ (treeSource).text,
10038-
options.format,
10037+
/** @type {DriverFormatOptions} */ (formatOptions),
1003910038
leadingToken
1004010039
);
1004110040
} else if (parsedTree !== null) {

‎test/external/oxc‎

Submodule oxc updated 1937 files

‎test/external/swc‎

Submodule swc updated 1134 files

‎test/unitCases/javascript/printer.unittest.js‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12730,6 +12730,21 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1273012730
);
1273112731
});
1273212732

12733+
it.each([
12734+
["the driver's own options", () => ({ code: true, ast: false })],
12735+
["a global regular expression", () => ({ comments: /keep/g })],
12736+
["a sticky regular expression", () => ({ comments: /keep/y })]
12737+
])(
12738+
"should print what terser's tree prints, with %s",
12739+
async (_name, format) => {
12740+
const source = "/*keep*/a();/*keep*/b();/*no*/c();/*keep*/d()";
12741+
const options = () => ({ compress: false, mangle: false, format: format() });
12742+
const reference = await minifyCounted(source, options(), false);
12743+
const printed = await minifyCounted(source, options(), true);
12744+
expect(printed.code).toBe(reference.code);
12745+
}
12746+
);
12747+
1273312748
it.each([
1273412749
["compressed", { compress: {}, mangle: false }],
1273512750
[

0 commit comments

Comments
 (0)