Skip to content

Commit

Permalink
invert reduce_vars tracking flag
Browse files Browse the repository at this point in the history
Modules like webpack and grunt-contrib-uglify still uses `ast.transform(compressor)` before `Compressor.compress(ast)` was introduced.

Workaround this compatibility issue by deactivating `reduce_vars` in such case.

fixes mishoo#1516
  • Loading branch information
alexlamsl committed Feb 28, 2017
1 parent 320984c commit 0bea291
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function Compressor(options, false_by_default) {
if_return : !false_by_default,
join_vars : !false_by_default,
collapse_vars : !false_by_default,
reduce_vars : false,
reduce_vars : !false_by_default,
cascade : !false_by_default,
side_effects : !false_by_default,
pure_getters : false,
Expand Down Expand Up @@ -187,8 +187,8 @@ merge(Compressor.prototype, {
if (node instanceof AST_SymbolRef) {
var d = node.definition();
d.references.push(node);
if (!d.modified && (d.orig.length > 1 || isModified(node, 0))) {
d.modified = true;
if (d.fixed && (d.orig.length > 1 || isModified(node, 0))) {
d.fixed = false;
}
}
if (node instanceof AST_Call && node.expression instanceof AST_Function) {
Expand All @@ -205,7 +205,7 @@ merge(Compressor.prototype, {
this.walk(tw);

function reset_def(def) {
def.modified = false;
def.fixed = true;
def.references = [];
def.should_replace = undefined;
if (unsafe && def.init) {
Expand Down Expand Up @@ -1258,7 +1258,7 @@ merge(Compressor.prototype, {
this._evaluating = true;
try {
var d = this.definition();
if (compressor.option("reduce_vars") && !d.modified && d.init) {
if (compressor.option("reduce_vars") && d.fixed && d.init) {
if (compressor.option("unsafe")) {
if (d.init._evaluated === undefined) {
d.init._evaluated = ev(d.init, compressor);
Expand Down Expand Up @@ -3041,7 +3041,7 @@ merge(Compressor.prototype, {
}
if (compressor.option("evaluate") && compressor.option("reduce_vars")) {
var d = self.definition();
if (!d.modified && d.init) {
if (d.fixed && d.init) {
if (d.should_replace === undefined) {
var init = d.init.evaluate(compressor);
if (init.length > 1) {
Expand Down
9 changes: 9 additions & 0 deletions test/mocha/minify.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,13 @@ describe("minify", function() {
});
});

describe("Compressor", function() {
it("should be backward compatible with ast.transform(compressor)", function() {
var ast = Uglify.parse("function f(a){for(var i=0;i<a;i++)console.log(i)}");
ast.figure_out_scope();
ast = ast.transform(Uglify.Compressor({ warnings: false }));
assert.strictEqual(ast.print_to_string(), "function f(a){for(var i=0;i<a;i++)console.log(i)}");
});
})

});

0 comments on commit 0bea291

Please sign in to comment.