Skip to content

Commit 6d5ffab

Browse files
test(javascript): count the node printer apart from printTree
`minifyCounted` added both routes into one count, so a source routed back to `printTree` still passed; each test now asserts which printer ran. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent 4944119 commit 6d5ffab

1 file changed

Lines changed: 28 additions & 13 deletions

File tree

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

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12644,27 +12644,28 @@ const OPTION_SETS = {
1264412644
* @param {import("../../../lib/javascript/syntax-printer").MinifySources} input the source, or the sources by name
1264512645
* @param {import("../../../lib/javascript/syntax-printer").MinifyOptions} options the options
1264612646
* @param {boolean} fromTree whether the tree printer may print it
12647-
* @returns {Promise<{ code: string | undefined, map: import("../../../lib/javascript/syntax-printer").MinifyOutput["map"], printedFromTree: number }>} the code, its source map, and how often the tree printer printed
12647+
* @returns {Promise<{ code: string | undefined, map: import("../../../lib/javascript/syntax-printer").MinifyOutput["map"], printedFromTree: number, printedNodes: number }>} the code, its source map, how often `printTree` printed and how often the node printer did
1264812648
*/
1264912649
const minifyCounted = async (input, options, fromTree) => {
1265012650
const { minify, modules } = await load();
1265112651
const { printTree, treePrinting } = modules;
1265212652
const syntaxParser = require("../../../lib/javascript/syntax-parser");
1265312653
const { walkSyntaxTree } = syntaxParser;
1265412654
let printedFromTree = 0;
12655+
let printedNodes = 0;
1265512656
modules.printTree = (/** @type {EXPECTED_ANY[]} */ ...args) => {
1265612657
printedFromTree++;
1265712658
return printTree(...args);
1265812659
};
1265912660
// A source printed with the node printer is walked once, to print it.
1266012661
syntaxParser.walkSyntaxTree = (...args) => {
12661-
printedFromTree++;
12662+
printedNodes++;
1266212663
return walkSyntaxTree(...args);
1266312664
};
1266412665
treePrinting.enabled = fromTree;
1266512666
try {
1266612667
const { code, map } = await minify(input, options);
12667-
return { code, map, printedFromTree };
12668+
return { code, map, printedFromTree, printedNodes };
1266812669
} finally {
1266912670
modules.printTree = printTree;
1267012671
syntaxParser.walkSyntaxTree = walkSyntaxTree;
@@ -12692,8 +12693,11 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1269212693
continue;
1269312694
}
1269412695
const printed = await minifyCounted(source, options(), true);
12695-
expect(reference.printedFromTree).toBe(0);
12696-
expect(printed.printedFromTree).toBe(1);
12696+
expect(reference.printedFromTree + reference.printedNodes).toBe(0);
12697+
// A source map is still printed by `printTree`.
12698+
const mapped = "sourceMap" in set ? 1 : 0;
12699+
expect(printed.printedFromTree).toBe(mapped);
12700+
expect(printed.printedNodes).toBe(1 - mapped);
1269712701
expect(printed.code).toBe(reference.code);
1269812702
expect(printed.map).toEqual(reference.map);
1269912703
}
@@ -12703,7 +12707,11 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1270312707
it("should read a single named source under its name", async () => {
1270412708
const options = { compress: false, mangle: false };
1270512709
const printed = await minifyCounted({ "a.js": "a( 1 )" }, options, true);
12706-
expect(printed).toEqual({ code: "a(1);", printedFromTree: 1 });
12710+
expect(printed).toEqual({
12711+
code: "a(1);",
12712+
printedFromTree: 0,
12713+
printedNodes: 1
12714+
});
1270712715
});
1270812716

1270912717
it("should throw the parse error terser's tree throws", async () => {
@@ -12731,17 +12739,20 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1273112739
});
1273212740

1273312741
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 })]
12742+
["the driver's own options", () => ({ code: true, ast: false }), 1],
12743+
// Tested ahead of printing, it would keep other comments than terser's.
12744+
["a global regular expression", () => ({ comments: /keep/g }), 0],
12745+
["a sticky regular expression", () => ({ comments: /keep/y }), 0]
1273712746
])(
1273812747
"should print what terser's tree prints, with %s",
12739-
async (_name, format) => {
12748+
async (_name, format, printedNodes) => {
1274012749
const source = "/*keep*/a();/*keep*/b();/*no*/c();/*keep*/d()";
1274112750
const options = () => ({ compress: false, mangle: false, format: format() });
1274212751
const reference = await minifyCounted(source, options(), false);
1274312752
const printed = await minifyCounted(source, options(), true);
1274412753
expect(printed.code).toBe(reference.code);
12754+
expect(printed.printedNodes).toBe(printedNodes);
12755+
expect(printed.printedFromTree).toBe(1 - printedNodes);
1274512756
}
1274612757
);
1274712758

@@ -12771,13 +12782,17 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1277112782
]
1277212783
])("should print terser's tree of a source %s", async (_name, options) => {
1277312784
const printed = await minifyCounted("a( 1 )", options, true);
12774-
expect(printed.printedFromTree).toBe(0);
12785+
expect(printed.printedFromTree + printed.printedNodes).toBe(0);
1277512786
});
1277612787

1277712788
it("should print terser's tree of several sources", async () => {
1277812789
const options = { compress: false, mangle: false };
1277912790
const printed = await minifyCounted({ "a.js": "a()", "b.js": "b()" }, options, true);
12780-
expect(printed).toEqual({ code: "a();b();", printedFromTree: 0 });
12791+
expect(printed).toEqual({
12792+
code: "a();b();",
12793+
printedFromTree: 0,
12794+
printedNodes: 0
12795+
});
1278112796
});
1278212797

1278312798
it("should print terser's tree with the correct or improve phase off", async () => {
@@ -12787,7 +12802,7 @@ describe("JavaScript minifier printing a SyntaxTree", () => {
1278712802
/** @type {{ enabled: boolean }} */ (phase).enabled = false;
1278812803
try {
1278912804
const printed = await minifyCounted("a( 1 )", options, true);
12790-
expect(printed.printedFromTree).toBe(0);
12805+
expect(printed.printedFromTree + printed.printedNodes).toBe(0);
1279112806
} finally {
1279212807
/** @type {{ enabled: boolean }} */ (phase).enabled = true;
1279312808
}

0 commit comments

Comments
 (0)