Skip to content

Commit

Permalink
Mangle private properties by default when manglin
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell committed Aug 31, 2021
1 parent d3fb5b0 commit 6c0055b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 22 deletions.
2 changes: 2 additions & 0 deletions lib/minify.js
Expand Up @@ -14,6 +14,7 @@ import { Compressor } from "./compress/index.js";
import { SourceMap } from "./sourcemap.js";
import {
mangle_properties,
mangle_private_properties,
reserve_quoted_keys,
} from "./propmangle.js";

Expand Down Expand Up @@ -204,6 +205,7 @@ async function minify(files, options) {
if (options.mangle) {
toplevel.compute_char_frequency(options.mangle);
toplevel.mangle_names(options.mangle);
toplevel = mangle_private_properties(toplevel, options.mangle);
}
if (timings) timings.properties = Date.now();
if (options.mangle && options.mangle.properties) {
Expand Down
51 changes: 32 additions & 19 deletions lib/propmangle.js
Expand Up @@ -140,6 +140,33 @@ function addStrings(node, add) {
}));
}

function mangle_private_properties(ast, options) {
var cprivate = -1;
var private_cache = new Map();
var nth_identifier = options.nth_identifier || base54;

return ast.transform(new TreeTransformer(function(node) {
if (
node instanceof AST_ClassPrivateProperty
|| node instanceof AST_PrivateMethod
) {
node.key.name = mangle_private(node.key.name);
} else if (node instanceof AST_DotHash) {
node.property = mangle_private(node.property);
}
}));

function mangle_private(name) {
let mangled = private_cache.get(name);
if (!mangled) {
mangled = nth_identifier.get(++cprivate);
private_cache.set(name, mangled);
}

return mangled;
}
}

function mangle_properties(ast, options) {
options = defaults(options, {
builtins: false,
Expand All @@ -161,10 +188,8 @@ function mangle_properties(ast, options) {
if (!options.builtins) find_builtins(reserved);

var cname = -1;
var cprivate = -1;

var cache;
var private_cache = new Map();
if (options.cache) {
cache = options.cache.props;
cache.forEach(function(mangled_name) {
Expand All @@ -187,7 +212,6 @@ function mangle_properties(ast, options) {

var names_to_mangle = new Set();
var unmangleable = new Set();
var private_properties = new Set();

var keep_quoted_strict = options.keep_quoted === "strict";

Expand All @@ -196,10 +220,9 @@ function mangle_properties(ast, options) {
if (
node instanceof AST_ClassPrivateProperty
|| node instanceof AST_PrivateMethod
|| node instanceof AST_DotHash
) {
private_properties.add(node.key.name);
} else if (node instanceof AST_DotHash) {
private_properties.add(node.property);
// handled by mangle_private_properties
} else if (node instanceof AST_ObjectKeyVal) {
if (typeof node.key == "string" &&
(!keep_quoted_strict || !node.quote)) {
Expand Down Expand Up @@ -240,10 +263,9 @@ function mangle_properties(ast, options) {
if (
node instanceof AST_ClassPrivateProperty
|| node instanceof AST_PrivateMethod
|| node instanceof AST_DotHash
) {
node.key.name = mangle_private(node.key.name);
} else if (node instanceof AST_DotHash) {
node.property = mangle_private(node.property);
// handled by mangle_private_properties
} else if (node instanceof AST_ObjectKeyVal) {
if (typeof node.key == "string" &&
(!keep_quoted_strict || !node.quote)) {
Expand Down Expand Up @@ -324,16 +346,6 @@ function mangle_properties(ast, options) {
return mangled;
}

function mangle_private(name) {
let mangled = private_cache.get(name);
if (!mangled) {
mangled = nth_identifier.get(++cprivate);
private_cache.set(name, mangled);
}

return mangled;
}

function mangleStrings(node) {
return node.transform(new TreeTransformer(function(node) {
if (node instanceof AST_Sequence) {
Expand All @@ -353,4 +365,5 @@ function mangle_properties(ast, options) {
export {
reserve_quoted_keys,
mangle_properties,
mangle_private_properties,
};
1 change: 1 addition & 0 deletions terser-functional-tests
Submodule terser-functional-tests added at 7e4474
3 changes: 3 additions & 0 deletions test/compress.js
Expand Up @@ -14,6 +14,7 @@ import { Compressor } from "../lib/compress/index.js";
import {
reserve_quoted_keys,
mangle_properties,
mangle_private_properties,
} from "../lib/propmangle.js";
import { base54 } from "../lib/scope.js";
import { string_template, defaults } from "../lib/utils/index.js";
Expand Down Expand Up @@ -227,6 +228,7 @@ async function run_compress_tests() {
}
})(test.mangle.cache);
output.mangle_names(test.mangle);
mangle_private_properties(output, test.mangle);
if (test.mangle.properties) {
output = mangle_properties(output, test.mangle.properties);
}
Expand Down Expand Up @@ -291,6 +293,7 @@ async function run_compress_tests() {
}
var tests = parse_test(path.resolve(dir, file));
for (var i in tests) if (tests.hasOwnProperty(i)) {
if (process.env.GREP && !i.includes(process.env.GREP)) continue;
if (!await test_case(tests[i])) {
failures++;
failed_files[file] = 1;
Expand Down
6 changes: 3 additions & 3 deletions test/compress/issue_1014.js
Expand Up @@ -74,10 +74,10 @@ ternary_and_private_methods: {
}
expect: {
class A {
#fail() { return false; }
get #pass() { return "PASS"; }
#s() { return false; }
get #i() { return "PASS"; }
print() {
console.log(this.#fail() ? this.#fail() : this.#pass);
console.log(this.#s() ? this.#s() : this.#i);
}
}
new A().print();
Expand Down

0 comments on commit 6c0055b

Please sign in to comment.