@@ -21685,7 +21685,20 @@ const installImprove = (modules) => {
2168521685 }
2168621686 };
2168721687
21688- // `function(a){…}(a)` flattens where `a` is taken, its body reading the `a`
21688+ /**
21689+ * Whether a function declares a name anywhere in it, its parameters included.
21690+ * @param {Node} lambda the function
21691+ * @param {string} name the name
21692+ * @returns {boolean} true when it does
21693+ */
21694+ const declaresName = (lambda, name) =>
21695+ A.walk(lambda, (/** @type {Node} */ node) => {
21696+ if (A.isSymbolDeclarationNode(node) && node.name === name) {
21697+ return A.walk_abort;
21698+ }
21699+ });
21700+
21701+ // `function(a){…}(b)` flattens where `a` is taken, its body reading the `b`
2168921702 // around it, which nothing writes, so it holds the argument's value.
2169021703 inlineHooks.aliasesParameter = (call, lambda, index, compressor) => {
2169121704 if (!improvements.enabled) return false;
@@ -21704,7 +21717,8 @@ const installImprove = (modules) => {
2170421717 ) ||
2170521718 !A.isSymbolFunargNode(parameter) ||
2170621719 !A.isSymbolRefNode(value) ||
21707- value.name !== parameter.name
21720+ // Read under the argument's name, the body must declare none like it.
21721+ (value.name !== parameter.name && declaresName(lambda, value.name))
2170821722 ) {
2170921723 return false;
2171021724 }
@@ -22871,133 +22885,6 @@ const installImprove = (modules) => {
2287122885 }
2287222886 );
2287322887
22874- /** @type {WeakMap<Node, boolean>} */
22875- const argumentsReadOnly = new WeakMap();
22876-
22877- /**
22878- * Whether a function only reads its `arguments` object's `length` and
22879- * indices, so an index at or past `length` reads `undefined`.
22880- * @param {Node} lambda a function
22881- * @returns {boolean} true when nothing writes or passes the object on
22882- */
22883- const readsArgumentsOnly = (lambda) => {
22884- const known = argumentsReadOnly.get(lambda);
22885- if (known !== undefined) return known;
22886- let readOnly = true;
22887- const walker = A.createWalker((/** @type {Node} */ node) => {
22888- if (!readOnly) return true;
22889- if (!A.isSymbolRefNode(node) || node.name !== "arguments") return;
22890- const access = walker.parent();
22891- const outer = walker.parent(1);
22892- if (
22893- !(
22894- (A.isDotNode(access) && access.property.name === "length") ||
22895- (A.isSubNode(access) && A.isNumberNode(access.property))
22896- ) ||
22897- access.object !== node ||
22898- (A.isAssignNode(outer) && outer.left === access) ||
22899- (A.isUnaryNode(outer) &&
22900- (outer.operator === "++" ||
22901- outer.operator === "--" ||
22902- outer.operator === "delete"))
22903- ) {
22904- readOnly = false;
22905- }
22906- });
22907- walkNode(lambda, walker);
22908- argumentsReadOnly.set(lambda, readOnly);
22909- return readOnly;
22910- };
22911-
22912- /**
22913- * The index `arguments.length > n` tests for, where the function's own
22914- * `arguments` object is only ever read.
22915- * @param {Node} node a test
22916- * @returns {number | undefined} the index n, or undefined
22917- */
22918- const testedArgumentIndex = (node) => {
22919- if (!A.isBinaryNode(node) || A.isAssignNode(node)) return;
22920- let length;
22921- let count;
22922- if (node.operator === ">") [length, count] = [node.left, node.right];
22923- else if (node.operator === "<") [count, length] = [node.left, node.right];
22924- else return;
22925- if (
22926- !A.isDotNode(length) ||
22927- length.property.name !== "length" ||
22928- !A.isArgumentsObject(length.object) ||
22929- !A.isNumberNode(count) ||
22930- !readsArgumentsOnly(length.object.definition.scope.node)
22931- ) {
22932- return;
22933- }
22934- return count.value;
22935- };
22936-
22937- /**
22938- * @param {Node} node an expression
22939- * @returns {boolean} whether it is `undefined` or `void` of a constant
22940- */
22941- const isUndefinedValue = (node) =>
22942- A.isUndefinedNode(node) ||
22943- (A.isUnaryPrefixNode(node) &&
22944- node.operator === "void" &&
22945- A.isConstantNode(node.argument));
22946-
22947- /**
22948- * Whether a node reads `arguments[n]` off the same object a test read.
22949- * @param {Node} node an expression
22950- * @param {number} index the index n
22951- * @returns {boolean} true for that read
22952- */
22953- const readsArgument = (node, index) =>
22954- A.isSubNode(node) &&
22955- A.isArgumentsObject(node.object) &&
22956- A.isNumberNode(node.property) &&
22957- node.property.value === index;
22958-
22959- // Babel's default parameters test `arguments.length > n` before reading
22960- // `arguments[n]`, which reads `undefined` past the length anyway.
22961- A.wrapOptimizer("Binary", (optimizeBinary) => (self, compressor) => {
22962- const optimized = optimizeBinary(self, compressor);
22963- if (
22964- !improvements.enabled ||
22965- !A.isBinaryNode(optimized) ||
22966- optimized.operator !== "&&"
22967- ) {
22968- return optimized;
22969- }
22970- const index = testedArgumentIndex(optimized.left);
22971- const { right } = optimized;
22972- if (
22973- index === undefined ||
22974- !A.isBinaryNode(right) ||
22975- right.operator !== "!==" ||
22976- !(
22977- (isUndefinedValue(right.left) && readsArgument(right.right, index)) ||
22978- (isUndefinedValue(right.right) && readsArgument(right.left, index))
22979- )
22980- ) {
22981- return optimized;
22982- }
22983- return right;
22984- });
22985- A.wrapOptimizer(
22986- "Conditional",
22987- (optimizeConditional) => (self, compressor) => {
22988- const optimized = optimizeConditional(self, compressor);
22989- if (!improvements.enabled || !A.isConditionalNode(optimized)) {
22990- return optimized;
22991- }
22992- const index = testedArgumentIndex(optimized.test);
22993- return index !== undefined &&
22994- readsArgument(optimized.consequent, index) &&
22995- isUndefinedValue(optimized.alternate)
22996- ? optimized.consequent
22997- : optimized;
22998- }
22999- );
23000-
2300122888 /**
2300222889 * Whether the value of the node visited goes on as it is, where no test or
2300322890 * operator around it could fold a constant written in its place.
@@ -23063,7 +22950,10 @@ const installImprove = (modules) => {
2306322950 A.nodeSize(written, compressor) - A.nodeSize(call, compressor);
2306422951 // The function stays for the calls kept, so two of them must repay it: a
2306522952 // call read by a test or an operator still inlines, and may be the rest.
23066- return compressor.option("unused") && !compressor.exposed(definition)
22953+ // Passed on as a value, it stays whatever its calls become.
22954+ return compressor.option("unused") &&
22955+ !compressor.exposed(definition) &&
22956+ !definition.escaped
2306722957 ? Math.min(definition.references.length, 2) * saved >
2306822958 A.nodeSize(fn, compressor)
2306922959 : saved > 0;
@@ -25591,6 +25481,11 @@ const installImprove = (modules) => {
2559125481 for (let i = 0; i < body.length; i++) {
2559225482 const effects = splitEffectsOf(body[i], walker);
2559325483 if (effects === undefined) continue;
25484+ // A function's own body ending in what only declares falls off there.
25485+ if (A.isLambdaNode(node) && body.slice(i + 1).every(onlyDeclares)) {
25486+ body[i] = effects;
25487+ continue;
25488+ }
2559425489 body.splice(
2559525490 i,
2559625491 1,
@@ -25601,6 +25496,19 @@ const installImprove = (modules) => {
2560125496 }
2560225497 };
2560325498
25499+ /**
25500+ * @param {Node} statement a statement
25501+ * @returns {boolean} whether running it does nothing: a function declaration or `var` without values
25502+ */
25503+ const onlyDeclares = (statement) =>
25504+ A.isDefunNode(statement) ||
25505+ A.isEmptyStatementNode(statement) ||
25506+ (A.isVarNode(statement) &&
25507+ statement.declarations.every(
25508+ (/** @type {Node} */ declaration) =>
25509+ !declaration.init && A.isSymbolDeclarationNode(declaration.id)
25510+ ));
25511+
2560425512 // The unary operators that evaluate their operand before anything else.
2560525513 const OPERAND_FIRST_UNARY = new Set(["!", "-", "+", "~", "void", "typeof"]);
2560625514
@@ -33210,6 +33118,13 @@ const createInline = (modules) => {
3321033118 if (variable === undefined) return false;
3321133119 (state.reuses || (state.reuses = new Map())).set(arg, variable);
3321233120 }
33121+ } else if (
33122+ state.self.callee === lambda &&
33123+ inlineHooks.aliasesParameter !== null &&
33124+ inlineHooks.aliasesParameter(state.self, lambda, i, state.compressor)
33125+ ) {
33126+ // Read in place, the argument needs no `var` holding a copy.
33127+ (state.aliases || (state.aliases = [])).push(arg);
3321333128 }
3321433129 if (inLoop) inLoop.push(arg.definition);
3321533130 }
@@ -33391,6 +33306,7 @@ const createInline = (modules) => {
3339133306 walk(lambda.body, (/** @type {Node} */ node) => {
3339233307 if (isSymbolRefNode(node) && node.definition === parameter) {
3339333308 node.definition = variable;
33309+ node.name = variable.name;
3339433310 variable.references.push(node);
3339533311 }
3339633312 });
@@ -46119,10 +46035,18 @@ const installEstreeMangle = (modules) => {
4611946035 let givenIn = null;
4612046036 /** @type {Set<string>} */
4612146037 const spelled = new Set();
46038+ // The names a scope's definitions want spelled, which counting skips there.
46039+ /** @type {Map<MinifierScope, Set<string>> | null} */
46040+ let wantedIn = null;
4612246041 if (wanted.size !== 0) {
4612346042 givenIn = new Map();
46124- for (const definition of wanted.keys()) {
46125- givenIn.set(countingScopeOf(definition, blockDefunScopes), new Set());
46043+ wantedIn = new Map();
46044+ for (const [definition, spelling] of wanted) {
46045+ const counting = countingScopeOf(definition, blockDefunScopes);
46046+ givenIn.set(counting, new Set());
46047+ const names = wantedIn.get(counting);
46048+ if (names === undefined) wantedIn.set(counting, new Set([spelling]));
46049+ else names.add(spelling);
4612646050 }
4612746051 }
4612846052 for (let i = 0; i < toMangle.length; i++) {
@@ -46151,6 +46075,7 @@ const installEstreeMangle = (modules) => {
4615146075 const counting = countingScopeOf(definition, blockDefunScopes);
4615246076 const taken = takenIn(counting);
4615346077 const given = givenIn === null ? undefined : givenIn.get(counting);
46078+ const wantedHere = wantedIn === null ? undefined : wantedIn.get(counting);
4615446079 if (given !== undefined) {
4615546080 const spelling = wanted.get(definition);
4615646081 if (
@@ -46178,6 +46103,7 @@ const installEstreeMangle = (modules) => {
4617846103 if (taken.has(name)) continue;
4617946104 if (shadowed !== null && shadowed === name) continue;
4618046105 if (given !== undefined && given.has(name)) continue;
46106+ if (wantedHere !== undefined && wantedHere.has(name)) continue;
4618146107 break;
4618246108 }
4618346109 if (given !== undefined) given.add(name);
0 commit comments