Skip to content

Commit deee15f

Browse files
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.
1 parent 159dfbf commit deee15f

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
@@ -1684,6 +1684,7 @@ const createCompressorScope = (type, node, upper, isVarScope) =>
16841684
* @property {PrintIdentifier[] | null} functionDeclarators under `keep_fnames`, the names declarators give anonymous functions, in the order the mangler asks `keep_fnames` of them
16851685
* @property {PrintIdentifier[]} catchParameters unless `ie8` or `safari10` move them, the catch parameters, in the order the mangler walks them
16861686
* @property {number[]} catchParameterScopeCounts how many scopes the walk had opened at each catch parameter
1687+
* @property {boolean} namesPrivate whether the program names a private member, which renaming walks it for
16871688
*/
16881689

16891690
/**
@@ -1832,6 +1833,8 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
18321833
this.catchParameters = [];
18331834
/** @type {number[]} */
18341835
this.catchParameterScopeCounts = [];
1836+
/** @type {boolean} whether the program names a private member */
1837+
this.namesPrivate = false;
18351838
/** @type {PrintNode[]} the nodes terser's walk has pushed, the node visited last */
18361839
this.stack = [];
18371840
/** @type {boolean} */
@@ -2334,7 +2337,11 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
23342337
case "MemberExpression":
23352338
stack.push(current);
23362339
this._visitPrint(current.object);
2337-
if (current.computed) this._visitPrint(current.property);
2340+
if (current.computed) {
2341+
this._visitPrint(current.property);
2342+
} else if (current.property.type === "PrivateIdentifier") {
2343+
this.namesPrivate = true;
2344+
}
23382345
stack.pop();
23392346
return;
23402347
case "CallExpression":
@@ -2509,6 +2516,7 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
25092516
return;
25102517
case "Property":
25112518
case "PropertyDefinition":
2519+
if (current.key.type === "PrivateIdentifier") this.namesPrivate = true;
25122520
stack.push(current);
25132521
if (current.computed) this._visitPrint(current.key);
25142522
if (current.value) this._visitPrint(current.value);
@@ -2530,12 +2538,14 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
25302538
this._visitList(current.args);
25312539
stack.pop();
25322540
return;
2541+
case "MethodDefinition":
2542+
if (current.key.type === "PrivateIdentifier") this.namesPrivate = true;
2543+
// falls through
25332544
case "IfStatement":
25342545
case "ConditionalExpression":
25352546
case "SwitchCase":
25362547
case "ArrayExpression":
25372548
case "ObjectExpression":
2538-
case "MethodDefinition":
25392549
case "SequenceExpression":
25402550
case "TaggedTemplateExpression":
25412551
case "ChainExpression":
@@ -2545,11 +2555,13 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
25452555
super._visit(/** @type {Node} */ (/** @type {unknown} */ (current)));
25462556
stack.pop();
25472557
return;
2558+
case "PrivateIdentifier":
2559+
this.namesPrivate = true;
2560+
return;
25482561
case "Super":
25492562
case "EmptyStatement":
25502563
case "DebuggerStatement":
25512564
case "MetaProperty":
2552-
case "PrivateIdentifier":
25532565
case "TemplateElement":
25542566
return;
25552567
case "ImportDeclaration": {
@@ -2753,7 +2765,8 @@ const analyzeMinifierScope = (program, options, raise, release) => {
27532765
blockDefunScopes: analyzer.blockDefunScopes,
27542766
functionDeclarators: analyzer.functionDeclarators,
27552767
catchParameters: analyzer.catchParameters,
2756-
catchParameterScopeCounts: analyzer.catchParameterScopeCounts
2768+
catchParameterScopeCounts: analyzer.catchParameterScopeCounts,
2769+
namesPrivate: analyzer.namesPrivate
27572770
};
27582771
};
27592772

0 commit comments

Comments
 (0)