Skip to content

Commit 089b4c0

Browse files
refactor(javascript): make the minifier's nodes plain ESTree objects (#22418)
* refactor(javascript): make the minifier's nodes plain objects from factories terser's node classes are gone from the minifier: each kind of node, what terser's class was, has a factory making a plain object with every field of its ESTree type in one order, so the nodes of a type share one shape. The `is<Kind>Node` predicates answer on a node's type and the values telling kinds apart (an Identifier's role, a VariableDeclaration's kind, a block's or function's role), `kindOf` names a node's kind, and the kind bits, comparisons and copies read it. A private member's key is typed PrivateIdentifier. * refactor(javascript): adopt ESTree without terser's node classes data The generator no longer reads terser's ast.js: the node kinds the factories are built from live in the printer, so the generated node-class table and the code deriving it are gone. The parser's ESTree is adopted by `ast.adoptEstree`, which a `parse.spidermonkey` input goes through too. * refactor(javascript): type the minifier nodes' common fields A node's type, name, kind, role, computed flag, label and property read as typed fields, and the test helpers type terser's factories and the ast they read kinds through, so type coverage stays above main's. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC * docs(javascript): describe the minifier's plain nodes and type-keyed functions Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC * refactor(javascript): read a node's kind bits by its type and role Kind bits asked `kindOf` of every visited node once nodes became plain objects, which doubled compress time; a table by type, then role, answers almost every node without it, and a kind's factory is named once. Also drops a stale comment and proves the nested size test's patch runs. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC * refactor(javascript): give scope nodes every field scope analysis writes Program, functions and classes now carry `screwy_argnames_scope`, and a static block the whole scope set, from the factory on, so figuring out scopes no longer moves these nodes to another hidden class. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent e930051 commit 089b4c0

8 files changed

Lines changed: 3467 additions & 4463 deletions

File tree

‎docs/syntax.md‎

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

‎lib/javascript/syntax-printer-data.js‎

Lines changed: 0 additions & 2582 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/javascript/syntax-printer.js‎

Lines changed: 3172 additions & 726 deletions
Large diffs are not rendered by default.

‎test/helpers/terserSources.js‎

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* terser's node classes under the names webpack gives them as well as their
77
* own: `AST_Node` is webpack's `SyntaxNode`, `AST_Call` its `CallNode`, and
88
* `AST_Token` its `ParsedToken`, with the `isCallNode` predicates webpack reads.
9-
* @param {EXPECTED_ANY} ast terser's `ast.js`
10-
* @returns {EXPECTED_ANY} it, reading under either name
9+
* @param {Record<string, EXPECTED_ANY>} ast terser's `ast.js`
10+
* @returns {import("../../lib/javascript/syntax-printer").Ast} it, reading under either name
1111
*/
1212
const webpackNames = (ast) => {
1313
const named = { ...ast };
@@ -20,11 +20,22 @@ const webpackNames = (ast) => {
2020
continue;
2121
}
2222
const className = type === "Node" ? "SyntaxNode" : `${type}Node`;
23-
named[className] = Type;
23+
// webpack's factories are called, where terser's classes are constructed.
24+
/**
25+
* @param {Record<string, unknown>} props the node's properties
26+
* @returns {EXPECTED_OBJECT} terser's node
27+
*/
28+
function factory(props) {
29+
return new Type(props);
30+
}
31+
factory.prototype = Type.prototype;
32+
named[className] = factory;
2433
named[`is${className}`] = (/** @type {unknown} */ value) =>
2534
value instanceof Type;
2635
}
27-
return named;
36+
return /** @type {import("../../lib/javascript/syntax-printer").Ast} */ (
37+
/** @type {unknown} */ (named)
38+
);
2839
};
2940

3041
/**
@@ -98,18 +109,57 @@ const thrownMessage = (error) =>
98109
? "TypeError"
99110
: String(error && error.message);
100111

112+
// The kinds terser has no class for, each a leaf of the class it extends,
113+
// which ESTree types apart.
114+
/** @type {Record<string, string>} */
115+
const ESTREE_LEAF_CLASSES = {
116+
Logical: "Binary",
117+
UpdatePrefix: "UnaryPrefix",
118+
CallExpression: "Call",
119+
SymbolImportForeignLiteral: "SymbolImportForeign",
120+
SymbolExportForeignLiteral: "SymbolExportForeign",
121+
SymbolExportLiteral: "SymbolExport",
122+
ArrayPattern: "Destructuring",
123+
ObjectPattern: "Destructuring",
124+
ExportAllDeclaration: "Export",
125+
ExportDefaultDeclaration: "Export",
126+
ExportNamedDeclaration: "Export",
127+
ImportSpecifier: "NameMapping",
128+
ImportNamespaceSpecifier: "NameMapping",
129+
ExportSpecifier: "NameMapping",
130+
RestElement: "Expansion",
131+
SpreadElement: "Expansion",
132+
ClassGetter: "ObjectGetter",
133+
ClassSetter: "ObjectSetter",
134+
ClassMethod: "ConciseMethod"
135+
};
136+
101137
/**
102138
* The class terser holds a node of one of webpack's classes as: a leaf class
103139
* terser has none for, which ESTree types apart, names the class it extends.
104140
* @param {string} type the class's `TYPE`
105141
* @returns {string} terser's class's `TYPE`
106142
*/
107-
const terserTypeOf = (type) => {
108-
const { ESTREE_LEAF_CLASSES } = require("../../tooling/generate-js-data");
109-
110-
return Object.prototype.hasOwnProperty.call(ESTREE_LEAF_CLASSES, type)
143+
const terserTypeOf = (type) =>
144+
Object.prototype.hasOwnProperty.call(ESTREE_LEAF_CLASSES, type)
111145
? terserTypeOf(ESTREE_LEAF_CLASSES[type])
112146
: type;
147+
148+
/** @type {import("../../lib/javascript/syntax-printer").Ast | undefined} */
149+
let ownAst;
150+
151+
/**
152+
* The class of a node of terser's tree, or the kind, terser's class, of a
153+
* node of webpack's, which holds it as data read by `kindOf`.
154+
* @param {import("../../lib/javascript/syntax-printer").Node} node a node of either tree
155+
* @returns {string} the class
156+
*/
157+
const kindName = (node) => {
158+
if (node.TYPE !== undefined) return node.TYPE;
159+
if (ownAst === undefined) {
160+
ownAst = require("../../lib/javascript/syntax-printer").createAst();
161+
}
162+
return String(ownAst.kindOf(node));
113163
};
114164

115-
module.exports = { loadTerserSources, terserTypeOf, thrownMessage };
165+
module.exports = { kindName, loadTerserSources, terserTypeOf, thrownMessage };

‎test/unitCases/syntax-printer-estree.unittest.js‎

Lines changed: 81 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
"use strict";
22

3+
// cspell:ignore argnames, bcatch, bfinally, argname
4+
35
// cspell:ignore endline, endcol, endpos, nlb, thedef
46

57
const acorn = require("acorn");
68
const { load } = require("../../lib/javascript/syntax").printer;
79
const {
10+
kindName,
811
loadTerserSources,
912
terserTypeOf,
1013
thrownMessage
@@ -16,18 +19,73 @@ const {
1619
*/
1720
const importTerserSource = (specifier) => import(specifier);
1821
const ACORN_CORPUS = require("../fixtures/acorn-corpus.json");
19-
const { nodeClasses } = require("../../lib/javascript/syntax-printer-data");
22+
const { NODE_KIND_ANCESTRY } = require("../../lib/javascript/syntax-printer");
23+
24+
// What terser calls each field webpack's nodes name as ESTree does, by the
25+
// kind renaming it, terser's class, whose kinds rename it too.
26+
/** @type {Record<string, Record<string, string>>} */
27+
const OWN_RENAMES = {
28+
Node: {"startToken": "start", "endToken": "end"},
29+
SimpleStatement: {"expression": "body"},
30+
DWLoop: {"test": "condition"},
31+
For: {"test": "condition", "update": "step"},
32+
ForIn: {"left": "init", "right": "object"},
33+
With: {"object": "expression"},
34+
Expansion: {"argument": "expression"},
35+
Lambda: {"id": "name", "params": "argnames", "generator": "is_generator"},
36+
PrefixedTemplateString: {"tag": "prefix", "quasi": "template_string"},
37+
Exit: {"argument": "value"},
38+
Await: {"argument": "expression"},
39+
Yield: {"argument": "expression", "delegate": "is_star"},
40+
If: {"test": "condition", "consequent": "body", "alternate": "alternative"},
41+
Switch: {"discriminant": "expression", "cases": "body"},
42+
SwitchBranch: {"consequent": "body"},
43+
Case: {"test": "expression"},
44+
Try: {"block": "body", "handler": "bcatch", "finalizer": "bfinally"},
45+
Catch: {"param": "argname"},
46+
DefinitionsLike: {"declarations": "definitions"},
47+
VarDefLike: {"id": "name", "init": "value"},
48+
NameMapping: {"local": "name"},
49+
Import: {"source": "module_name"},
50+
Export: {"source": "module_name"},
51+
Call: {"callee": "expression", "arguments": "args"},
52+
PropAccess: {"object": "expression"},
53+
Unary: {"argument": "expression"},
54+
Conditional: {"test": "condition", "alternate": "alternative"},
55+
Class: {"id": "name", "superClass": "extends"},
56+
PrivateIn: {"left": "key", "right": "value"},
57+
Symbol: {"definition": "thedef"}
58+
};
2059

21-
// What terser calls each field webpack's node classes name as ESTree does, by
22-
// class: the trees are compared under terser's names.
60+
// Each kind's renames, its own and those of the kinds it is one of: the
61+
// trees are compared under terser's names.
2362
/** @type {Map<string, Record<string, string>>} */
2463
const RENAMED_FIELDS = new Map(
25-
nodeClasses().map(({ type, renamed }) => [type, renamed])
64+
Object.keys(NODE_KIND_ANCESTRY).map((kind) => [
65+
kind,
66+
Object.assign(
67+
{},
68+
...[...NODE_KIND_ANCESTRY[kind]]
69+
.reverse()
70+
.map((name) => OWN_RENAMES[name] || {})
71+
)
72+
])
2673
);
2774

28-
// The fields webpack's node classes hold where ESTree has them and terser's
29-
// classes none.
30-
const ESTREE_ADDED_FIELDS = new Set(["computed", "kind", "method", "shorthand"]);
75+
// The fields webpack's nodes hold that terser's do not: ESTree's, and the
76+
// values that tell one kind of node from another of its type.
77+
const ESTREE_ADDED_FIELDS = new Set([
78+
"computed",
79+
"kind",
80+
"method",
81+
"shorthand",
82+
"type",
83+
"role",
84+
"atom",
85+
"prefix",
86+
"operator",
87+
"_block_scope"
88+
]);
3189

3290
// A token's fields, past which a converted tree keeps nothing a minify reads.
3391
const TOKEN_FIELDS = [
@@ -67,7 +125,7 @@ const firstDifference = (theirs, ours) => {
67125
typeof a === "string" &&
68126
typeof b === "object" &&
69127
b !== null &&
70-
(b.TYPE === "SymbolProperty" || b.TYPE === "SymbolPrivateProperty")
128+
(kindName(b) === "SymbolProperty" || kindName(b) === "SymbolPrivateProperty")
71129
) {
72130
return a === b.name ? undefined : `${where}: ${a} vs ${b.name}`;
73131
}
@@ -96,11 +154,13 @@ const firstDifference = (theirs, ours) => {
96154
// token class `ParsedToken`, where terser prefixes `AST_`, so the classes
97155
// are compared by what they hold rather than by name.
98156
const kindOf = (/** @type {EXPECTED_ANY} */ node) =>
99-
terserTypeOf(node.constructor.name.replace(/^(?:AST_|Parsed)/, "").replace(/Node$/, ""));
157+
node.constructor && node.constructor.name !== "Object"
158+
? terserTypeOf(node.constructor.name.replace(/^(?:AST_|Parsed)/, "").replace(/Node$/, ""))
159+
: terserTypeOf(kindName(node));
100160
if (kindOf(a) !== kindOf(b)) {
101-
return `${where}: ${a.constructor.name} vs ${b.constructor.name}`;
161+
return `${where}: ${kindOf(a)} vs ${kindOf(b)}`;
102162
}
103-
const renamed = (kindOf(b) !== "Token" && RENAMED_FIELDS.get(b.TYPE)) || {};
163+
const renamed = (kindOf(b) !== "Token" && RENAMED_FIELDS.get(kindName(b))) || {};
104164
/** @type {Record<string, string>} */
105165
const ourNames = {};
106166
for (const ourName of Object.keys(renamed)) {
@@ -116,22 +176,22 @@ const firstDifference = (theirs, ours) => {
116176
for (const key of keys) {
117177
// webpack holds a class's members in a class body, where terser's
118178
// class holds them as `properties` and its `body` holds nothing.
119-
const isClass = b.TYPE === "DefClass" || b.TYPE === "ClassExpression";
179+
const isClass = kindName(b) === "DefClass" || kindName(b) === "ClassExpression";
120180
if (isClass && key === "body" && a.body === undefined) continue;
121181
// webpack's directive holds terser's `value` as `directive`, its quote
122182
// on the string literal it holds as `expression`.
123-
const isDirective = b.TYPE === "Directive";
183+
const isDirective = kindName(b) === "Directive";
124184
if (isDirective && (key === "directive" || key === "expression")) {
125185
continue;
126186
}
127187
if (ESTREE_ADDED_FIELDS.has(key) && !(key in a)) continue;
128188
// webpack's destructuring holds terser's `names` as an array pattern's
129189
// `elements` or an object pattern's `properties`, the other null.
130-
const isPattern = terserTypeOf(b.TYPE) === "Destructuring";
190+
const isPattern = terserTypeOf(kindName(b)) === "Destructuring";
131191
if (isPattern && (key === "elements" || key === "properties")) continue;
132192
// webpack's specifier holds terser's `foreign_name` as what it imports
133193
// or what it exports, the other null.
134-
const isMapping = terserTypeOf(b.TYPE) === "NameMapping";
194+
const isMapping = terserTypeOf(kindName(b)) === "NameMapping";
135195
if (isMapping && (key === "imported" || key === "exported")) continue;
136196
let ours =
137197
isClass && key === "properties"
@@ -151,7 +211,7 @@ const firstDifference = (theirs, ours) => {
151211
if (
152212
Array.isArray(a[key]) &&
153213
ours &&
154-
ours.TYPE === "BlockStatement" &&
214+
kindName(ours) === "BlockStatement" &&
155215
Array.isArray(ours.body)
156216
) {
157217
ours = ours.body;
@@ -179,7 +239,10 @@ const text = (value) =>
179239
* @param {EXPECTED_ANY} ast one side's node classes
180240
* @returns {EXPECTED_ANY} its root class
181241
*/
182-
const rootOf = (ast) => ast.SyntaxNode || ast.AST_Node;
242+
const rootOf = (ast) =>
243+
ast.AST_Node || {
244+
from_mozilla_ast: /** @type {EXPECTED_FUNCTION} */ (ast.adoptEstree)
245+
};
183246

184247
/**
185248
* Reads back a BigInt `text` wrote as a string.
@@ -429,7 +492,7 @@ describe("syntax-printer's port of terser's ESTree conversion", () => {
429492
const results = [];
430493
for (const { ast } of [modules.theirs, modules.ours]) {
431494
try {
432-
results.push(text(rootOf(ast).from_mozilla_ast(JSON.parse(JSON.stringify(tree))).TYPE));
495+
results.push(text(kindName(rootOf(ast).from_mozilla_ast(JSON.parse(JSON.stringify(tree))))));
433496
} catch (err) {
434497
results.push(`throws ${thrownMessage(err)}`);
435498
}

0 commit comments

Comments
 (0)