Skip to content

Commit e538a18

Browse files
feat(javascript): name bindings by the keys and module names they spell, drop dead class parts and write exact fractions in the minifier (#22679)
* fix(javascript): keep the callers of an `__INLINE__` function reading their own parameters * feat(javascript): name bindings by the keys and module names they spell, drop dead class parts and write exact fractions in the minifier Keep a destructured, imported or exported name's own spelling where it saves the `:` or ` as ` clause; drop an unused class run only for what it extends, a derived constructor only forwarding to `super` and private fields nothing reads; fold `[...].slice()`, merge spread calls across a conditional; print `2/3` for `.6666666666666666`. * fix(javascript): spell a binding only by a key or module name that is an identifier, quoted or not * fix(javascript): never spell a binding by a name strict code cannot bind * fix(javascript): keep a derived class's unread instance private fields Adding one throws when `super()` returns an object already holding it, which test262's privatefieldadd-typeerror reads. * fix(javascript): merge spread calls only where no spread precedes the one they differ in Its iteration ran after the conditional's test, and in the merged call runs before it, where an iterator may change what the test reads.
1 parent dbb4605 commit e538a18

7 files changed

Lines changed: 853 additions & 60 deletions

File tree

‎lib/javascript/ScopeAnalyzer.js‎

Lines changed: 220 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,6 +1127,7 @@ const analyzeScope = (ast, recordEveryReference = false) => {
11271127
* @typedef {import("./syntax-printer").PrintLiteral} PrintLiteral
11281128
* @typedef {import("./syntax-printer").PrintFunction} PrintFunction
11291129
* @typedef {import("./syntax-printer").PrintClass} PrintClass
1130+
* @typedef {import("./syntax-printer").PrintProperty} PrintProperty
11301131
* @typedef {import("./syntax-printer").PrintObject} PrintObject
11311132
* @typedef {import("./syntax-printer").PrintArray} PrintArray
11321133
* @typedef {import("./syntax-printer").PrintCallExpression} PrintCallExpression
@@ -1154,6 +1155,32 @@ const DECLARES_CONST = 4;
11541155
const DECLARES_USING = 5;
11551156
const DECLARES_CATCH = 6;
11561157
const DECLARES_IMPORT = 7;
1158+
1159+
// What a name one long saves taking the name spelled beside it: a key's `:`
1160+
// and the name, or an import's or export's ` as ` and the name.
1161+
const SHORTHAND_SAVING = 2;
1162+
const AS_SAVING = 5;
1163+
// `spelledNames` collecting the names a key spells too, not only a module's.
1164+
const SPELLED_KEYS = 2;
1165+
// A name the mangler may hand out as it is spelled: one ASCII identifier, so it
1166+
// prints bare and costs a byte a character.
1167+
const SPELLABLE_NAME = /^[A-Za-z_$][\w$]*$/;
1168+
// Names no binding may take in strict code, a module's, beyond the keywords.
1169+
const NAMES_NO_BINDING_TAKES = new Set([
1170+
"arguments",
1171+
"await",
1172+
"enum",
1173+
"eval",
1174+
"implements",
1175+
"interface",
1176+
"let",
1177+
"package",
1178+
"private",
1179+
"protected",
1180+
"public",
1181+
"static",
1182+
"yield"
1183+
]);
11571184
const DECLARES_DEFUN = 8;
11581185
const DECLARES_LAMBDA = 9;
11591186
const DECLARES_CLASS = 10;
@@ -1690,6 +1717,7 @@ const createCompressorScope = (type, node, upper, isVarScope) =>
16901717
* @property {EXPECTED_ANY=} keep_classnames which class names survive, collecting the declarators naming one under `classDeclarators`
16911718
* @property {boolean=} classDeclarators whether, under `keep_classnames`, the names declarators give anonymous classes are kept as `keep_fnames` keeps a function's
16921719
* @property {boolean=} parameterScopes whether names are scoped as the spec scopes them where terser does not: a parameter list's or a catch parameter's reads skip what the body declares, and a block's async or generator function stays in the block
1720+
* @property {number=} spelledNames which names to collect that the mangler may give the name beside them to write less: 1 for an import's or export's, 2 for a key's too
16931721
*/
16941722

16951723
/**
@@ -1702,6 +1730,16 @@ const createCompressorScope = (type, node, upper, isVarScope) =>
17021730
* @property {PrintIdentifier[] | null} classDeclarators under `classDeclarators` and `keep_classnames`, the names declarators give anonymous classes
17031731
* @property {PrintIdentifier[]} catchParameters unless `ie8` or `safari10` move them, the catch parameters, each named where the mangler's walk meets its scope
17041732
* @property {boolean} namesPrivate whether the program names a private member, which renaming walks it for
1733+
* @property {SpelledNames | null} spelledNames under `spelledNames`, the names a key or an import or export spells for a name beside it
1734+
*/
1735+
1736+
/**
1737+
* Where a name stands beside one spelled for it: a property's key, which the
1738+
* name as its value lets print alone, or an import's or export's other name.
1739+
* @typedef {object} SpelledNames
1740+
* @property {PrintIdentifier[]} identifiers each name, its `definition` read once the analysis resolves
1741+
* @property {string[]} spellings the name spelled beside each
1742+
* @property {number[]} savings the characters each saves, its own name one long, where it takes the name spelled
17051743
*/
17061744

17071745
/**
@@ -1905,6 +1943,67 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
19051943
this.currentLabel = null;
19061944
/** @type {PrintNode | null} the pattern the walk is inside, which `_markExport` counts from */
19071945
this.inDestructuring = null;
1946+
/** @type {SpelledNames | null} */
1947+
this.spelledNames = options.spelledNames
1948+
? { identifiers: [], spellings: [], savings: [] }
1949+
: null;
1950+
/** @type {boolean} whether a key spells a name, which prints alone where it is its value's */
1951+
this.spelledKeys = options.spelledNames === SPELLED_KEYS;
1952+
}
1953+
1954+
/**
1955+
* Collects a name and one spelled beside it, under `spelledNames`.
1956+
* @param {PrintNode | null} identifier the name, if it is one
1957+
* @param {PrintNode | null} spelled what spells the other, if a name
1958+
* @param {number} saving the characters it saves taking that name
1959+
* @returns {void}
1960+
*/
1961+
_spell(identifier, spelled, saving) {
1962+
const names = this.spelledNames;
1963+
if (
1964+
names === null ||
1965+
identifier === null ||
1966+
spelled === null ||
1967+
identifier.type !== "Identifier"
1968+
) {
1969+
return;
1970+
}
1971+
const spelling =
1972+
spelled.type === "Identifier"
1973+
? spelled.name
1974+
: spelled.type !== "Literal"
1975+
? undefined
1976+
: typeof spelled.value === "string"
1977+
? spelled.value
1978+
: // A module's quoted name holds its text as its name.
1979+
/** @type {{ name?: string }} */ (spelled).name;
1980+
if (
1981+
spelling === undefined ||
1982+
!SPELLABLE_NAME.test(spelling) ||
1983+
NAMES_NO_BINDING_TAKES.has(spelling) ||
1984+
// A shorthand `__proto__` is an own property; `__proto__:` sets the prototype.
1985+
spelling === "__proto__"
1986+
) {
1987+
return;
1988+
}
1989+
names.identifiers.push(/** @type {PrintIdentifier} */ (identifier));
1990+
names.spellings.push(spelling);
1991+
names.savings.push(saving);
1992+
}
1993+
1994+
/**
1995+
* Collects a property's value named as its key could be, under `spelledNames`.
1996+
* @param {PrintProperty} property a property of an object or a pattern
1997+
* @returns {void}
1998+
*/
1999+
_spellProperty(property) {
2000+
if (!this.spelledKeys || property.computed) return;
2001+
const { value } = property;
2002+
this._spell(
2003+
value.type === "AssignmentPattern" ? value.left : value,
2004+
property.key,
2005+
SHORTHAND_SAVING
2006+
);
19082007
}
19092008

19102009
/**
@@ -2123,6 +2222,7 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
21232222
stack.push(element);
21242223
if (element.computed) this._visitPrint(element.key);
21252224
this._visitBinding(element.value, kind);
2225+
this._spellProperty(element);
21262226
stack.pop();
21272227
}
21282228
stack.pop();
@@ -2678,6 +2778,13 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
26782778
stack.push(current);
26792779
if (current.computed) this._visitPrint(current.key);
26802780
if (current.value) this._visitPrint(current.value);
2781+
if (
2782+
current.type === "Property" &&
2783+
current.kind === "init" &&
2784+
!current.method
2785+
) {
2786+
this._spellProperty(current);
2787+
}
26812788
stack.pop();
26822789
return;
26832790
case "ObjectPattern":
@@ -2738,6 +2845,9 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
27382845
/** @type {PrintIdentifier} */ (specifier.local),
27392846
DECLARES_IMPORT
27402847
);
2848+
if (specifier.type === "ImportSpecifier") {
2849+
this._spell(specifier.local, specifier.imported, AS_SAVING);
2850+
}
27412851
stack.pop();
27422852
}
27432853
}
@@ -2762,6 +2872,9 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
27622872
? REFERENCE_EXPORTED | REFERENCE_FROM_MODULE
27632873
: REFERENCE_EXPORTED
27642874
);
2875+
if (!fromModule) {
2876+
this._spell(specifier.local, specifier.exported, AS_SAVING);
2877+
}
27652878
stack.pop();
27662879
}
27672880
}
@@ -2924,7 +3037,8 @@ const analyzeMinifierScope = (program, options, raise, release) => {
29243037
functionDeclarators: analyzer.functionDeclarators,
29253038
classDeclarators: analyzer.classDeclarators,
29263039
catchParameters: analyzer.catchParameters,
2927-
namesPrivate: analyzer.namesPrivate
3040+
namesPrivate: analyzer.namesPrivate,
3041+
spelledNames: analyzer.spelledNames
29283042
};
29293043
};
29303044

@@ -3018,6 +3132,86 @@ class MinifierTreeScopeAnalyzer {
30183132
/** @type {TreeDefinition[]} */
30193133
// eslint-disable-next-line unicorn/no-new-array
30203134
this.definitions = new Array(types.length).fill(null);
3135+
/** @type {number[] | null} each name with one spelled beside it */
3136+
this.spelledIdentifiers = options.spelledNames ? [] : null;
3137+
/** @type {string[]} the name spelled beside each */
3138+
this.spellings = [];
3139+
/** @type {number[]} the characters each pair saves */
3140+
this.spelledSavings = [];
3141+
/** @type {boolean} whether a key spells a name, which prints alone where it is its value's */
3142+
this.spelledKeys = options.spelledNames === SPELLED_KEYS;
3143+
}
3144+
3145+
/**
3146+
* Collects a name and one spelled beside it, under `spelledNames`.
3147+
* @param {number} identifier the name, 0 for none
3148+
* @param {number} spelled what spells the other, 0 for none
3149+
* @param {number} saving the characters it saves taking that name
3150+
* @returns {void}
3151+
*/
3152+
_spell(identifier, spelled, saving) {
3153+
const identifiers = this.spelledIdentifiers;
3154+
const { types, tree } = this;
3155+
if (
3156+
identifiers === null ||
3157+
identifier === 0 ||
3158+
spelled === 0 ||
3159+
types[identifier] !== NodeType.Identifier
3160+
) {
3161+
return;
3162+
}
3163+
/** @type {unknown} */
3164+
let spelling;
3165+
if (types[spelled] === NodeType.Identifier) {
3166+
spelling = tree.name(spelled);
3167+
} else if (types[spelled] === NodeType.Literal) {
3168+
spelling = tree.value(spelled);
3169+
}
3170+
if (
3171+
typeof spelling !== "string" ||
3172+
!SPELLABLE_NAME.test(spelling) ||
3173+
NAMES_NO_BINDING_TAKES.has(spelling) ||
3174+
// A shorthand `__proto__` is an own property; `__proto__:` sets the prototype.
3175+
spelling === "__proto__"
3176+
) {
3177+
return;
3178+
}
3179+
identifiers.push(identifier);
3180+
this.spellings.push(spelling);
3181+
this.spelledSavings.push(saving);
3182+
}
3183+
3184+
/**
3185+
* Collects a property's value named as its key could be, under `spelledNames`.
3186+
* @param {number} property a property of an object or a pattern
3187+
* @returns {void}
3188+
*/
3189+
_spellProperty(property) {
3190+
const { tree } = this;
3191+
if (!this.spelledKeys || tree.flag(Flag.computed, property)) {
3192+
return;
3193+
}
3194+
const value = tree.field(0, Field.value, property);
3195+
this._spell(
3196+
this.types[value] === NodeType.AssignmentPattern
3197+
? tree.field(0, Field.left, value)
3198+
: value,
3199+
tree.field(0, Field.key, property),
3200+
SHORTHAND_SAVING
3201+
);
3202+
}
3203+
3204+
/**
3205+
* @returns {SpelledNames | null} the names collected under `spelledNames`, resolved
3206+
*/
3207+
_spelledNames() {
3208+
const identifiers = this.spelledIdentifiers;
3209+
if (identifiers === null) return null;
3210+
return {
3211+
identifiers: this._asNames(identifiers),
3212+
spellings: this.spellings,
3213+
savings: this.spelledSavings
3214+
};
30213215
}
30223216

30233217
/**
@@ -3258,6 +3452,7 @@ class MinifierTreeScopeAnalyzer {
32583452
this._visitChild(Field.key, element);
32593453
}
32603454
this._visitBinding(this._child(Field.value, element), kind);
3455+
this._spellProperty(element);
32613456
stack.pop();
32623457
}
32633458
stack.pop();
@@ -3854,6 +4049,13 @@ class MinifierTreeScopeAnalyzer {
38544049
stack.push(node);
38554050
if (tree.flag(Flag.computed, node)) this._visitChild(Field.key, node);
38564051
this._visitChild(Field.value, node);
4052+
if (
4053+
this.types[node] === NodeType.Property &&
4054+
tree.textOf(Part.kind, node) === "init" &&
4055+
!tree.flag(Flag.method, node)
4056+
) {
4057+
this._spellProperty(node);
4058+
}
38574059
stack.pop();
38584060
return;
38594061
case NodeType.ObjectPattern:
@@ -3950,6 +4152,13 @@ class MinifierTreeScopeAnalyzer {
39504152
}
39514153
stack.push(specifier);
39524154
this._declare(local, DECLARES_IMPORT);
4155+
if (this.types[specifier] === NodeType.ImportSpecifier) {
4156+
this._spell(
4157+
local,
4158+
tree.field(0, Field.imported, specifier),
4159+
AS_SAVING
4160+
);
4161+
}
39534162
stack.pop();
39544163
}
39554164
stack.pop();
@@ -3967,7 +4176,15 @@ class MinifierTreeScopeAnalyzer {
39674176
for (let i = 0; i < count; i++) {
39684177
const specifier = tree.field(i, Field.specifiers, node);
39694178
stack.push(specifier);
3970-
this._addReference(tree.field(0, Field.local, specifier), flags);
4179+
const local = tree.field(0, Field.local, specifier);
4180+
this._addReference(local, flags);
4181+
if ((flags & REFERENCE_FROM_MODULE) === 0) {
4182+
this._spell(
4183+
local,
4184+
tree.field(0, Field.exported, specifier),
4185+
AS_SAVING
4186+
);
4187+
}
39714188
stack.pop();
39724189
}
39734190
stack.pop();
@@ -4109,6 +4326,7 @@ const analyzeMinifierTree = (tree, program, types, options, raise) => {
41094326
classDeclarators === null ? null : analyzer._asNames(classDeclarators),
41104327
catchParameters: analyzer._asNames(analyzer.catchParameters),
41114328
namesPrivate: analyzer.namesPrivate,
4329+
spelledNames: analyzer._spelledNames(),
41124330
definitions: analyzer.definitions
41134331
};
41144332
};

0 commit comments

Comments
 (0)