invert reduce_vars tracking flag (#1519)

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.

Also fix use case with omitted `options` when calling `Compressor()`.

fixes #1516
This commit is contained in:
Alex Lam S.L
2017-03-01 04:12:10 +08:00
committed by GitHub
parent b34fa11a13
commit f5cbe19b75
3 changed files with 18 additions and 9 deletions

View File

@@ -621,7 +621,7 @@ function uglify(ast, options, mangle) {
// Compression // Compression
uAST.figure_out_scope(); uAST.figure_out_scope();
uAST = uAST.transform(UglifyJS.Compressor(options)); uAST = UglifyJS.Compressor(options).compress(uAST);
// Mangling (optional) // Mangling (optional)
if (mangle) { if (mangle) {
@@ -865,7 +865,7 @@ toplevel.figure_out_scope()
Like this: Like this:
```javascript ```javascript
var compressor = UglifyJS.Compressor(options); var compressor = UglifyJS.Compressor(options);
var compressed_ast = toplevel.transform(compressor); var compressed_ast = compressor.compress(toplevel);
``` ```
The `options` can be missing. Available options are discussed above in The `options` can be missing. Available options are discussed above in

View File

@@ -61,7 +61,7 @@ function Compressor(options, false_by_default) {
booleans : !false_by_default, booleans : !false_by_default,
loops : !false_by_default, loops : !false_by_default,
unused : !false_by_default, unused : !false_by_default,
toplevel : !!options["top_retain"], toplevel : !!(options && options["top_retain"]),
top_retain : null, top_retain : null,
hoist_funs : !false_by_default, hoist_funs : !false_by_default,
keep_fargs : true, keep_fargs : true,
@@ -70,7 +70,7 @@ function Compressor(options, false_by_default) {
if_return : !false_by_default, if_return : !false_by_default,
join_vars : !false_by_default, join_vars : !false_by_default,
collapse_vars : !false_by_default, collapse_vars : !false_by_default,
reduce_vars : false, reduce_vars : !false_by_default,
cascade : !false_by_default, cascade : !false_by_default,
side_effects : !false_by_default, side_effects : !false_by_default,
pure_getters : false, pure_getters : false,
@@ -187,8 +187,8 @@ merge(Compressor.prototype, {
if (node instanceof AST_SymbolRef) { if (node instanceof AST_SymbolRef) {
var d = node.definition(); var d = node.definition();
d.references.push(node); d.references.push(node);
if (!d.modified && (d.orig.length > 1 || isModified(node, 0))) { if (d.fixed && (d.orig.length > 1 || isModified(node, 0))) {
d.modified = true; d.fixed = false;
} }
} }
if (node instanceof AST_Call && node.expression instanceof AST_Function) { if (node instanceof AST_Call && node.expression instanceof AST_Function) {
@@ -205,7 +205,7 @@ merge(Compressor.prototype, {
this.walk(tw); this.walk(tw);
function reset_def(def) { function reset_def(def) {
def.modified = false; def.fixed = true;
def.references = []; def.references = [];
def.should_replace = undefined; def.should_replace = undefined;
if (unsafe && def.init) { if (unsafe && def.init) {
@@ -1263,7 +1263,7 @@ merge(Compressor.prototype, {
this._evaluating = true; this._evaluating = true;
try { try {
var d = this.definition(); 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 (compressor.option("unsafe")) {
if (d.init._evaluated === undefined) { if (d.init._evaluated === undefined) {
d.init._evaluated = ev(d.init, compressor); d.init._evaluated = ev(d.init, compressor);
@@ -3046,7 +3046,7 @@ merge(Compressor.prototype, {
} }
if (compressor.option("evaluate") && compressor.option("reduce_vars")) { if (compressor.option("evaluate") && compressor.option("reduce_vars")) {
var d = self.definition(); var d = self.definition();
if (!d.modified && d.init) { if (d.fixed && d.init) {
if (d.should_replace === undefined) { if (d.should_replace === undefined) {
var init = d.init.evaluate(compressor); var init = d.init.evaluate(compressor);
if (init.length > 1) { if (init.length > 1) {

View File

@@ -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());
assert.strictEqual(ast.print_to_string(), "function f(a){for(var i=0;i<a;i++)console.log(i)}");
});
})
}); });