diff options
author | Shrey Banga <shrey@quip.com> | 2016-05-05 13:44:59 -0700 |
---|---|---|
committer | Richard van Velzen <rvanvelzen1@gmail.com> | 2016-06-19 21:13:31 +0200 |
commit | e645ba84cfc950183a222c2cce2ea6b94fe51226 (patch) | |
tree | 9ca5d7b1d55c36adb14f0468d4442f2a3a0aeb2d /lib | |
parent | 6c99816855b650c6804a67f4891c339a3e8970f4 (diff) | |
download | tracifyjs-e645ba84cfc950183a222c2cce2ea6b94fe51226.tar.gz tracifyjs-e645ba84cfc950183a222c2cce2ea6b94fe51226.zip |
Respect quote style in object literals
The option added in fbbaa42ee55a7f753f7cab9b1a905ccf73cf26d5 wasn't
being respected inside object literals, so quoted property names would
still be stripped out with this option.
This is mostly a corner-case, but useful when the output is passed to
something like the Closure compiler, where quoted property names can be
used to prevent mangling.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/output.js | 9 | ||||
-rw-r--r-- | lib/propmangle.js | 16 |
2 files changed, 18 insertions, 7 deletions
diff --git a/lib/output.js b/lib/output.js index 1fa9899f..10465e21 100644 --- a/lib/output.js +++ b/lib/output.js @@ -66,7 +66,8 @@ function OutputStream(options) { preserve_line : false, screw_ie8 : false, preamble : null, - quote_style : 0 + quote_style : 0, + keep_quoted_props: false }, true); var indentation = 0; @@ -1173,7 +1174,11 @@ function OutputStream(options) { && parseFloat(key) >= 0) { output.print(make_num(key)); } else if (RESERVED_WORDS(key) ? output.option("screw_ie8") : is_identifier_string(key)) { - output.print_name(key); + if (quote && output.option("keep_quoted_props")) { + output.print_string(key, quote); + } else { + output.print_name(key); + } } else { output.print_string(key, quote); } diff --git a/lib/propmangle.js b/lib/propmangle.js index 840bda91..08043d73 100644 --- a/lib/propmangle.js +++ b/lib/propmangle.js @@ -65,7 +65,8 @@ function mangle_properties(ast, options) { reserved : null, cache : null, only_cache : false, - regex : null + regex : null, + ignore_quoted : false }); var reserved = options.reserved; @@ -81,6 +82,7 @@ function mangle_properties(ast, options) { } var regex = options.regex; + var ignore_quoted = options.ignore_quoted; var names_to_mangle = []; var unmangleable = []; @@ -88,7 +90,8 @@ function mangle_properties(ast, options) { // step 1: find candidates to mangle ast.walk(new TreeWalker(function(node){ if (node instanceof AST_ObjectKeyVal) { - add(node.key); + if (!(ignore_quoted && node.quote)) + add(node.key); } else if (node instanceof AST_ObjectProperty) { // setter or getter, since KeyVal is handled above @@ -101,7 +104,8 @@ function mangle_properties(ast, options) { } else if (node instanceof AST_Sub) { if (this.parent() instanceof AST_Assign) { - addStrings(node.property); + if (!ignore_quoted) + addStrings(node.property); } } })); @@ -109,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 @@ -119,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)) { |