Skip to content

Commit

Permalink
mangle private class properties as local scope (#5944)
Browse files Browse the repository at this point in the history
closes #5937
  • Loading branch information
alexlamsl committed Sep 29, 2024
1 parent 465c29e commit 50f8a5a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 13 deletions.
15 changes: 11 additions & 4 deletions lib/propmangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function mangle_properties(ast, options) {
reserved.set(name, true);
});

var cname = -1;
var cname = [ -1, -1 ];
var cache;
if (options.cache) {
cache = options.cache.props;
Expand Down Expand Up @@ -245,7 +245,7 @@ function mangle_properties(ast, options) {
}));

// step 2: renaming properties
ast.walk(new TreeWalker(function(node) {
ast.walk(new TreeWalker(function(node, descend) {
if (node instanceof AST_Binary) {
if (node.operator == "in") mangleStrings(node.left);
} else if (node.TYPE == "Call") {
Expand All @@ -265,6 +265,12 @@ function mangle_properties(ast, options) {
mangleStrings(node.args[0]);
break;
}
} else if (node instanceof AST_Class) {
var save_cname = cname[1];
cname[1] = -1;
descend();
cname[1] = save_cname;
return true;
} else if (node instanceof AST_ClassProperty
|| node instanceof AST_DestructuredKeyVal
|| node instanceof AST_ObjectProperty) {
Expand Down Expand Up @@ -331,11 +337,12 @@ function mangle_properties(ast, options) {
var debug_mangled = "_$" + name + "$" + debug_suffix + "_";
if (can_mangle(debug_mangled)) mangled = debug_mangled;
}
var hash = +/^#/.test(name);
// either debug mode is off, or it is on and we could not use the mangled name
if (!mangled) do {
mangled = base54(++cname);
mangled = base54(++cname[hash]);
} while (!can_mangle(mangled));
if (/^#/.test(name)) mangled = "#" + mangled;
if (hash) mangled = "#" + mangled;
cache.set(name, mangled);
}
AST_Node.info("Mapping property {name} to {mangled}", {
Expand Down
80 changes: 71 additions & 9 deletions test/compress/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2401,31 +2401,93 @@ mangle_properties: {
expect: {
class A {
static #t = "PASS";
static get s() {
static get t() {
return this.#t;
}
#i(t) {
return (this["q"] = t) * this.e;
#s(t) {
return (this["q"] = t) * this.s;
}
set q(t) {
this.e = t + 1;
this.s = t + 1;
}
e = this.#i(6);
s = this.#s(6);
}
console.log(A.s, new A().e);
console.log(A.t, new A().s);
}
expect_stdout: "PASS 42"
expect_warnings: [
"INFO: Preserving reserved property q",
"INFO: Mapping property #P to #t",
"INFO: Mapping property Q to s",
"INFO: Mapping property #p to #i",
"INFO: Mapping property r to e",
"INFO: Mapping property Q to t",
"INFO: Mapping property #p to #s",
"INFO: Mapping property r to s",
"INFO: Preserving reserved property log",
]
node_version: ">=14.6"
}

mangle_private_local: {
mangle = {
properties: {
domprops: true,
},
}
input: {
class A {
p = "foo";
#q = "bar";
f() {
console.log(this.p, this.#q);
}
}
class B {
#r = "moo";
#g() {
return "baz";
}
h() {
console.log(this.#r, this.#g());
}
}
new A().f();
new B().h();
}
expect: {
class A {
s = "foo";
#s = "bar";
o() {
console.log(this.s, this.#s);
}
}
class B {
#s = "moo";
#o() {
return "baz";
}
e() {
console.log(this.#s, this.#o());
}
}
new A().o();
new B().e();
}
expect_stdout: [
"foo bar",
"moo baz",
]
expect_warnings: [
"INFO: Mapping property p to s",
"INFO: Mapping property #q to #s",
"INFO: Mapping property f to o",
"INFO: Preserving reserved property log",
"INFO: Mapping property #r to #s",
"INFO: Mapping property #g to #o",
"INFO: Mapping property h to e",
]
node_version: ">=14.6"
}

issue_4848: {
options = {
if_return: true,
Expand Down

0 comments on commit 50f8a5a

Please sign in to comment.