Skip to content

Commit fc68c44

Browse files
perf(javascript): print a mangled program once, naming it afterwards (#22504)
* perf(javascript): print a mangled program once, naming it afterwards The mangle phase printed the tree twice: once to count the characters the names are chosen from, once for the result. It now prints before the names are chosen, recording where each name and each import or export `as` clause lies, counts the characters off that text and writes the names into it once chosen, moving the source map's columns with them. The `let` declarations the improve phase left for after naming are written before that print, and the count puts back what they changed. A print that wrote a comment, shebang or preamble still counts with a print of its own, as those are not counted. * test(javascript): fit two minifier tests to #22473 and #22499 together #22473 counts a conversion as code that may run, so the value #22499's improved case moved past `console` now has effects and stays put; it now moves a comparison, which converts nothing. And #22499 now shortens #22473's literal case, whose assertion holds the output to terser's, so that case runs with the improve phase off. With both in, `vercel/ms` is bigger than oxc's output again (787 against 781 gzip), so it goes back on OXC_SMALLER, where #22499 had retired it on its own. And #22502 leaves terser's `side_effects_cascade_3` no bigger than oxc's, so it comes off. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 79aa5df commit fc68c44

4 files changed

Lines changed: 603 additions & 82 deletions

File tree

‎lib/javascript/ScopeAnalyzer.js‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1691,6 +1691,7 @@ const createCompressorScope = (type, node, upper, isVarScope) =>
16911691
* @property {Set<MinifierScope>} blockDefunScopes the function scopes holding a function declared in a block
16921692
* @property {PrintIdentifier[] | null} functionDeclarators under `keep_fnames`, the names declarators give anonymous functions, in the order the mangler asks `keep_fnames` of them
16931693
* @property {PrintIdentifier[]} catchParameters unless `ie8` or `safari10` move them, the catch parameters, each named where the mangler's walk meets its scope
1694+
* @property {boolean} namesPrivate whether the program names a private member, which renaming walks it for
16941695
*/
16951696

16961697
/**
@@ -1835,6 +1836,8 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
18351836
// Otherwise the catch parameters, which the mangler names with their scope.
18361837
/** @type {PrintIdentifier[]} */
18371838
this.catchParameters = [];
1839+
/** @type {boolean} whether the program names a private member */
1840+
this.namesPrivate = false;
18381841
/** @type {PrintNode[]} the nodes terser's walk has pushed, the node visited last */
18391842
this.stack = [];
18401843
/** @type {boolean} */
@@ -2344,7 +2347,11 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
23442347
case "MemberExpression":
23452348
stack.push(current);
23462349
this._visitPrint(current.object);
2347-
if (current.computed) this._visitPrint(current.property);
2350+
if (current.computed) {
2351+
this._visitPrint(current.property);
2352+
} else if (current.property.type === "PrivateIdentifier") {
2353+
this.namesPrivate = true;
2354+
}
23482355
stack.pop();
23492356
return;
23502357
case "CallExpression":
@@ -2519,6 +2526,7 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
25192526
return;
25202527
case "Property":
25212528
case "PropertyDefinition":
2529+
if (current.key.type === "PrivateIdentifier") this.namesPrivate = true;
25222530
stack.push(current);
25232531
if (current.computed) this._visitPrint(current.key);
25242532
if (current.value) this._visitPrint(current.value);
@@ -2540,12 +2548,14 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
25402548
this._visitList(current.args);
25412549
stack.pop();
25422550
return;
2551+
case "MethodDefinition":
2552+
if (current.key.type === "PrivateIdentifier") this.namesPrivate = true;
2553+
// falls through
25432554
case "IfStatement":
25442555
case "ConditionalExpression":
25452556
case "SwitchCase":
25462557
case "ArrayExpression":
25472558
case "ObjectExpression":
2548-
case "MethodDefinition":
25492559
case "SequenceExpression":
25502560
case "TaggedTemplateExpression":
25512561
case "ChainExpression":
@@ -2555,11 +2565,13 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
25552565
super._visit(/** @type {Node} */ (/** @type {unknown} */ (current)));
25562566
stack.pop();
25572567
return;
2568+
case "PrivateIdentifier":
2569+
this.namesPrivate = true;
2570+
return;
25582571
case "Super":
25592572
case "EmptyStatement":
25602573
case "DebuggerStatement":
25612574
case "MetaProperty":
2562-
case "PrivateIdentifier":
25632575
case "TemplateElement":
25642576
return;
25652577
case "ImportDeclaration": {
@@ -2762,7 +2774,8 @@ const analyzeMinifierScope = (program, options, raise, release) => {
27622774
globals: analyzer.globals,
27632775
blockDefunScopes: analyzer.blockDefunScopes,
27642776
functionDeclarators: analyzer.functionDeclarators,
2765-
catchParameters: analyzer.catchParameters
2777+
catchParameters: analyzer.catchParameters,
2778+
namesPrivate: analyzer.namesPrivate
27662779
};
27672780
};
27682781

0 commit comments

Comments
 (0)