Skip to content

Commit 35956eb

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 dcbc85f commit 35956eb

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
@@ -14706,6 +14706,51 @@ const installCorrect = (modules) => {
1470614706
// A class dropped for its effects leaves its computed keys and static values
1470714707
// outside it, where a private name it declares is no longer declared: terser's
1470814708
// output no longer parses, so a class whose effects read one of its own stays.
14709+
/**
14710+
* @param {Node} classNode a class
14711+
* @returns {Set<string>} the private names it declares
14712+
*/
14713+
const privateNamesDeclaredBy = (classNode) => {
14714+
/** @type {Set<string>} */
14715+
const names = new Set();
14716+
for (const member of classNode.body.body) {
14717+
if (
14718+
A.isClassPrivatePropertyNode(member) ||
14719+
A.isPrivateMethodNode(member) ||
14720+
A.isPrivateGetterNode(member) ||
14721+
A.isPrivateSetterNode(member)
14722+
) {
14723+
names.add(member.key.name);
14724+
}
14725+
}
14726+
return names;
14727+
};
14728+
/**
14729+
* Whether a tree reads one of the private names, past a class nested in it
14730+
* that declares the same name for itself.
14731+
* @param {Node} tree the tree
14732+
* @param {Set<string>} names the names
14733+
* @returns {boolean} true where it reads one
14734+
*/
14735+
const readsPrivateName = (tree, names) =>
14736+
A.walk(tree, (/** @type {Node} */ node) => {
14737+
if (node !== tree && A.isClassNode(node)) {
14738+
const own = privateNamesDeclaredBy(node);
14739+
if (![...own].some((name) => names.has(name))) return undefined;
14740+
// Its `extends` is read where the class sits, its body past its names.
14741+
if (node.superClass && readsPrivateName(node.superClass, names)) {
14742+
return A.walk_abort;
14743+
}
14744+
const outer = new Set([...names].filter((name) => !own.has(name)));
14745+
return outer.size > 0 && readsPrivateName(node.body, outer)
14746+
? A.walk_abort
14747+
: true;
14748+
}
14749+
return (A.isDotHashNode(node) && names.has(node.property.name)) ||
14750+
(A.isPrivateInNode(node) && names.has(node.left.name))
14751+
? A.walk_abort
14752+
: undefined;
14753+
});
1470914754
for (const type of ["ClassDeclaration", "ClassExpression"]) {
1471014755
A.wrapDropSideEffectFree(
1471114756
type,
@@ -14720,26 +14765,10 @@ const installCorrect = (modules) => {
1472014765
if (left === null || left === self || !corrections.enabled) {
1472114766
return left;
1472214767
}
14723-
/** @type {Set<string>} */
14724-
const declared = new Set();
14725-
for (const member of self.body.body) {
14726-
if (
14727-
A.isClassPrivatePropertyNode(member) ||
14728-
A.isPrivateMethodNode(member) ||
14729-
A.isPrivateGetterNode(member) ||
14730-
A.isPrivateSetterNode(member)
14731-
) {
14732-
declared.add(member.key.name);
14733-
}
14734-
}
14735-
if (declared.size === 0) return left;
14736-
const readsOwnName = A.walk(left, (/** @type {Node} */ node) =>
14737-
(A.isDotHashNode(node) && declared.has(node.property.name)) ||
14738-
(A.isPrivateInNode(node) && declared.has(node.left.name))
14739-
? A.walk_abort
14740-
: undefined
14741-
);
14742-
return readsOwnName ? self : left;
14768+
const declared = privateNamesDeclaredBy(self);
14769+
return declared.size > 0 && readsPrivateName(left, declared)
14770+
? self
14771+
: left;
1474314772
}
1474414773
);
1474514774
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,34 @@ const CORRECTED_CASES = [
432432
[
433433
"a class dropped for its effects, a computed method key reading its private name",
434434
"var o = {}; try { (function () { class C { [o.#f]() {} #f = 2; } })(); } catch (e) { console.log(e.name); }"
435+
],
436+
[
437+
"a class expression dropped for its effects, a computed key reading its private name",
438+
"var o = {}; try { (function () { (class { [o.#f] = 1; #f = 2; }); })(); } catch (e) { console.log(e.name); }"
439+
],
440+
[
441+
"a class dropped for its effects, a computed key reading its private method",
442+
"var o = {}; try { (function () { class C { [o.#m] = 1; #m() {} } })(); } catch (e) { console.log(e.name); }"
443+
],
444+
[
445+
"a class dropped for its effects, a computed key reading its private getter",
446+
"var o = {}; try { (function () { class C { [o.#g] = 1; get #g() { return 1; } } })(); } catch (e) { console.log(e.name); }"
447+
],
448+
[
449+
"a class dropped for its effects, a computed key reading its private setter",
450+
"var o = {}; try { (function () { class C { [o.#s] = 1; set #s(v) {} } })(); } catch (e) { console.log(e.name); }"
451+
],
452+
[
453+
"a class dropped for its effects, a computed key reading its static private method",
454+
"var o = {}; try { (function () { class C { [o.#m] = 1; static #m() {} } })(); } catch (e) { console.log(e.name); }"
455+
],
456+
[
457+
"a class dropped for its effects, a class in its computed key reading its private name",
458+
'var o = {}; (function () { class C { [(class { static r(x) { return #f in x; } }).r(o)] = 1; #f = 2; } })(); console.log("ok");'
459+
],
460+
[
461+
"a class dropped for its effects, the extends of a class in its key reading its private name",
462+
"var o = {}; try { (function () { class C { [((class extends (o.#f, Object) { #f = 1; }), 1)] = 1; #f = 2; } })(); } catch (e) { console.log(e.name); }"
435463
]
436464
].map(
437465
([name, input]) =>
@@ -3480,6 +3508,20 @@ describe("syntax-printer", () => {
34803508
expect(phases).toContain("correct");
34813509
});
34823510

3511+
it("should drop a class whose effects read only a nested class's own private name, as terser does", async () => {
3512+
const { minify } = await load();
3513+
const input =
3514+
'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");';
3515+
const { code } = await minify(input, { compress: {}, mangle: false });
3516+
const reference = await terserReference().minify(input, {
3517+
compress: {},
3518+
mangle: false
3519+
});
3520+
3521+
expect(code).toBe(reference.code);
3522+
expect(runProgram(/** @type {string} */ (code))).toBe(runProgram(input));
3523+
});
3524+
34833525
it("should drop a class whose effects read only an enclosing class's private name, as terser does", async () => {
34843526
const { minify } = await load();
34853527
const input =

0 commit comments

Comments
 (0)