Skip to content

Commit be2d17f

Browse files
fix(minifier): leave a declared function's typeof to terser
Terser already folds `typeof f` compared against a string where `f` is a function declaration nothing writes; the improve-phase fold only added the bare form, where `"function"` is longer than `typeof f`. Retire the swc entries the catch-shadowed function drop now beats. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 122e921 commit be2d17f

3 files changed

Lines changed: 9 additions & 59 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22546,43 +22546,6 @@ const installImprove = (modules) => {
2254622546
return undeletable ? makeNode(A.FalseNode, optimized) : optimized;
2254722547
});
2254822548

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-
2258622549
// `typeof (a === b)` as `"boolean"`: the type of what nothing else observes.
2258722550
A.wrapOptimizer("UnaryPrefix", (optimizeUnary) => (self, compressor) => {
2258822551
const optimized = optimizeUnary(self, compressor);
@@ -22596,16 +22559,15 @@ const installImprove = (modules) => {
2259622559
return optimized;
2259722560
}
2259822561
const { argument } = optimized;
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;
22562+
const type = A.isClassNode(argument)
22563+
? "function"
22564+
: A.isBoolean(argument)
22565+
? "boolean"
22566+
: A.isNumber(argument, compressor)
22567+
? "number"
22568+
: A.isString(argument, compressor)
22569+
? "string"
22570+
: undefined;
2260922571
return type === undefined
2261022572
? optimized
2261122573
: makeNode(A.StringNode, optimized, { value: type });

‎test/helpers/swcSmaller.js‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ const SWC_SMALLER_BY_REASON = [
7676
"terser compress: loops/issue_2740_7",
7777
"terser compress: loops/issue_2740_8",
7878
"terser compress: reduce_vars/boolean_binary_assign",
79-
"terser compress: reduce_vars/defun_catch_1",
80-
"terser compress: reduce_vars/defun_catch_2",
81-
"terser compress: reduce_vars/defun_catch_3",
82-
"terser compress: reduce_vars/defun_catch_6",
8379
"terser compress: reduce_vars/escape_local_sequence",
8480
"terser compress: reduce_vars/issue_3140_5",
8581
"terser compress: reduce_vars/multi_def_3",
@@ -480,9 +476,7 @@ const SWC_SMALLER_BY_REASON = [
480476
"swc minifier: fixture/issues/11684/class-expression/input.js",
481477
"swc minifier: fixture/issues/11684/constructor-scopes/input.js",
482478
"swc minifier: fixture/issues/11684/identifier-reduce-vars-only/input.js",
483-
"swc minifier: fixture/issues/11684/identifier-side-effects/input.js",
484479
"swc minifier: fixture/issues/11684/identifier-unused-only/input.js",
485-
"swc minifier: fixture/issues/11684/side-effects/input.js",
486480
"swc minifier: fixture/issues/12212/safe-unused-rest/input.js",
487481
"swc minifier: fixture/issues/12218/leading-argument/input.js",
488482
"terser compress: collapse_vars/issue_2436_12",

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,12 +293,6 @@ const IMPROVED_CASES = [
293293
{ compress: {}, mangle: false },
294294
"function f(g){let d;return[d,1]}console.log(f(function(){throw 0}));"
295295
],
296-
[
297-
"the type of a function its scope declares and nothing writes",
298-
'function g() { function x() {} return [typeof x === "function", "function" != typeof x, typeof x]; } console.log(g());',
299-
{ compress: {}, mangle: false },
300-
'function g(){return[!0,!1,"function"]}console.log(g());'
301-
],
302296
[
303297
"an unused function a catch clause's parameter shadows",
304298
"function f() { function a() {} try { throw 42; } catch (a) { return a; } } console.log(f());",

0 commit comments

Comments
 (0)