Skip to content

Commit 24199e2

Browse files
committed
fix(javascript): read a nested class's private names as its own when keeping a dropped class
A class nested in a dropped class's key that declares the same #name reads its own, so the outer class was kept where terser drops it. The check now reads such a class's body without the names it declares, and its extends with all of them. Tests cover class expressions, private methods, getters, setters, static methods and nested classes, one per branch the check takes.
1 parent 5592543 commit 24199e2

2 files changed

Lines changed: 91 additions & 20 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13932,6 +13932,51 @@ const installCorrect = (modules) => {
1393213932
// A class dropped for its effects leaves its computed keys and static values
1393313933
// outside it, where a private name it declares is no longer declared: terser's
1393413934
// output no longer parses, so a class whose effects read one of its own stays.
13935+
/**
13936+
* @param {Node} classNode a class
13937+
* @returns {Set<string>} the private names it declares
13938+
*/
13939+
const privateNamesDeclaredBy = (classNode) => {
13940+
/** @type {Set<string>} */
13941+
const names = new Set();
13942+
for (const member of classNode.body.body) {
13943+
if (
13944+
A.isClassPrivatePropertyNode(member) ||
13945+
A.isPrivateMethodNode(member) ||
13946+
A.isPrivateGetterNode(member) ||
13947+
A.isPrivateSetterNode(member)
13948+
) {
13949+
names.add(member.key.name);
13950+
}
13951+
}
13952+
return names;
13953+
};
13954+
/**
13955+
* Whether a tree reads one of the private names, past a class nested in it
13956+
* that declares the same name for itself.
13957+
* @param {Node} tree the tree
13958+
* @param {Set<string>} names the names
13959+
* @returns {boolean} true where it reads one
13960+
*/
13961+
const readsPrivateName = (tree, names) =>
13962+
A.walk(tree, (/** @type {Node} */ node) => {
13963+
if (node !== tree && A.isClassNode(node)) {
13964+
const own = privateNamesDeclaredBy(node);
13965+
if (![...own].some((name) => names.has(name))) return undefined;
13966+
// Its `extends` is read where the class sits, its body past its names.
13967+
if (node.superClass && readsPrivateName(node.superClass, names)) {
13968+
return A.walk_abort;
13969+
}
13970+
const outer = new Set([...names].filter((name) => !own.has(name)));
13971+
return outer.size > 0 && readsPrivateName(node.body, outer)
13972+
? A.walk_abort
13973+
: true;
13974+
}
13975+
return (A.isDotHashNode(node) && names.has(node.property.name)) ||
13976+
(A.isPrivateInNode(node) && names.has(node.left.name))
13977+
? A.walk_abort
13978+
: undefined;
13979+
});
1393513980
for (const type of ["ClassDeclaration", "ClassExpression"]) {
1393613981
A.wrapDropSideEffectFree(
1393713982
type,
@@ -13946,26 +13991,10 @@ const installCorrect = (modules) => {
1394613991
if (left === null || left === self || !corrections.enabled) {
1394713992
return left;
1394813993
}
13949-
/** @type {Set<string>} */
13950-
const declared = new Set();
13951-
for (const member of self.body.body) {
13952-
if (
13953-
A.isClassPrivatePropertyNode(member) ||
13954-
A.isPrivateMethodNode(member) ||
13955-
A.isPrivateGetterNode(member) ||
13956-
A.isPrivateSetterNode(member)
13957-
) {
13958-
declared.add(member.key.name);
13959-
}
13960-
}
13961-
if (declared.size === 0) return left;
13962-
const readsOwnName = A.walk(left, (/** @type {Node} */ node) =>
13963-
(A.isDotHashNode(node) && declared.has(node.property.name)) ||
13964-
(A.isPrivateInNode(node) && declared.has(node.left.name))
13965-
? A.walk_abort
13966-
: undefined
13967-
);
13968-
return readsOwnName ? self : left;
13994+
const declared = privateNamesDeclaredBy(self);
13995+
return declared.size > 0 && readsPrivateName(left, declared)
13996+
? self
13997+
: left;
1396913998
}
1397013999
);
1397114000
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,34 @@ const CORRECTED_CASES = [
375375
[
376376
"a class dropped for its effects, a computed method key reading its private name",
377377
"var o = {}; try { (function () { class C { [o.#f]() {} #f = 2; } })(); } catch (e) { console.log(e.name); }"
378+
],
379+
[
380+
"a class expression dropped for its effects, a computed key reading its private name",
381+
"var o = {}; try { (function () { (class { [o.#f] = 1; #f = 2; }); })(); } catch (e) { console.log(e.name); }"
382+
],
383+
[
384+
"a class dropped for its effects, a computed key reading its private method",
385+
"var o = {}; try { (function () { class C { [o.#m] = 1; #m() {} } })(); } catch (e) { console.log(e.name); }"
386+
],
387+
[
388+
"a class dropped for its effects, a computed key reading its private getter",
389+
"var o = {}; try { (function () { class C { [o.#g] = 1; get #g() { return 1; } } })(); } catch (e) { console.log(e.name); }"
390+
],
391+
[
392+
"a class dropped for its effects, a computed key reading its private setter",
393+
"var o = {}; try { (function () { class C { [o.#s] = 1; set #s(v) {} } })(); } catch (e) { console.log(e.name); }"
394+
],
395+
[
396+
"a class dropped for its effects, a computed key reading its static private method",
397+
"var o = {}; try { (function () { class C { [o.#m] = 1; static #m() {} } })(); } catch (e) { console.log(e.name); }"
398+
],
399+
[
400+
"a class dropped for its effects, a class in its computed key reading its private name",
401+
'var o = {}; (function () { class C { [(class { static r(x) { return #f in x; } }).r(o)] = 1; #f = 2; } })(); console.log("ok");'
402+
],
403+
[
404+
"a class dropped for its effects, the extends of a class in its key reading its private name",
405+
"var o = {}; try { (function () { class C { [((class extends (o.#f, Object) { #f = 1; }), 1)] = 1; #f = 2; } })(); } catch (e) { console.log(e.name); }"
378406
]
379407
].map(
380408
([name, input]) =>
@@ -3361,6 +3389,20 @@ describe("syntax-printer", () => {
33613389
expect(phases).toContain("correct");
33623390
});
33633391

3392+
it("should drop a class whose effects read only a nested class's own private name, as terser does", async () => {
3393+
const { minify } = await load();
3394+
const input =
3395+
'var o = {}; (function () { class C { [(class { #f = 1; static r(x) { return #f in x; } }).r(o)] = 1; #f = 2; #g = 3; } })(); console.log("ok");';
3396+
const { code } = await minify(input, { compress: {}, mangle: false });
3397+
const reference = await terserReference().minify(input, {
3398+
compress: {},
3399+
mangle: false
3400+
});
3401+
3402+
expect(code).toBe(reference.code);
3403+
expect(runProgram(/** @type {string} */ (code))).toBe(runProgram(input));
3404+
});
3405+
33643406
it("should drop a class whose effects read only an enclosing class's private name, as terser does", async () => {
33653407
const { minify } = await load();
33663408
const input =

0 commit comments

Comments
 (0)