Skip to content

Commit e038bb5

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
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`.
1 parent cb3b594 commit e038bb5

7 files changed

Lines changed: 745 additions & 59 deletions

File tree

‎lib/javascript/ScopeAnalyzer.js‎

Lines changed: 178 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,13 @@ 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;
11571165
const DECLARES_DEFUN = 8;
11581166
const DECLARES_LAMBDA = 9;
11591167
const DECLARES_CLASS = 10;
@@ -1690,6 +1698,7 @@ const createCompressorScope = (type, node, upper, isVarScope) =>
16901698
* @property {EXPECTED_ANY=} keep_classnames which class names survive, collecting the declarators naming one under `classDeclarators`
16911699
* @property {boolean=} classDeclarators whether, under `keep_classnames`, the names declarators give anonymous classes are kept as `keep_fnames` keeps a function's
16921700
* @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
1701+
* @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
16931702
*/
16941703

16951704
/**
@@ -1702,6 +1711,16 @@ const createCompressorScope = (type, node, upper, isVarScope) =>
17021711
* @property {PrintIdentifier[] | null} classDeclarators under `classDeclarators` and `keep_classnames`, the names declarators give anonymous classes
17031712
* @property {PrintIdentifier[]} catchParameters unless `ie8` or `safari10` move them, the catch parameters, each named where the mangler's walk meets its scope
17041713
* @property {boolean} namesPrivate whether the program names a private member, which renaming walks it for
1714+
* @property {SpelledNames | null} spelledNames under `spelledNames`, the names a key or an import or export spells for a name beside it
1715+
*/
1716+
1717+
/**
1718+
* Where a name stands beside one spelled for it: a property's key, which the
1719+
* name as its value lets print alone, or an import's or export's other name.
1720+
* @typedef {object} SpelledNames
1721+
* @property {PrintIdentifier[]} identifiers each name, its `definition` read once the analysis resolves
1722+
* @property {string[]} spellings the name spelled beside each
1723+
* @property {number[]} savings the characters each saves, its own name one long, where it takes the name spelled
17051724
*/
17061725

17071726
/**
@@ -1905,6 +1924,52 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
19051924
this.currentLabel = null;
19061925
/** @type {PrintNode | null} the pattern the walk is inside, which `_markExport` counts from */
19071926
this.inDestructuring = null;
1927+
/** @type {SpelledNames | null} */
1928+
this.spelledNames = options.spelledNames
1929+
? { identifiers: [], spellings: [], savings: [] }
1930+
: null;
1931+
/** @type {boolean} whether a key spells a name, which prints alone where it is its value's */
1932+
this.spelledKeys = options.spelledNames === SPELLED_KEYS;
1933+
}
1934+
1935+
/**
1936+
* Collects a name and one spelled beside it, under `spelledNames`.
1937+
* @param {PrintNode | null} identifier the name, if it is one
1938+
* @param {PrintNode | null} spelled what spells the other, if a name
1939+
* @param {number} saving the characters it saves taking that name
1940+
* @returns {void}
1941+
*/
1942+
_spell(identifier, spelled, saving) {
1943+
const names = this.spelledNames;
1944+
if (
1945+
names === null ||
1946+
identifier === null ||
1947+
spelled === null ||
1948+
identifier.type !== "Identifier" ||
1949+
spelled.type !== "Identifier" ||
1950+
// A shorthand `__proto__` is an own property; `__proto__:` sets the prototype.
1951+
spelled.name === "__proto__"
1952+
) {
1953+
return;
1954+
}
1955+
names.identifiers.push(/** @type {PrintIdentifier} */ (identifier));
1956+
names.spellings.push(spelled.name);
1957+
names.savings.push(saving);
1958+
}
1959+
1960+
/**
1961+
* Collects a property's value named as its key could be, under `spelledNames`.
1962+
* @param {PrintProperty} property a property of an object or a pattern
1963+
* @returns {void}
1964+
*/
1965+
_spellProperty(property) {
1966+
if (!this.spelledKeys || property.computed) return;
1967+
const { value } = property;
1968+
this._spell(
1969+
value.type === "AssignmentPattern" ? value.left : value,
1970+
property.key,
1971+
SHORTHAND_SAVING
1972+
);
19081973
}
19091974

19101975
/**
@@ -2123,6 +2188,7 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
21232188
stack.push(element);
21242189
if (element.computed) this._visitPrint(element.key);
21252190
this._visitBinding(element.value, kind);
2191+
this._spellProperty(element);
21262192
stack.pop();
21272193
}
21282194
stack.pop();
@@ -2678,6 +2744,13 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
26782744
stack.push(current);
26792745
if (current.computed) this._visitPrint(current.key);
26802746
if (current.value) this._visitPrint(current.value);
2747+
if (
2748+
current.type === "Property" &&
2749+
current.kind === "init" &&
2750+
!current.method
2751+
) {
2752+
this._spellProperty(current);
2753+
}
26812754
stack.pop();
26822755
return;
26832756
case "ObjectPattern":
@@ -2738,6 +2811,9 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
27382811
/** @type {PrintIdentifier} */ (specifier.local),
27392812
DECLARES_IMPORT
27402813
);
2814+
if (specifier.type === "ImportSpecifier") {
2815+
this._spell(specifier.local, specifier.imported, AS_SAVING);
2816+
}
27412817
stack.pop();
27422818
}
27432819
}
@@ -2762,6 +2838,9 @@ class MinifierScopeAnalyzer extends ScopeAnalyzer {
27622838
? REFERENCE_EXPORTED | REFERENCE_FROM_MODULE
27632839
: REFERENCE_EXPORTED
27642840
);
2841+
if (!fromModule) {
2842+
this._spell(specifier.local, specifier.exported, AS_SAVING);
2843+
}
27652844
stack.pop();
27662845
}
27672846
}
@@ -2924,7 +3003,8 @@ const analyzeMinifierScope = (program, options, raise, release) => {
29243003
functionDeclarators: analyzer.functionDeclarators,
29253004
classDeclarators: analyzer.classDeclarators,
29263005
catchParameters: analyzer.catchParameters,
2927-
namesPrivate: analyzer.namesPrivate
3006+
namesPrivate: analyzer.namesPrivate,
3007+
spelledNames: analyzer.spelledNames
29283008
};
29293009
};
29303010

@@ -3018,6 +3098,78 @@ class MinifierTreeScopeAnalyzer {
30183098
/** @type {TreeDefinition[]} */
30193099
// eslint-disable-next-line unicorn/no-new-array
30203100
this.definitions = new Array(types.length).fill(null);
3101+
/** @type {number[] | null} each name and the one spelled beside it, in pairs */
3102+
this.spelledPairs = options.spelledNames ? [] : null;
3103+
/** @type {number[]} the characters each pair saves */
3104+
this.spelledSavings = [];
3105+
/** @type {boolean} whether a key spells a name, which prints alone where it is its value's */
3106+
this.spelledKeys = options.spelledNames === SPELLED_KEYS;
3107+
}
3108+
3109+
/**
3110+
* Collects a name and one spelled beside it, under `spelledNames`.
3111+
* @param {number} identifier the name, 0 for none
3112+
* @param {number} spelled what spells the other, 0 for none
3113+
* @param {number} saving the characters it saves taking that name
3114+
* @returns {void}
3115+
*/
3116+
_spell(identifier, spelled, saving) {
3117+
const pairs = this.spelledPairs;
3118+
const { types } = this;
3119+
if (
3120+
pairs === null ||
3121+
identifier === 0 ||
3122+
spelled === 0 ||
3123+
types[identifier] !== NodeType.Identifier ||
3124+
types[spelled] !== NodeType.Identifier ||
3125+
// A shorthand `__proto__` is an own property; `__proto__:` sets the prototype.
3126+
this.tree.name(spelled) === "__proto__"
3127+
) {
3128+
return;
3129+
}
3130+
pairs.push(identifier, spelled);
3131+
this.spelledSavings.push(saving);
3132+
}
3133+
3134+
/**
3135+
* Collects a property's value named as its key could be, under `spelledNames`.
3136+
* @param {number} property a property of an object or a pattern
3137+
* @returns {void}
3138+
*/
3139+
_spellProperty(property) {
3140+
const { tree } = this;
3141+
if (!this.spelledKeys || tree.flag(Flag.computed, property)) {
3142+
return;
3143+
}
3144+
const value = tree.field(0, Field.value, property);
3145+
this._spell(
3146+
this.types[value] === NodeType.AssignmentPattern
3147+
? tree.field(0, Field.left, value)
3148+
: value,
3149+
tree.field(0, Field.key, property),
3150+
SHORTHAND_SAVING
3151+
);
3152+
}
3153+
3154+
/**
3155+
* @returns {SpelledNames | null} the names collected under `spelledNames`, resolved
3156+
*/
3157+
_spelledNames() {
3158+
const pairs = this.spelledPairs;
3159+
if (pairs === null) return null;
3160+
/** @type {number[]} */
3161+
const identifiers = [];
3162+
/** @type {string[]} */
3163+
const spellings = [];
3164+
for (let i = 0; i < pairs.length; i += 2) {
3165+
identifiers.push(pairs[i]);
3166+
spellings.push(this.tree.name(pairs[i + 1]));
3167+
}
3168+
return {
3169+
identifiers: this._asNames(identifiers),
3170+
spellings,
3171+
savings: this.spelledSavings
3172+
};
30213173
}
30223174

30233175
/**
@@ -3258,6 +3410,7 @@ class MinifierTreeScopeAnalyzer {
32583410
this._visitChild(Field.key, element);
32593411
}
32603412
this._visitBinding(this._child(Field.value, element), kind);
3413+
this._spellProperty(element);
32613414
stack.pop();
32623415
}
32633416
stack.pop();
@@ -3854,6 +4007,13 @@ class MinifierTreeScopeAnalyzer {
38544007
stack.push(node);
38554008
if (tree.flag(Flag.computed, node)) this._visitChild(Field.key, node);
38564009
this._visitChild(Field.value, node);
4010+
if (
4011+
this.types[node] === NodeType.Property &&
4012+
tree.textOf(Part.kind, node) === "init" &&
4013+
!tree.flag(Flag.method, node)
4014+
) {
4015+
this._spellProperty(node);
4016+
}
38574017
stack.pop();
38584018
return;
38594019
case NodeType.ObjectPattern:
@@ -3950,6 +4110,13 @@ class MinifierTreeScopeAnalyzer {
39504110
}
39514111
stack.push(specifier);
39524112
this._declare(local, DECLARES_IMPORT);
4113+
if (this.types[specifier] === NodeType.ImportSpecifier) {
4114+
this._spell(
4115+
local,
4116+
tree.field(0, Field.imported, specifier),
4117+
AS_SAVING
4118+
);
4119+
}
39534120
stack.pop();
39544121
}
39554122
stack.pop();
@@ -3967,7 +4134,15 @@ class MinifierTreeScopeAnalyzer {
39674134
for (let i = 0; i < count; i++) {
39684135
const specifier = tree.field(i, Field.specifiers, node);
39694136
stack.push(specifier);
3970-
this._addReference(tree.field(0, Field.local, specifier), flags);
4137+
const local = tree.field(0, Field.local, specifier);
4138+
this._addReference(local, flags);
4139+
if ((flags & REFERENCE_FROM_MODULE) === 0) {
4140+
this._spell(
4141+
local,
4142+
tree.field(0, Field.exported, specifier),
4143+
AS_SAVING
4144+
);
4145+
}
39714146
stack.pop();
39724147
}
39734148
stack.pop();
@@ -4109,6 +4284,7 @@ const analyzeMinifierTree = (tree, program, types, options, raise) => {
41094284
classDeclarators === null ? null : analyzer._asNames(classDeclarators),
41104285
catchParameters: analyzer._asNames(analyzer.catchParameters),
41114286
namesPrivate: analyzer.namesPrivate,
4287+
spelledNames: analyzer._spelledNames(),
41124288
definitions: analyzer.definitions
41134289
};
41144290
};

0 commit comments

Comments
 (0)