@@ -5516,6 +5516,35 @@ const installUnused = (modules) => {
55165516 definition.kind === DECLARES_CATCH ||
55175517 (definition.kind === 0 && isSymbolCatchNode(definition.identifiers[0]));
55185518
5519+ /**
5520+ * The binding around a catch clause that a read of its parameter keeps, as
5521+ * terser keeps it; improved, only where the clause declares that name again.
5522+ * @param {Definition} definition the catch parameter
5523+ * @returns {Definition | false | undefined} the binding kept, if any
5524+ */
5525+ const redefinedByCatch = (definition) => {
5526+ const redefined =
5527+ definition.scope.isBlockScope() &&
5528+ definition.scope.getDefunScope().getBinding(definition.name);
5529+ if (
5530+ !redefined ||
5531+ modules.improvements === undefined ||
5532+ !modules.improvements.enabled
5533+ ) {
5534+ return redefined;
5535+ }
5536+ const { identifiers } = definition;
5537+ return walk(definition.scope.node, (/** @type {Node} */ node) =>
5538+ isSymbolDeclarationNode(node) &&
5539+ node.name === definition.name &&
5540+ !identifiers.includes(node)
5541+ ? ast.walk_abort
5542+ : undefined
5543+ )
5544+ ? redefined
5545+ : undefined;
5546+ };
5547+
55195548 // A recording: one walk of a function nested in the scope being dropped
55205549 // from, writing its scan as events into these scratch arrays, copied to
55215550 // their size at the end for the logs of the functions walked to share.
@@ -5912,9 +5941,7 @@ const installUnused = (modules) => {
59125941 if (!isInUse(definition)) {
59135942 markInUse(definition);
59145943 if (isCatchDefinition(definition)) {
5915- const redefined =
5916- definition.scope.isBlockScope() &&
5917- definition.scope.getDefunScope().getBinding(definition.name);
5944+ const redefined = redefinedByCatch(definition);
59185945 if (redefined) markInUse(redefined);
59195946 }
59205947 }
@@ -6638,11 +6665,7 @@ const installUnused = (modules) => {
66386665 if (!isInUse(nodeDefinition)) {
66396666 markInUse(nodeDefinition);
66406667 if (isCatchDefinition(nodeDefinition)) {
6641- const redefined =
6642- nodeDefinition.scope.isBlockScope() &&
6643- nodeDefinition.scope
6644- .getDefunScope()
6645- .getBinding(nodeDefinition.name);
6668+ const redefined = redefinedByCatch(nodeDefinition);
66466669 if (redefined) markInUse(redefined);
66476670 }
66486671 }
@@ -22523,6 +22546,43 @@ const installImprove = (modules) => {
2252322546 return undeletable ? makeNode(A.FalseNode, optimized) : optimized;
2252422547 });
2252522548
22549+ /**
22550+ * Whether a node reads a function declared in its scope's own body, which
22551+ * nothing writes: hoisted, it holds the function wherever it is read.
22552+ * @param {Node} node a node
22553+ * @param {Compressor} compressor the compressor
22554+ * @returns {boolean} true when it does
22555+ */
22556+ const readsDeclaredFunction = (node, compressor) => {
22557+ const definition = A.isSymbolRefNode(node) ? node.definition : undefined;
22558+ if (
22559+ !definition ||
22560+ definition.undeclared ||
22561+ definition.identifiers.length !== 1 ||
22562+ !A.isSymbolDefunNode(definition.identifiers[0]) ||
22563+ definition.assignments !== 0 ||
22564+ definition.patternWrites !== 0 ||
22565+ definition.scope.isPinned() ||
22566+ compressor.exposed(definition)
22567+ ) {
22568+ return false;
22569+ }
22570+ // One in a block is the sloppy-mode `var` only once the block runs.
22571+ const scope = definition.scope.node;
22572+ const body = A.bodyOf(scope);
22573+ return (
22574+ (A.isLambdaNode(scope) || A.isToplevelNode(scope)) &&
22575+ Array.isArray(body) &&
22576+ body.some(
22577+ (/** @type {Node} */ statement) =>
22578+ A.isDefunNode(statement) &&
22579+ statement.id &&
22580+ statement.id.definition === definition
22581+ ) &&
22582+ !writesVariable(definition.scope.node, definition)
22583+ );
22584+ };
22585+
2252622586 // `typeof (a === b)` as `"boolean"`: the type of what nothing else observes.
2252722587 A.wrapOptimizer("UnaryPrefix", (optimizeUnary) => (self, compressor) => {
2252822588 const optimized = optimizeUnary(self, compressor);
@@ -22536,15 +22596,16 @@ const installImprove = (modules) => {
2253622596 return optimized;
2253722597 }
2253822598 const { argument } = optimized;
22539- const type = A.isClassNode(argument)
22540- ? "function"
22541- : A.isBoolean(argument)
22542- ? "boolean"
22543- : A.isNumber(argument, compressor)
22544- ? "number"
22545- : A.isString(argument, compressor)
22546- ? "string"
22547- : undefined;
22599+ const type =
22600+ A.isClassNode(argument) || readsDeclaredFunction(argument, compressor)
22601+ ? "function"
22602+ : A.isBoolean(argument)
22603+ ? "boolean"
22604+ : A.isNumber(argument, compressor)
22605+ ? "number"
22606+ : A.isString(argument, compressor)
22607+ ? "string"
22608+ : undefined;
2254822609 return type === undefined
2254922610 ? optimized
2255022611 : makeNode(A.StringNode, optimized, { value: type });
@@ -24843,9 +24904,12 @@ const installImprove = (modules) => {
2484324904 args.length > read &&
2484424905 !args.some((/** @type {Node} */ arg) => A.isExpansionNode(arg))
2484524906 ) {
24846- let end = args.length;
24847- while (end > read && !A.hasSideEffects(args[end - 1], compressor)) end--;
24848- args.length = end;
24907+ // An argument past them only runs, so one without effects goes.
24908+ let kept = read;
24909+ for (let i = read; i < args.length; i++) {
24910+ if (A.hasSideEffects(args[i], compressor)) args[kept++] = args[i];
24911+ }
24912+ args.length = kept;
2484924913 }
2485024914 if (!constructsWhenCalled(optimized, compressor)) return optimized;
2485124915 return A.optimize(
@@ -43468,6 +43532,13 @@ const createCompressHelpers = (modules) => {
4346843532 !compressor.exposed(node.definition)
4346943533 ) {
4347043534 fixed = ast.fixedValue(node);
43535+ // A `let` whose `void 0` was dropped still reads as `undefined`.
43536+ if (
43537+ fixed === null &&
43538+ ast.isSymbolLetNode(node.definition.identifiers[0])
43539+ ) {
43540+ return true;
43541+ }
4347143542 }
4347243543 return (
4347343544 isSyntaxNode(fixed) && isNullish(/** @type {Node} */ (fixed), compressor)
0 commit comments