Skip to content

Commit

Permalink
make collapse_vars consistent with toplevel (#1608)
Browse files Browse the repository at this point in the history
fixes #1605
  • Loading branch information
alexlamsl committed Mar 16, 2017
1 parent a80b228 commit 5ae04b3
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/compress.js
Expand Up @@ -521,6 +521,7 @@ merge(Compressor.prototype, {

var self = compressor.self();
var var_defs_removed = false;
var toplevel = compressor.option("toplevel");
for (var stat_index = statements.length; --stat_index >= 0;) {
var stat = statements[stat_index];
if (stat instanceof AST_Definitions) continue;
Expand Down Expand Up @@ -558,7 +559,8 @@ merge(Compressor.prototype, {

// Only interested in cases with just one reference to the variable.
var def = self.find_variable && self.find_variable(var_name);
if (!def || !def.references || def.references.length !== 1 || var_name == "arguments") {
if (!def || !def.references || def.references.length !== 1
|| var_name == "arguments" || (!toplevel && def.global)) {
side_effects_encountered = true;
continue;
}
Expand Down
47 changes: 46 additions & 1 deletion test/compress/collapse_vars.js
Expand Up @@ -1152,7 +1152,8 @@ collapse_vars_arguments: {
options = {
collapse_vars:true, sequences:true, properties:true, dead_code:true, conditionals:true,
comparisons:true, evaluate:true, booleans:true, loops:true, unused:true, hoist_funs:true,
keep_fargs:true, if_return:true, join_vars:true, cascade:true, side_effects:true
keep_fargs:true, if_return:true, join_vars:true, cascade:true, side_effects:true,
toplevel:true
}
input: {
var outer = function() {
Expand Down Expand Up @@ -1335,6 +1336,7 @@ issue_1537: {
issue_1562: {
options = {
collapse_vars: true,
toplevel: true,
}
input: {
var v = 1, B = 2;
Expand Down Expand Up @@ -1363,3 +1365,46 @@ issue_1562: {
for (; f(z + 2) ;) bar(30);
}
}

issue_1605_1: {
options = {
collapse_vars: true,
toplevel: false,
}
input: {
function foo(x) {
var y = x;
return y;
}
var o = new Object;
o.p = 1;
}
expect: {
function foo(x) {
return x;
}
var o = new Object;
o.p = 1;
}
}

issue_1605_2: {
options = {
collapse_vars: true,
toplevel: "vars",
}
input: {
function foo(x) {
var y = x;
return y;
}
var o = new Object;
o.p = 1;
}
expect: {
function foo(x) {
return x;
}
(new Object).p = 1;
}
}
1 change: 1 addition & 0 deletions test/compress/issue-973.js
Expand Up @@ -50,6 +50,7 @@ this_binding_conditionals: {
this_binding_collapse_vars: {
options = {
collapse_vars: true,
toplevel: true,
};
input: {
var c = a; c();
Expand Down
2 changes: 1 addition & 1 deletion test/mocha/cli.js
Expand Up @@ -152,7 +152,7 @@ describe("bin/uglifyjs", function () {
});
});
it("Should process inline source map", function(done) {
var command = uglifyjscmd + ' test/input/issue-520/input.js -cm toplevel --in-source-map inline --source-map-inline';
var command = uglifyjscmd + ' test/input/issue-520/input.js -mc toplevel --in-source-map inline --source-map-inline';

exec(command, function (err, stdout) {
if (err) throw err;
Expand Down
1 change: 1 addition & 0 deletions test/mocha/minify.js
Expand Up @@ -78,6 +78,7 @@ describe("minify", function() {
});
it("Should process inline source map", function() {
var code = Uglify.minify("./test/input/issue-520/input.js", {
compress: { toplevel: true },
inSourceMap: "inline",
sourceMapInline: true
}).code + "\n";
Expand Down

0 comments on commit 5ae04b3

Please sign in to comment.