Skip to content

Commit

Permalink
harmony-v3.0.27
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Jul 30, 2017
2 parents d600c78 + 69cb459 commit f54ab16
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 139 deletions.
9 changes: 8 additions & 1 deletion .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@

**Uglify version (`uglifyjs -V`)**

**JavaScript input** <!-- ideally as small as possible -->
**JavaScript input**

<!--
A complete parsable JS program exhibiting the issue with
UglifyJS alone - without third party tools or libraries.
Ideally the input should be as small as possible.
Post a link to a gist if necessary.
-->

**The `uglifyjs` CLI command executed or `minify()` options used.**

Expand Down
13 changes: 11 additions & 2 deletions lib/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function minify(files, options) {
set_shorthand("keep_fnames", options, [ "compress", "mangle" ]);
set_shorthand("toplevel", options, [ "compress", "mangle" ]);
set_shorthand("warnings", options, [ "compress" ]);
var quoted_props;
if (options.mangle) {
options.mangle = defaults(options.mangle, {
cache: options.nameCache && (options.nameCache.vars || {}),
Expand All @@ -82,11 +83,16 @@ function minify(files, options) {
safari10: false,
toplevel: false,
}, true);
if (options.nameCache && options.mangle.properties) {
if (options.mangle.properties) {
if (typeof options.mangle.properties != "object") {
options.mangle.properties = {};
}
if (!("cache" in options.mangle.properties)) {
if (options.mangle.properties.keep_quoted) {
quoted_props = options.mangle.properties.reserved;
if (!Array.isArray(quoted_props)) quoted_props = [];
options.mangle.properties.reserved = quoted_props;
}
if (options.nameCache && !("cache" in options.mangle.properties)) {
options.mangle.properties.cache = options.nameCache.props || {};
}
}
Expand Down Expand Up @@ -129,6 +135,9 @@ function minify(files, options) {
}
toplevel = options.parse.toplevel;
}
if (quoted_props) {
reserve_quoted_keys(toplevel, quoted_props);
}
if (options.wrap) {
toplevel = toplevel.wrap_commonjs(options.wrap);
}
Expand Down
105 changes: 38 additions & 67 deletions lib/propmangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,36 @@ function find_builtins(reserved) {
}
}

function reserve_quoted_keys(ast, reserved) {
function add(name) {
push_uniq(reserved, name);
}

ast.walk(new TreeWalker(function(node) {
if (node instanceof AST_ObjectKeyVal && node.quote) {
add(node.key);
} else if (node instanceof AST_ObjectProperty && node.quote) {
add(node.key.name);
} else if (node instanceof AST_Sub) {
addStrings(node.property, add);
}
}));
}

function addStrings(node, add) {
node.walk(new TreeWalker(function(node) {
if (node instanceof AST_Sequence) {
addStrings(node.expressions[node.expressions.length - 1], add);
} else if (node instanceof AST_String) {
add(node.value);
} else if (node instanceof AST_Conditional) {
addStrings(node.consequent, add);
addStrings(node.alternative, add);
}
return true;
}));
}

function mangle_properties(ast, options) {
options = defaults(options, {
builtins: false,
Expand All @@ -109,7 +139,6 @@ function mangle_properties(ast, options) {
}

var regex = options.regex;
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'
Expand All @@ -122,15 +151,11 @@ function mangle_properties(ast, options) {

var names_to_mangle = [];
var unmangleable = [];
var to_keep = {};

// step 1: find candidates to mangle
ast.walk(new TreeWalker(function(node){
if (node instanceof AST_ObjectKeyVal) {
add(node.key, keep_quoted && node.quote);
}
else if (node instanceof AST_ConciseMethod && node.key && node.key.name) {
add(node.key.name, keep_quoted && node.quote);
add(node.key);
}
else if (node instanceof AST_ObjectProperty) {
// setter or getter, since KeyVal is handled above
Expand All @@ -140,20 +165,14 @@ function mangle_properties(ast, options) {
add(node.property);
}
else if (node instanceof AST_Sub) {
addStrings(node.property, keep_quoted);
addStrings(node.property, add);
}
}));

// step 2: transform the tree, renaming properties
return ast.transform(new TreeTransformer(function(node){
if (node instanceof AST_ObjectKeyVal) {
if (!(keep_quoted && node.quote))
node.key = mangle(node.key);
}
else if (node instanceof AST_ConciseMethod && node.name && node.key.name) {
if (!(keep_quoted && node.quote) && should_mangle(node.key.name)) {
node.key.name = mangle(node.key.name);
}
node.key = mangle(node.key);
}
else if (node instanceof AST_ObjectProperty) {
// setter or getter
Expand All @@ -162,22 +181,9 @@ function mangle_properties(ast, options) {
else if (node instanceof AST_Dot) {
node.property = mangle(node.property);
}
else if (node instanceof AST_Sub) {
if (!keep_quoted)
node.property = mangleStrings(node.property);
else if (!options.keep_quoted && node instanceof AST_Sub) {
node.property = mangleStrings(node.property);
}
// else if (node instanceof AST_String) {
// if (should_mangle(node.value)) {
// AST_Node.warn(
// "Found \"{prop}\" property candidate for mangling in an arbitrary string [{file}:{line},{col}]", {
// file : node.start.file,
// line : node.start.line,
// col : node.start.col,
// prop : node.value
// }
// );
// }
// }
}));

// only function declarations after this line
Expand All @@ -193,19 +199,13 @@ function mangle_properties(ast, options) {
}

function should_mangle(name) {
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, keep) {
if (keep) {
to_keep[name] = true;
return;
}

function add(name) {
if (can_mangle(name))
push_uniq(names_to_mangle, name);

Expand All @@ -225,52 +225,23 @@ 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) && !(keep_quoted && debug_mangled in to_keep)) {
if (can_mangle(debug_mangled)) {
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 `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) || keep_quoted && mangled in to_keep);
} while (!can_mangle(mangled));
}

cache.props.set(name, mangled);
}
return mangled;
}

function addStrings(node, keep) {
var out = {};
try {
(function walk(node){
node.walk(new TreeWalker(function(node){
if (node instanceof AST_Sequence) {
walk(node.expressions[node.expressions.length - 1]);
return true;
}
if (node instanceof AST_String) {
add(node.value, keep);
return true;
}
if (node instanceof AST_Conditional) {
walk(node.consequent);
walk(node.alternative);
return true;
}
throw out;
}));
})(node);
} catch(ex) {
if (ex !== out) throw ex;
}
}

function mangleStrings(node) {
return node.transform(new TreeTransformer(function(node){
if (node instanceof AST_Sequence) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"homepage": "https://github.com/mishoo/UglifyJS2/tree/harmony",
"author": "Mihai Bazon <[email protected]> (http://lisperator.net/)",
"license": "BSD-2-Clause",
"version": "3.0.26",
"version": "3.0.27",
"engines": {
"node": ">=0.8.0"
},
Expand Down
38 changes: 22 additions & 16 deletions test/compress/issue-1321.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
issue_1321_no_debug: {
mangle_props = {
keep_quoted: true
mangle = {
properties: {
keep_quoted: true,
},
}
input: {
var x = {};
Expand All @@ -10,17 +12,19 @@ issue_1321_no_debug: {
}
expect: {
var x = {};
x.o = 1;
x["a"] = 2 * x.o;
console.log(x.o, x["a"]);
x.x = 1;
x["a"] = 2 * x.x;
console.log(x.x, x["a"]);
}
expect_stdout: true
}

issue_1321_debug: {
mangle_props = {
keep_quoted: true,
debug: ""
mangle = {
properties: {
debug: "",
keep_quoted: true,
},
}
input: {
var x = {};
Expand All @@ -30,16 +34,18 @@ issue_1321_debug: {
}
expect: {
var x = {};
x.o = 1;
x["_$foo$_"] = 2 * x.o;
console.log(x.o, x["_$foo$_"]);
x.x = 1;
x["_$foo$_"] = 2 * x.x;
console.log(x.x, x["_$foo$_"]);
}
expect_stdout: true
}

issue_1321_with_quoted: {
mangle_props = {
keep_quoted: false
mangle = {
properties: {
keep_quoted: false,
},
}
input: {
var x = {};
Expand All @@ -49,9 +55,9 @@ issue_1321_with_quoted: {
}
expect: {
var x = {};
x.o = 1;
x["x"] = 2 * x.o;
console.log(x.o, x["x"]);
x.x = 1;
x["o"] = 2 * x.x;
console.log(x.x, x["o"]);
}
expect_stdout: true
}
8 changes: 6 additions & 2 deletions test/compress/issue-1770.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mangle_props: {
mangle_props = {}
mangle = {
properties: true,
}
input: {
var obj = {
undefined: 1,
Expand Down Expand Up @@ -54,10 +56,12 @@ mangle_props: {
}

numeric_literal: {
mangle = {
properties: true,
}
beautify = {
beautify: true,
}
mangle_props = {}
input: {
var obj = {
0: 0,
Expand Down
12 changes: 8 additions & 4 deletions test/compress/issue-747.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
dont_reuse_prop: {
mangle_props = {
regex: /asd/
mangle = {
properties: {
regex: /asd/,
},
}
input: {
"aaaaaaaaaabbbbb";
Expand All @@ -20,8 +22,10 @@ dont_reuse_prop: {
}

unmangleable_props_should_always_be_reserved: {
mangle_props = {
regex: /asd/
mangle = {
properties: {
regex: /asd/,
},
}
input: {
"aaaaaaaaaabbbbb";
Expand Down
8 changes: 5 additions & 3 deletions test/compress/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,11 @@ concise_methods_with_various_property_names: {
}

concise_methods_and_mangle_props: {
mangle_props = {
regex: /_/
};
mangle = {
properties: {
regex: /_/,
},
}
input: {
function x() {
obj = {
Expand Down
Loading

0 comments on commit f54ab16

Please sign in to comment.