Address feedback
- Fix mangle implementation to work for all cases - Update and add tests - Document usage via the API
This commit is contained in:
parent
6f3fea60d2
commit
51ed9b80c9
10
README.md
10
README.md
|
|
@ -135,9 +135,12 @@ The available options are:
|
||||||
--mangle-props
|
--mangle-props
|
||||||
--mangle-props Mangle property names (default `0`). Set to
|
--mangle-props Mangle property names (default `0`). Set to
|
||||||
`true` or `1` to mangle all property names. Set
|
`true` or `1` to mangle all property names. Set
|
||||||
to `1` to only mangle unquoted property names.
|
to `2` to only mangle unquoted property names.
|
||||||
Use the `keep_quoted_props` beautifier option to
|
Use the `keep_quoted_props` beautifier option to
|
||||||
preserve the quotes around property names.
|
preserve the quotes around property names and set
|
||||||
|
the `properties` compressor option to `false` to
|
||||||
|
prevent rewriting quoted properties with dot
|
||||||
|
notation.
|
||||||
--mangle-regex Only mangle property names matching the regex
|
--mangle-regex Only mangle property names matching the regex
|
||||||
--name-cache File to hold mangled names mappings
|
--name-cache File to hold mangled names mappings
|
||||||
--pure-funcs List of functions that can be safely removed if
|
--pure-funcs List of functions that can be safely removed if
|
||||||
|
|
@ -666,7 +669,8 @@ Other options:
|
||||||
|
|
||||||
##### mangleProperties options
|
##### mangleProperties options
|
||||||
|
|
||||||
- `regex` — Pass a RegExp to only mangle certain names (maps to the `--mange-regex` CLI arguments option)
|
- `regex` — Pass a RegExp to only mangle certain names (maps to the `--mangle-regex` CLI arguments option)
|
||||||
|
- `ignore_quoted` – Only mangle unquoted property names (maps to the `--mangle-props 2` CLI arguments option)
|
||||||
|
|
||||||
We could add more options to `UglifyJS.minify` — if you need additional
|
We could add more options to `UglifyJS.minify` — if you need additional
|
||||||
functionality please suggest!
|
functionality please suggest!
|
||||||
|
|
|
||||||
|
|
@ -95,8 +95,7 @@ function mangle_properties(ast, options) {
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_ObjectProperty) {
|
else if (node instanceof AST_ObjectProperty) {
|
||||||
// setter or getter, since KeyVal is handled above
|
// setter or getter, since KeyVal is handled above
|
||||||
if (!(ignore_quoted && node.key.quote))
|
add(node.key.name);
|
||||||
add(node.key.name);
|
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_Dot) {
|
else if (node instanceof AST_Dot) {
|
||||||
if (this.parent() instanceof AST_Assign) {
|
if (this.parent() instanceof AST_Assign) {
|
||||||
|
|
@ -105,7 +104,7 @@ function mangle_properties(ast, options) {
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_Sub) {
|
else if (node instanceof AST_Sub) {
|
||||||
if (this.parent() instanceof AST_Assign) {
|
if (this.parent() instanceof AST_Assign) {
|
||||||
if (!(ignore_quoted && node.property.quote))
|
if (!ignore_quoted)
|
||||||
addStrings(node.property);
|
addStrings(node.property);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -114,7 +113,8 @@ function mangle_properties(ast, options) {
|
||||||
// step 2: transform the tree, renaming properties
|
// step 2: transform the tree, renaming properties
|
||||||
return ast.transform(new TreeTransformer(function(node){
|
return ast.transform(new TreeTransformer(function(node){
|
||||||
if (node instanceof AST_ObjectKeyVal) {
|
if (node instanceof AST_ObjectKeyVal) {
|
||||||
node.key = mangle(node.key);
|
if (!(ignore_quoted && node.quote))
|
||||||
|
node.key = mangle(node.key);
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_ObjectProperty) {
|
else if (node instanceof AST_ObjectProperty) {
|
||||||
// setter or getter
|
// setter or getter
|
||||||
|
|
@ -124,7 +124,8 @@ function mangle_properties(ast, options) {
|
||||||
node.property = mangle(node.property);
|
node.property = mangle(node.property);
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_Sub) {
|
else if (node instanceof AST_Sub) {
|
||||||
node.property = mangleStrings(node.property);
|
if (!ignore_quoted)
|
||||||
|
node.property = mangleStrings(node.property);
|
||||||
}
|
}
|
||||||
// else if (node instanceof AST_String) {
|
// else if (node instanceof AST_String) {
|
||||||
// if (should_mangle(node.value)) {
|
// if (should_mangle(node.value)) {
|
||||||
|
|
|
||||||
|
|
@ -92,15 +92,34 @@ mangle_properties: {
|
||||||
mangle_unquoted_properties: {
|
mangle_unquoted_properties: {
|
||||||
mangle_props = {
|
mangle_props = {
|
||||||
ignore_quoted: true
|
ignore_quoted: true
|
||||||
};
|
}
|
||||||
|
beautify = {
|
||||||
|
beautify: false,
|
||||||
|
quote_style: 3,
|
||||||
|
keep_quoted_props: true,
|
||||||
|
}
|
||||||
input: {
|
input: {
|
||||||
a["foo"] = "bar";
|
function f1() {
|
||||||
a.color = "red";
|
a["foo"] = "bar";
|
||||||
x = {"bar": 10};
|
a.color = "red";
|
||||||
|
x = {"bar": 10};
|
||||||
|
}
|
||||||
|
function f2() {
|
||||||
|
a.foo = "bar";
|
||||||
|
a['color'] = "red";
|
||||||
|
x = {bar: 10};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
a["foo"] = "bar";
|
function f1() {
|
||||||
a.a = "red";
|
a["foo"] = "bar";
|
||||||
x = {"bar": 10};
|
a.a = "red";
|
||||||
|
x = {"bar": 10};
|
||||||
|
}
|
||||||
|
function f2() {
|
||||||
|
a.b = "bar";
|
||||||
|
a['color'] = "red";
|
||||||
|
x = {c: 10};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -38,4 +38,25 @@ describe("minify", function() {
|
||||||
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
|
assert.strictEqual(result.code, 'var foo={x:1,y:2,z:3};');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("mangleProperties", function() {
|
||||||
|
it("Shouldn't mangle quoted properties", function() {
|
||||||
|
var js = 'a["foo"] = "bar"; a.color = "red"; x = {"bar": 10};';
|
||||||
|
var result = Uglify.minify(js, {
|
||||||
|
fromString: true,
|
||||||
|
compress: {
|
||||||
|
properties: false
|
||||||
|
},
|
||||||
|
mangleProperties: {
|
||||||
|
ignore_quoted: true
|
||||||
|
},
|
||||||
|
output: {
|
||||||
|
keep_quoted_props: true,
|
||||||
|
quote_style: 3
|
||||||
|
}
|
||||||
|
});
|
||||||
|
assert.strictEqual(result.code,
|
||||||
|
'a["foo"]="bar",a.a="red",x={"bar":10};');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -106,7 +106,11 @@ function run_compress_tests() {
|
||||||
expect = test.expect_exact;
|
expect = test.expect_exact;
|
||||||
}
|
}
|
||||||
var input = as_toplevel(test.input);
|
var input = as_toplevel(test.input);
|
||||||
var input_code = make_code(test.input, { beautify: true });
|
var input_code = make_code(test.input, {
|
||||||
|
beautify: true,
|
||||||
|
quote_style: 3,
|
||||||
|
keep_quoted_props: true
|
||||||
|
});
|
||||||
if (test.mangle_props) {
|
if (test.mangle_props) {
|
||||||
input = U.mangle_properties(input, test.mangle_props);
|
input = U.mangle_properties(input, test.mangle_props);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user