Skip to content

Commit

Permalink
implement mangle.properties.domprops (#5686)
Browse files Browse the repository at this point in the history
- support destructuring syntax
- fix corner case with comma operator
  • Loading branch information
alexlamsl authored Sep 29, 2022
1 parent a570c00 commit bd5fc4c
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 38 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -891,12 +891,15 @@ UglifyJS.minify(code, { mangle: { toplevel: true } }).code;

### Mangle properties options

- `builtins` (default: `false`) — Use `true` to allow the mangling of builtin
DOM properties. Not recommended to override this setting.
- `builtins` (default: `false`) — Use `true` to allow the mangling of built-in
properties of JavaScript API. Not recommended to override this setting.

- `debug` (default: `false`) — Mangle names with the original name still present.
Pass an empty string `""` to enable, or a non-empty string to set the debug suffix.

- `domprops` (default: `false`) — Use `true` to allow the mangling of properties
commonly found in Document Object Model. Not recommended to override this setting.

- `keep_fargs` (default: `false`) — Use `true` to prevent mangling of function
arguments.

Expand Down
11 changes: 0 additions & 11 deletions bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,6 @@ if (specified["beautify"] && specified["output-opts"]) fatal("--beautify cannot
[ "compress", "mangle" ].forEach(function(name) {
if (!(name in options)) options[name] = false;
});
if (options.mangle && options.mangle.properties) {
if (options.mangle.properties.domprops) {
delete options.mangle.properties.domprops;
} else {
if (typeof options.mangle.properties != "object") options.mangle.properties = {};
if (!Array.isArray(options.mangle.properties.reserved)) options.mangle.properties.reserved = [];
require("../tools/domprops").forEach(function(name) {
UglifyJS.push_uniq(options.mangle.properties.reserved, name);
});
}
}
if (/^ast|spidermonkey$/.test(output)) {
if (typeof options.output != "object") options.output = {};
options.output.ast = true;
Expand Down
18 changes: 14 additions & 4 deletions lib/propmangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ function get_builtins() {

function reserve_quoted_keys(ast, reserved) {
ast.walk(new TreeWalker(function(node) {
if (node instanceof AST_ClassProperty || node instanceof AST_ObjectProperty) {
if (node instanceof AST_ClassProperty
|| node instanceof AST_DestructuredKeyVal
|| node instanceof AST_ObjectProperty) {
if (node.key instanceof AST_Node) {
addStrings(node.key, add);
} else if (node.start && node.start.quote) {
Expand Down Expand Up @@ -158,12 +160,16 @@ function mangle_properties(ast, options) {
builtins: false,
cache: null,
debug: false,
domprops: false,
keep_quoted: false,
regex: null,
reserved: null,
}, true);

var reserved = options.builtins ? new Dictionary() : get_builtins();
if (!options.domprops && typeof domprops !== "undefined") domprops.forEach(function(name) {
reserved.set(name, true);
});
if (Array.isArray(options.reserved)) options.reserved.forEach(function(name) {
reserved.set(name, true);
});
Expand Down Expand Up @@ -210,7 +216,9 @@ function mangle_properties(ast, options) {
addStrings(node.args[0], add);
break;
}
} else if (node instanceof AST_ClassProperty || node instanceof AST_ObjectProperty) {
} else if (node instanceof AST_ClassProperty
|| node instanceof AST_DestructuredKeyVal
|| node instanceof AST_ObjectProperty) {
if (node.key instanceof AST_Node) {
addStrings(node.key, add);
} else {
Expand Down Expand Up @@ -244,7 +252,9 @@ function mangle_properties(ast, options) {
mangleStrings(node.args[0]);
break;
}
} else if (node instanceof AST_ClassProperty || node instanceof AST_ObjectProperty) {
} else if (node instanceof AST_ClassProperty
|| node instanceof AST_DestructuredKeyVal
|| node instanceof AST_ObjectProperty) {
if (node.key instanceof AST_Node) {
mangleStrings(node.key);
} else {
Expand Down Expand Up @@ -307,7 +317,7 @@ function mangle_properties(ast, options) {

function mangleStrings(node) {
if (node instanceof AST_Sequence) {
mangleStrings(node.expressions.tail_node());
mangleStrings(node.tail_node());
} else if (node instanceof AST_String) {
node.value = mangle(node.value);
} else if (node instanceof AST_Conditional) {
Expand Down
4 changes: 3 additions & 1 deletion test/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ function test_case(test) {
expect = test.expect_exact;
}
var input = to_toplevel(test.input, test.mangle, test.expression);
var input_code = make_code(input, {}, test.expression);
var input_code = make_code(input, {
keep_quoted_props: true,
}, test.expression);
var input_formatted = make_code(test.input, {
annotations: true,
beautify: true,
Expand Down
1 change: 1 addition & 0 deletions test/compress/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2164,6 +2164,7 @@ issue_4829_2: {
mangle_properties: {
mangle = {
properties: {
domprops: true,
keep_quoted: true,
},
}
Expand Down
17 changes: 17 additions & 0 deletions test/compress/destructured.js
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,23 @@ singleton_side_effects: {
node_version: ">=6"
}

mangle_properties: {
mangle = {
properties: {
domprops: true,
},
}
input: {
function f({ p: a }) {
return a;
}
console.log(f({ p: "PASS" }));
}
expect_exact: 'function f({n}){return n}console.log(f({n:"PASS"}));'
expect_stdout: "PASS"
node_version: ">=6"
}

issue_4280: {
options = {
evaluate: true,
Expand Down
3 changes: 3 additions & 0 deletions test/compress/issue-1321.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
issue_1321_no_debug: {
mangle = {
properties: {
domprops: true,
keep_quoted: true,
},
}
Expand All @@ -23,6 +24,7 @@ issue_1321_debug: {
mangle = {
properties: {
debug: "",
domprops: true,
keep_quoted: true,
},
}
Expand All @@ -44,6 +46,7 @@ issue_1321_debug: {
issue_1321_with_quoted: {
mangle = {
properties: {
domprops: true,
keep_quoted: false,
},
}
Expand Down
5 changes: 4 additions & 1 deletion test/compress/issue-1770.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ mangle_props: {

numeric_literal: {
mangle = {
properties: true,
properties: {
domprops: true,
},
}
beautify = {
beautify: true,
Expand Down Expand Up @@ -125,6 +127,7 @@ identifier: {
mangle = {
properties: {
builtins: true,
domprops: true,
},
}
input: {
Expand Down
2 changes: 2 additions & 0 deletions test/compress/issue-747.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dont_reuse_prop: {
mangle = {
properties: {
domprops: true,
regex: /asd/,
},
}
Expand Down Expand Up @@ -29,6 +30,7 @@ dont_reuse_prop: {
unmangleable_props_should_always_be_reserved: {
mangle = {
properties: {
domprops: true,
regex: /asd/,
},
}
Expand Down
4 changes: 3 additions & 1 deletion test/compress/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ numeric_literal: {
side_effects: true,
}
mangle = {
properties: true,
properties: {
domprops: true,
},
}
beautify = {
beautify: true,
Expand Down
24 changes: 23 additions & 1 deletion test/compress/properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ evaluate_string_length: {
mangle_properties_1: {
mangle = {
properties: {
domprops: true,
keep_quoted: false,
},
}
Expand All @@ -155,9 +156,10 @@ mangle_properties_1: {
mangle_properties_2: {
mangle = {
properties: {
domprops: true,
reserved: [
"value",
]
],
},
}
input: {
Expand Down Expand Up @@ -199,6 +201,24 @@ mangle_properties_2: {
]
}

mangle_properties_3: {
mangle = {
properties: true,
}
input: {
console.log({
[(console, "foo")]: "PASS",
}.foo);
}
expect: {
console.log({
[(console, "o")]: "PASS",
}.o);
}
expect_stdout: "PASS"
node_version: ">=4"
}

mangle_unquoted_properties: {
options = {
evaluate: true,
Expand All @@ -207,6 +227,7 @@ mangle_unquoted_properties: {
mangle = {
properties: {
builtins: true,
domprops: true,
keep_quoted: true,
},
}
Expand Down Expand Up @@ -308,6 +329,7 @@ mangle_debug_suffix_keep_quoted: {
properties: {
builtins: true,
debug: "XYZ",
domprops: true,
keep_quoted: true,
reserved: [],
},
Expand Down
4 changes: 3 additions & 1 deletion test/mocha/let.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ describe("let", function() {
compress: false,
ie: true,
mangle: {
properties: true,
properties: {
domprops: true,
},
},
});
if (result.error) throw result.error;
Expand Down
24 changes: 12 additions & 12 deletions test/mocha/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ describe("minify", function() {
var result = UglifyJS.minify(code, {
compress: false,
mangle: {
properties: true,
toplevel: true
properties: {
domprops: true,
},
toplevel: true,
},
nameCache: cache
nameCache: cache,
});
if (result.error) throw result.error;
original += code;
Expand Down Expand Up @@ -188,21 +190,19 @@ describe("minify", function() {
it("Shouldn't mangle quoted properties", function() {
var js = 'a["foo"] = "bar"; a.color = "red"; x = {"bar": 10};';
var result = UglifyJS.minify(js, {
compress: {
properties: false
},
compress: true,
mangle: {
properties: {
keep_quoted: true
}
domprops: true,
keep_quoted: true,
},
},
output: {
keep_quoted_props: true,
quote_style: 3
}
quote_style: 3,
},
});
assert.strictEqual(result.code,
'a["foo"]="bar",a.a="red",x={"bar":10};');
assert.strictEqual(result.code, 'a["foo"]="bar",a.a="red",x={"bar":10};');
});
it("Should not mangle quoted property within dead code", function() {
var result = UglifyJS.minify('({ "keep": 1 }); g.keep = g.change = 42;', {
Expand Down
4 changes: 2 additions & 2 deletions test/node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var fs = require("fs");

new Function("exports", require("../tools/node").FILES.map(function(file) {
new Function("domprops", "exports", require("../tools/node").FILES.map(function(file) {
if (/exports\.js$/.test(file)) file = require.resolve("./exports");
return fs.readFileSync(file, "utf8");
}).join("\n\n"))(exports);
}).join("\n\n"))(require("../tools/domprops.json"), exports);
4 changes: 2 additions & 2 deletions tools/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ exports.FILES = [
require.resolve("./exports.js"),
];

new Function("exports", function() {
new Function("domprops", "exports", function() {
var code = exports.FILES.map(function(file) {
return fs.readFileSync(file, "utf8");
});
code.push("exports.describe_ast = " + describe_ast.toString());
return code.join("\n\n");
}())(exports);
}())(require("./domprops.json"), exports);

function to_comment(value) {
if (typeof value != "string") value = JSON.stringify(value, function(key, value) {
Expand Down

0 comments on commit bd5fc4c

Please sign in to comment.