diff --git a/README.md b/README.md index 9c63d727..796d37b9 100644 --- a/README.md +++ b/README.md @@ -135,9 +135,12 @@ The available options are: --mangle-props --mangle-props Mangle property names (default `0`). Set to `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 - 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 --name-cache File to hold mangled names mappings --pure-funcs List of functions that can be safely removed if @@ -666,7 +669,8 @@ Other 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 functionality please suggest! diff --git a/lib/propmangle.js b/lib/propmangle.js index c905e85e..08043d73 100644 --- a/lib/propmangle.js +++ b/lib/propmangle.js @@ -95,8 +95,7 @@ function mangle_properties(ast, options) { } else if (node instanceof AST_ObjectProperty) { // 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) { if (this.parent() instanceof AST_Assign) { @@ -105,7 +104,7 @@ function mangle_properties(ast, options) { } else if (node instanceof AST_Sub) { if (this.parent() instanceof AST_Assign) { - if (!(ignore_quoted && node.property.quote)) + if (!ignore_quoted) addStrings(node.property); } } @@ -114,7 +113,8 @@ function mangle_properties(ast, options) { // step 2: transform the tree, renaming properties return ast.transform(new TreeTransformer(function(node){ 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) { // setter or getter @@ -124,7 +124,8 @@ function mangle_properties(ast, options) { node.property = mangle(node.property); } 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) { // if (should_mangle(node.value)) { diff --git a/test/compress/properties.js b/test/compress/properties.js index c903d315..574c5142 100644 --- a/test/compress/properties.js +++ b/test/compress/properties.js @@ -92,15 +92,34 @@ mangle_properties: { mangle_unquoted_properties: { mangle_props = { ignore_quoted: true - }; + } + beautify = { + beautify: false, + quote_style: 3, + keep_quoted_props: true, + } input: { - a["foo"] = "bar"; - a.color = "red"; - x = {"bar": 10}; + function f1() { + a["foo"] = "bar"; + a.color = "red"; + x = {"bar": 10}; + } + function f2() { + a.foo = "bar"; + a['color'] = "red"; + x = {bar: 10}; + } } expect: { - a["foo"] = "bar"; - a.a = "red"; - x = {"bar": 10}; + function f1() { + a["foo"] = "bar"; + a.a = "red"; + x = {"bar": 10}; + } + function f2() { + a.b = "bar"; + a['color'] = "red"; + x = {c: 10}; + } } } diff --git a/test/mocha/minify.js b/test/mocha/minify.js index 66fd8885..02d31558 100644 --- a/test/mocha/minify.js +++ b/test/mocha/minify.js @@ -38,4 +38,25 @@ describe("minify", function() { 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};'); + }); + }); }); diff --git a/test/run-tests.js b/test/run-tests.js index 0a249b9f..01948630 100755 --- a/test/run-tests.js +++ b/test/run-tests.js @@ -106,7 +106,11 @@ function run_compress_tests() { expect = test.expect_exact; } 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) { input = U.mangle_properties(input, test.mangle_props); }