Skip to content

Commit 5f49ce5

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, each `const` the improve phase writes as `let` 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. A print that wrote a comment, shebang or preamble still counts with a print of its own, as those are not counted.
1 parent ab4d0e6 commit 5f49ce5

3 files changed

Lines changed: 486 additions & 59 deletions

File tree

‎lib/javascript/ScopeAnalyzer.js‎

Lines changed: 12 additions & 3 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} */
@@ -2509,6 +2512,7 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
25092512
return;
25102513
case "Property":
25112514
case "PropertyDefinition":
2515+
if (current.key.type === "PrivateIdentifier") this.namesPrivate = true;
25122516
stack.push(current);
25132517
if (current.computed) this._visitPrint(current.key);
25142518
if (current.value) this._visitPrint(current.value);
@@ -2530,12 +2534,14 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
25302534
this._visitList(current.args);
25312535
stack.pop();
25322536
return;
2537+
case "MethodDefinition":
2538+
if (current.key.type === "PrivateIdentifier") this.namesPrivate = true;
2539+
// falls through
25332540
case "IfStatement":
25342541
case "ConditionalExpression":
25352542
case "SwitchCase":
25362543
case "ArrayExpression":
25372544
case "ObjectExpression":
2538-
case "MethodDefinition":
25392545
case "SequenceExpression":
25402546
case "TaggedTemplateExpression":
25412547
case "ChainExpression":
@@ -2545,11 +2551,13 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
25452551
super._visit(/** @type {Node} */ (/** @type {unknown} */ (current)));
25462552
stack.pop();
25472553
return;
2554+
case "PrivateIdentifier":
2555+
this.namesPrivate = true;
2556+
return;
25482557
case "Super":
25492558
case "EmptyStatement":
25502559
case "DebuggerStatement":
25512560
case "MetaProperty":
2552-
case "PrivateIdentifier":
25532561
case "TemplateElement":
25542562
return;
25552563
case "ImportDeclaration": {
@@ -2753,7 +2761,8 @@ const analyzeMinifierScope = (program, options, raise, release) => {
27532761
blockDefunScopes: analyzer.blockDefunScopes,
27542762
functionDeclarators: analyzer.functionDeclarators,
27552763
catchParameters: analyzer.catchParameters,
2756-
catchParameterScopeCounts: analyzer.catchParameterScopeCounts
2764+
catchParameterScopeCounts: analyzer.catchParameterScopeCounts,
2765+
namesPrivate: analyzer.namesPrivate
27572766
};
27582767
};
27592768

0 commit comments

Comments
 (0)