Fix --mangle-props and --mangle-props=unquoted
Fixes: #1247 Fix --mangle-props and --name-cache inconsistency. AST_Dot and AST_Sub properties are now mangled by --mangle-props without regard to being used in an assignment statement. Note: if --mangle-props is used then *all* javascript files used must be uglified with the same mangle options. Fix the ignore_quoted=true mangle option, also known as `--mangle-props=unquoted`. If a given property is quoted anywhere it will not be mangled in any quoted or non-quoted context.
This commit is contained in:
@@ -86,27 +86,22 @@ function mangle_properties(ast, options) {
|
|||||||
|
|
||||||
var names_to_mangle = [];
|
var names_to_mangle = [];
|
||||||
var unmangleable = [];
|
var unmangleable = [];
|
||||||
|
var ignored = {};
|
||||||
|
|
||||||
// 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) {
|
||||||
if (!(ignore_quoted && node.quote))
|
add(node.key, ignore_quoted && node.quote);
|
||||||
add(node.key);
|
|
||||||
}
|
}
|
||||||
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
|
||||||
add(node.key.name);
|
add(node.key.name);
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_Dot) {
|
else if (node instanceof AST_Dot) {
|
||||||
if (this.parent() instanceof AST_Assign) {
|
add(node.property);
|
||||||
add(node.property);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (node instanceof AST_Sub) {
|
else if (node instanceof AST_Sub) {
|
||||||
if (this.parent() instanceof AST_Assign) {
|
addStrings(node.property, ignore_quoted);
|
||||||
if (!ignore_quoted)
|
|
||||||
addStrings(node.property);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -154,13 +149,19 @@ function mangle_properties(ast, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function should_mangle(name) {
|
function should_mangle(name) {
|
||||||
|
if (ignore_quoted && name in ignored) 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) {
|
function add(name, ignore) {
|
||||||
|
if (ignore) {
|
||||||
|
ignored[name] = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (can_mangle(name))
|
if (can_mangle(name))
|
||||||
push_uniq(names_to_mangle, name);
|
push_uniq(names_to_mangle, name);
|
||||||
|
|
||||||
@@ -184,7 +185,7 @@ function mangle_properties(ast, options) {
|
|||||||
return mangled;
|
return mangled;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addStrings(node) {
|
function addStrings(node, ignore) {
|
||||||
var out = {};
|
var out = {};
|
||||||
try {
|
try {
|
||||||
(function walk(node){
|
(function walk(node){
|
||||||
@@ -194,7 +195,7 @@ function mangle_properties(ast, options) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_String) {
|
if (node instanceof AST_String) {
|
||||||
add(node.value);
|
add(node.value, ignore);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (node instanceof AST_Conditional) {
|
if (node instanceof AST_Conditional) {
|
||||||
|
|||||||
@@ -82,15 +82,22 @@ mangle_properties: {
|
|||||||
a["foo"] = "bar";
|
a["foo"] = "bar";
|
||||||
a.color = "red";
|
a.color = "red";
|
||||||
x = {"bar": 10};
|
x = {"bar": 10};
|
||||||
|
a.run(x.bar, a.foo);
|
||||||
|
a['run']({color: "blue", foo: "baz"});
|
||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
a["a"] = "bar";
|
a["a"] = "bar";
|
||||||
a.b = "red";
|
a.b = "red";
|
||||||
x = {c: 10};
|
x = {c: 10};
|
||||||
|
a.d(x.c, a.a);
|
||||||
|
a['d']({b: "blue", a: "baz"});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mangle_unquoted_properties: {
|
mangle_unquoted_properties: {
|
||||||
|
options = {
|
||||||
|
properties: false
|
||||||
|
}
|
||||||
mangle_props = {
|
mangle_props = {
|
||||||
ignore_quoted: true
|
ignore_quoted: true
|
||||||
}
|
}
|
||||||
@@ -100,27 +107,37 @@ mangle_unquoted_properties: {
|
|||||||
keep_quoted_props: true,
|
keep_quoted_props: true,
|
||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
|
a.top = 1;
|
||||||
function f1() {
|
function f1() {
|
||||||
a["foo"] = "bar";
|
a["foo"] = "bar";
|
||||||
a.color = "red";
|
a.color = "red";
|
||||||
x = {"bar": 10};
|
a.stuff = 2;
|
||||||
|
x = {"bar": 10, size: 7};
|
||||||
|
a.size = 9;
|
||||||
}
|
}
|
||||||
function f2() {
|
function f2() {
|
||||||
a.foo = "bar";
|
a.foo = "bar";
|
||||||
a['color'] = "red";
|
a['color'] = "red";
|
||||||
x = {bar: 10};
|
x = {bar: 10, size: 7};
|
||||||
|
a.size = 9;
|
||||||
|
a.stuff = 3;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
|
a.a = 1;
|
||||||
function f1() {
|
function f1() {
|
||||||
a["foo"] = "bar";
|
a["foo"] = "bar";
|
||||||
a.a = "red";
|
a.color = "red";
|
||||||
x = {"bar": 10};
|
a.b = 2;
|
||||||
|
x = {"bar": 10, c: 7};
|
||||||
|
a.c = 9;
|
||||||
}
|
}
|
||||||
function f2() {
|
function f2() {
|
||||||
a.b = "bar";
|
a.foo = "bar";
|
||||||
a['color'] = "red";
|
a['color'] = "red";
|
||||||
x = {c: 10};
|
x = {bar: 10, c: 7};
|
||||||
|
a.c = 9;
|
||||||
|
a.b = 3;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user