diff --git a/README.md b/README.md index 40b2e1a0..8d07cfcd 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ The available options are: `debug` Add debug prefix and suffix. `domprops` Mangle property names that overlaps with DOM properties. - `ignore_quoted` Only mangle unquoted properies. + `keep_quoted` Only mangle unquoted properies. `regex` Only mangle matched property names. `reserved` List of names that should not be mangled. -b, --beautify [options] Beautify output/specify output options: @@ -256,14 +256,14 @@ of mangled property names. Using the name cache is not necessary if you compress all your files in a single call to UglifyJS. -#### Mangling unquoted names (`--mangle-props ignore_quoted`) +#### Mangling unquoted names (`--mangle-props keep_quoted`) Using quoted property name (`o["foo"]`) reserves the property name (`foo`) so that it is not mangled throughout the entire script even when used in an unquoted style (`o.foo`). Example: ``` -$ echo 'var o={"foo":1, bar:3}; o.foo += o.bar; console.log(o.foo);' | uglifyjs --mangle-props ignore_quoted -mc +$ echo 'var o={"foo":1, bar:3}; o.foo += o.bar; console.log(o.foo);' | uglifyjs --mangle-props keep_quoted -mc var o={foo:1,a:3};o.foo+=o.a,console.log(o.foo); ``` @@ -745,8 +745,8 @@ Other options: ##### mangle.properties options - `regex` — Pass a RegExp to only mangle certain names - - `ignore_quoted` – Only mangle unquoted property names - - `debug` – Mangle names with the original name still present. Defaults to `false`. + - `keep_quoted` — Only mangle unquoted property names + - `debug` — Mangle names with the original name still present. Defaults to `false`. Pass an empty string to enable, or a non-empty string to set the suffix. We could add more options to `UglifyJS.minify` — if you need additional diff --git a/lib/propmangle.js b/lib/propmangle.js index d480d3da..efb31cc1 100644 --- a/lib/propmangle.js +++ b/lib/propmangle.js @@ -72,7 +72,7 @@ function mangle_properties(ast, options) { builtins: false, cache: null, debug: false, - ignore_quoted: false, + keep_quoted: false, only_cache: false, regex: null, reserved: null, @@ -90,12 +90,12 @@ function mangle_properties(ast, options) { } var regex = options.regex; - var ignore_quoted = options.ignore_quoted; + var keep_quoted = options.keep_quoted; // note debug is either false (disabled), or a string of the debug suffix to use (enabled). // note debug may be enabled as an empty string, which is falsey. Also treat passing 'true' // the same as passing an empty string. - var debug = (options.debug !== false); + var debug = options.debug !== false; var debug_name_suffix; if (debug) { debug_name_suffix = (options.debug === true ? "" : options.debug); @@ -103,12 +103,12 @@ function mangle_properties(ast, options) { var names_to_mangle = []; var unmangleable = []; - var ignored = {}; + var to_keep = {}; // step 1: find candidates to mangle ast.walk(new TreeWalker(function(node){ if (node instanceof AST_ObjectKeyVal) { - add(node.key, ignore_quoted && node.quote); + add(node.key, keep_quoted && node.quote); } else if (node instanceof AST_ObjectProperty) { // setter or getter, since KeyVal is handled above @@ -118,14 +118,14 @@ function mangle_properties(ast, options) { add(node.property); } else if (node instanceof AST_Sub) { - addStrings(node.property, ignore_quoted); + addStrings(node.property, keep_quoted); } })); // step 2: transform the tree, renaming properties return ast.transform(new TreeTransformer(function(node){ if (node instanceof AST_ObjectKeyVal) { - if (!(ignore_quoted && node.quote)) + if (!(keep_quoted && node.quote)) node.key = mangle(node.key); } else if (node instanceof AST_ObjectProperty) { @@ -136,7 +136,7 @@ function mangle_properties(ast, options) { node.property = mangle(node.property); } else if (node instanceof AST_Sub) { - if (!ignore_quoted) + if (!keep_quoted) node.property = mangleStrings(node.property); } // else if (node instanceof AST_String) { @@ -166,16 +166,16 @@ function mangle_properties(ast, options) { } function should_mangle(name) { - if (ignore_quoted && name in ignored) return false; + if (keep_quoted && name in to_keep) return false; if (regex && !regex.test(name)) return false; if (reserved.indexOf(name) >= 0) return false; return cache.props.has(name) || names_to_mangle.indexOf(name) >= 0; } - function add(name, ignore) { - if (ignore) { - ignored[name] = true; + function add(name, keep) { + if (keep) { + to_keep[name] = true; return; } @@ -198,19 +198,19 @@ function mangle_properties(ast, options) { // debug mode: use a prefix and suffix to preserve readability, e.g. o.foo -> o._$foo$NNN_. var debug_mangled = "_$" + name + "$" + debug_name_suffix + "_"; - if (can_mangle(debug_mangled) && !(ignore_quoted && debug_mangled in ignored)) { + if (can_mangle(debug_mangled) && !(keep_quoted && debug_mangled in to_keep)) { mangled = debug_mangled; } } // either debug mode is off, or it is on and we could not use the mangled name if (!mangled) { - // note can_mangle() does not check if the name collides with the 'ignored' set - // (filled with quoted properties when ignore_quoted set). Make sure we add this + // Note: `can_mangle()` does not check if the name collides with the `to_keep` set + // (filled with quoted properties when `keep_quoted` is set). Make sure we add this // check so we don't collide with a quoted name. do { mangled = base54(++cache.cname); - } while (!can_mangle(mangled) || (ignore_quoted && mangled in ignored)); + } while (!can_mangle(mangled) || keep_quoted && mangled in to_keep); } cache.props.set(name, mangled); @@ -218,7 +218,7 @@ function mangle_properties(ast, options) { return mangled; } - function addStrings(node, ignore) { + function addStrings(node, keep) { var out = {}; try { (function walk(node){ @@ -228,7 +228,7 @@ function mangle_properties(ast, options) { return true; } if (node instanceof AST_String) { - add(node.value, ignore); + add(node.value, keep); return true; } if (node instanceof AST_Conditional) { @@ -260,5 +260,4 @@ function mangle_properties(ast, options) { return node; })); } - } diff --git a/test/compress/issue-1321.js b/test/compress/issue-1321.js index 7449d3e2..dcbfde64 100644 --- a/test/compress/issue-1321.js +++ b/test/compress/issue-1321.js @@ -1,6 +1,6 @@ issue_1321_no_debug: { mangle_props = { - ignore_quoted: true + keep_quoted: true } input: { var x = {}; @@ -19,7 +19,7 @@ issue_1321_no_debug: { issue_1321_debug: { mangle_props = { - ignore_quoted: true, + keep_quoted: true, debug: "" } input: { @@ -39,7 +39,7 @@ issue_1321_debug: { issue_1321_with_quoted: { mangle_props = { - ignore_quoted: false + keep_quoted: false } input: { var x = {}; diff --git a/test/compress/properties.js b/test/compress/properties.js index b2c201d7..3e06dc1e 100644 --- a/test/compress/properties.js +++ b/test/compress/properties.js @@ -125,7 +125,7 @@ evaluate_string_length: { mangle_properties: { mangle_props = { - ignore_quoted: false + keep_quoted: false }; input: { a["foo"] = "bar"; @@ -148,7 +148,7 @@ mangle_unquoted_properties: { properties: false } mangle_props = { - ignore_quoted: true + keep_quoted: true } beautify = { beautify: false, @@ -233,12 +233,12 @@ mangle_debug_suffix: { } } -mangle_debug_suffix_ignore_quoted: { +mangle_debug_suffix_keep_quoted: { options = { properties: false } mangle_props = { - ignore_quoted: true, + keep_quoted: true, debug: "XYZ", reserved: [] } diff --git a/test/mocha/minify.js b/test/mocha/minify.js index e332dbef..18840a58 100644 --- a/test/mocha/minify.js +++ b/test/mocha/minify.js @@ -53,7 +53,7 @@ describe("minify", function() { }, mangle: { properties: { - ignore_quoted: true + keep_quoted: true } }, output: {