rename ignore_quoted to keep_quoted

This commit is contained in:
alexlamsl 2017-04-15 22:10:30 +08:00
parent bd116f128e
commit 663042d5aa
5 changed files with 31 additions and 32 deletions

View File

@ -76,7 +76,7 @@ The available options are:
`debug` Add debug prefix and suffix. `debug` Add debug prefix and suffix.
`domprops` Mangle property names that overlaps `domprops` Mangle property names that overlaps
with DOM properties. with DOM properties.
`ignore_quoted` Only mangle unquoted properies. `keep_quoted` Only mangle unquoted properies.
`regex` Only mangle matched property names. `regex` Only mangle matched property names.
`reserved` List of names that should not be mangled. `reserved` List of names that should not be mangled.
-b, --beautify [options] Beautify output/specify output options: -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 Using the name cache is not necessary if you compress all your files in a
single call to UglifyJS. 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`) 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 so that it is not mangled throughout the entire script even when used in an
unquoted style (`o.foo`). Example: 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); var o={foo:1,a:3};o.foo+=o.a,console.log(o.foo);
``` ```
@ -745,8 +745,8 @@ Other options:
##### mangle.properties options ##### mangle.properties options
- `regex` — Pass a RegExp to only mangle certain names - `regex` — Pass a RegExp to only mangle certain names
- `ignore_quoted` Only mangle unquoted property names - `keep_quoted` — Only mangle unquoted property names
- `debug` Mangle names with the original name still present. Defaults to `false`. - `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. 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 We could add more options to `UglifyJS.minify` — if you need additional

View File

@ -72,7 +72,7 @@ function mangle_properties(ast, options) {
builtins: false, builtins: false,
cache: null, cache: null,
debug: false, debug: false,
ignore_quoted: false, keep_quoted: false,
only_cache: false, only_cache: false,
regex: null, regex: null,
reserved: null, reserved: null,
@ -90,12 +90,12 @@ function mangle_properties(ast, options) {
} }
var regex = options.regex; 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 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' // note debug may be enabled as an empty string, which is falsey. Also treat passing 'true'
// the same as passing an empty string. // the same as passing an empty string.
var debug = (options.debug !== false); var debug = options.debug !== false;
var debug_name_suffix; var debug_name_suffix;
if (debug) { if (debug) {
debug_name_suffix = (options.debug === true ? "" : options.debug); debug_name_suffix = (options.debug === true ? "" : options.debug);
@ -103,12 +103,12 @@ function mangle_properties(ast, options) {
var names_to_mangle = []; var names_to_mangle = [];
var unmangleable = []; var unmangleable = [];
var ignored = {}; var to_keep = {};
// step 1: find candidates to mangle // step 1: find candidates to mangle
ast.walk(new TreeWalker(function(node){ ast.walk(new TreeWalker(function(node){
if (node instanceof AST_ObjectKeyVal) { 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) { else if (node instanceof AST_ObjectProperty) {
// setter or getter, since KeyVal is handled above // setter or getter, since KeyVal is handled above
@ -118,14 +118,14 @@ function mangle_properties(ast, options) {
add(node.property); add(node.property);
} }
else if (node instanceof AST_Sub) { else if (node instanceof AST_Sub) {
addStrings(node.property, ignore_quoted); addStrings(node.property, keep_quoted);
} }
})); }));
// 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) {
if (!(ignore_quoted && node.quote)) if (!(keep_quoted && node.quote))
node.key = mangle(node.key); node.key = mangle(node.key);
} }
else if (node instanceof AST_ObjectProperty) { else if (node instanceof AST_ObjectProperty) {
@ -136,7 +136,7 @@ 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) {
if (!ignore_quoted) if (!keep_quoted)
node.property = mangleStrings(node.property); node.property = mangleStrings(node.property);
} }
// else if (node instanceof AST_String) { // else if (node instanceof AST_String) {
@ -166,16 +166,16 @@ function mangle_properties(ast, options) {
} }
function should_mangle(name) { 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 (regex && !regex.test(name)) return false;
if (reserved.indexOf(name) >= 0) return false; if (reserved.indexOf(name) >= 0) return false;
return cache.props.has(name) return cache.props.has(name)
|| names_to_mangle.indexOf(name) >= 0; || names_to_mangle.indexOf(name) >= 0;
} }
function add(name, ignore) { function add(name, keep) {
if (ignore) { if (keep) {
ignored[name] = true; to_keep[name] = true;
return; 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_. // debug mode: use a prefix and suffix to preserve readability, e.g. o.foo -> o._$foo$NNN_.
var debug_mangled = "_$" + name + "$" + debug_name_suffix + "_"; 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; mangled = debug_mangled;
} }
} }
// either debug mode is off, or it is on and we could not use the mangled name // either debug mode is off, or it is on and we could not use the mangled name
if (!mangled) { if (!mangled) {
// note can_mangle() does not check if the name collides with the 'ignored' set // Note: `can_mangle()` does not check if the name collides with the `to_keep` set
// (filled with quoted properties when ignore_quoted set). Make sure we add this // (filled with quoted properties when `keep_quoted` is set). Make sure we add this
// check so we don't collide with a quoted name. // check so we don't collide with a quoted name.
do { do {
mangled = base54(++cache.cname); 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); cache.props.set(name, mangled);
@ -218,7 +218,7 @@ function mangle_properties(ast, options) {
return mangled; return mangled;
} }
function addStrings(node, ignore) { function addStrings(node, keep) {
var out = {}; var out = {};
try { try {
(function walk(node){ (function walk(node){
@ -228,7 +228,7 @@ function mangle_properties(ast, options) {
return true; return true;
} }
if (node instanceof AST_String) { if (node instanceof AST_String) {
add(node.value, ignore); add(node.value, keep);
return true; return true;
} }
if (node instanceof AST_Conditional) { if (node instanceof AST_Conditional) {
@ -260,5 +260,4 @@ function mangle_properties(ast, options) {
return node; return node;
})); }));
} }
} }

View File

@ -1,6 +1,6 @@
issue_1321_no_debug: { issue_1321_no_debug: {
mangle_props = { mangle_props = {
ignore_quoted: true keep_quoted: true
} }
input: { input: {
var x = {}; var x = {};
@ -19,7 +19,7 @@ issue_1321_no_debug: {
issue_1321_debug: { issue_1321_debug: {
mangle_props = { mangle_props = {
ignore_quoted: true, keep_quoted: true,
debug: "" debug: ""
} }
input: { input: {
@ -39,7 +39,7 @@ issue_1321_debug: {
issue_1321_with_quoted: { issue_1321_with_quoted: {
mangle_props = { mangle_props = {
ignore_quoted: false keep_quoted: false
} }
input: { input: {
var x = {}; var x = {};

View File

@ -125,7 +125,7 @@ evaluate_string_length: {
mangle_properties: { mangle_properties: {
mangle_props = { mangle_props = {
ignore_quoted: false keep_quoted: false
}; };
input: { input: {
a["foo"] = "bar"; a["foo"] = "bar";
@ -148,7 +148,7 @@ mangle_unquoted_properties: {
properties: false properties: false
} }
mangle_props = { mangle_props = {
ignore_quoted: true keep_quoted: true
} }
beautify = { beautify = {
beautify: false, beautify: false,
@ -233,12 +233,12 @@ mangle_debug_suffix: {
} }
} }
mangle_debug_suffix_ignore_quoted: { mangle_debug_suffix_keep_quoted: {
options = { options = {
properties: false properties: false
} }
mangle_props = { mangle_props = {
ignore_quoted: true, keep_quoted: true,
debug: "XYZ", debug: "XYZ",
reserved: [] reserved: []
} }

View File

@ -53,7 +53,7 @@ describe("minify", function() {
}, },
mangle: { mangle: {
properties: { properties: {
ignore_quoted: true keep_quoted: true
} }
}, },
output: { output: {