Respect quote style in object literals

The option added in fbbaa42ee5 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.
This commit is contained in:
Shrey Banga
2016-05-05 13:44:59 -07:00
committed by Richard van Velzen
parent 6c99816855
commit e645ba84cf
7 changed files with 158 additions and 17 deletions

View File

@@ -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)) {