Skip to content

Commit 497492d

Browse files
fix(javascript): keep an unused class whose heritage or computed key reads its name in the minifier
Claude-Session: https://claude.ai/code/session_01UgQAw35dRJd9GKMF9p5CcQ
1 parent 31062ab commit 497492d

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16954,6 +16954,41 @@ const installCorrect = (modules) => {
1695416954
? self
1695516955
: original(self, compressor, firstInStatement)
1695616956
);
16957+
/**
16958+
* Whether a class's heritage or a computed key reads the class's own name,
16959+
* which throws, the binding being set only after both run.
16960+
* @param {Node} self a class
16961+
* @returns {boolean} true for `class x { [x] }`
16962+
*/
16963+
const readsOwnNameEarly = (self) => {
16964+
const { id } = self;
16965+
if (!id) return false;
16966+
/** @type {Node[]} */
16967+
const toVisit = [];
16968+
if (self.superClass) toVisit.push(self.superClass);
16969+
for (const property of self.body.body) {
16970+
if (property.computed) toVisit.push(property.key);
16971+
}
16972+
return ast.walk(
16973+
self,
16974+
(/** @type {Node} */ node, /** @type {Node[]} */ rest) => {
16975+
if (A.isLambdaNode(node)) return true;
16976+
if (A.isClassNode(node)) {
16977+
if (node.superClass) rest.push(node.superClass);
16978+
for (const property of node.body.body) {
16979+
if (property.computed) rest.push(property.key);
16980+
}
16981+
return true;
16982+
}
16983+
return A.isSymbolRefNode(node) &&
16984+
node.definition &&
16985+
node.definition.identifiers.includes(id)
16986+
? ast.walk_abort
16987+
: undefined;
16988+
},
16989+
toVisit
16990+
);
16991+
};
1695716992
for (const type of ["ClassDeclaration", "ClassExpression"]) {
1695816993
for (const wrapAnswer of [ast.wrapHasSideEffects, ast.wrapMayThrow]) {
1695916994
wrapAnswer(
@@ -16967,7 +17002,9 @@ const installCorrect = (modules) => {
1696717002
) =>
1696817003
original(self, compressor) ||
1696917004
(corrections.enabled &&
16970-
(extendsNoConstructor(self) || extendsUnknown(self, compressor)))
17005+
(extendsNoConstructor(self) ||
17006+
extendsUnknown(self, compressor) ||
17007+
readsOwnNameEarly(self)))
1697117008
);
1697217009
}
1697317010
A.wrapDropSideEffectFree(
@@ -16980,7 +17017,9 @@ const installCorrect = (modules) => {
1698017017
/** @type {CompressorShape} */ compressor
1698117018
) =>
1698217019
corrections.enabled &&
16983-
(extendsNoConstructor(self) || extendsUnknown(self, compressor))
17020+
(extendsNoConstructor(self) ||
17021+
extendsUnknown(self, compressor) ||
17022+
readsOwnNameEarly(self))
1698417023
? self
1698517024
: original(self, compressor)
1698617025
);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1389,6 +1389,11 @@ const CORRECTED_CASES = [
13891389
"function m(v) { v = 2; return arguments[0]; } console.log(m());",
13901390
{ compress: { arguments: true }, mangle: false }
13911391
],
1392+
[
1393+
"an unused class whose computed key reads its name, before it is set",
1394+
"(function () { try { class C { [typeof { x: C }]() {} } console.log('no error'); } catch (e) { console.log(e.name); } })();",
1395+
{ compress: {}, mangle: false }
1396+
],
13921397
[
13931398
"a class's name read in its own computed key, before it is set",
13941399
"try { class x { [!{ x }]; } new x(); console.log('no error'); } catch (e) { console.log(e.name); }",

0 commit comments

Comments
 (0)