diff --git a/lib/compress.js b/lib/compress.js index 05c7fb65b0e..91e3c388f97 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4220,7 +4220,7 @@ merge(Compressor.prototype, { } } if (is_func_expr(fn) && !fn.is_generator && !fn.async) { - var def, scope, value; + var def, value, scope, level = -1; if (compressor.option("inline") && simple_args && !fn.uses_arguments @@ -4233,9 +4233,9 @@ merge(Compressor.prototype, { && fn.is_constant_expression(exp.scope)) && !self.pure && !fn.contains_this() - && (scope = can_flatten_args(fn)) + && can_flatten_args(fn) && (value = flatten_body(stat))) { - var expressions = flatten_args(fn, scope); + var expressions = flatten_args(fn); expressions.push(value.clone(true)); return make_sequence(self, expressions).optimize(compressor); } @@ -4268,10 +4268,9 @@ merge(Compressor.prototype, { return self; function can_flatten_args(fn) { - var scope, level = 0; var catches = Object.create(null); do { - scope = compressor.parent(level++); + scope = compressor.parent(++level); if (scope instanceof AST_SymbolRef) { scope = scope.fixed_value(); } else if (scope instanceof AST_Catch) { @@ -4288,10 +4287,10 @@ merge(Compressor.prototype, { && !catches[arg.name] && !identifier_atom(arg.name) && !scope.var_names()[arg.name]; - }) && scope; + }); } - function flatten_args(fn, scope) { + function flatten_args(fn) { var decls = []; var expressions = []; for (var len = fn.argnames.length, i = len; --i >= 0;) { @@ -4325,8 +4324,7 @@ merge(Compressor.prototype, { expressions.push(self.args[i]); } if (decls.length) { - for (i = 0; compressor.parent(i) !== scope;) i++; - i = scope.body.indexOf(compressor.parent(i - 1)) + 1; + i = scope.body.indexOf(compressor.parent(level - 1)) + 1; scope.body.splice(i, 0, make_node(AST_Var, fn, { definitions: decls })); diff --git a/package.json b/package.json index c83a7953264..71e0b421932 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "homepage": "https://github.com/mishoo/UglifyJS2/tree/harmony", "author": "Mihai Bazon (http://lisperator.net/)", "license": "BSD-2-Clause", - "version": "3.3.0", + "version": "3.3.1", "engines": { "node": ">=0.8.0" }, diff --git a/test/benchmark.js b/test/benchmark.js index 8b20ec13c78..593836061dd 100644 --- a/test/benchmark.js +++ b/test/benchmark.js @@ -21,6 +21,7 @@ var urls = [ "http://builds.emberjs.com/tags/v2.11.0/ember.prod.js", "https://cdn.jsdelivr.net/lodash/4.17.4/lodash.js", "https://cdnjs.cloudflare.com/ajax/libs/d3/4.5.0/d3.js", + "https://raw.githubusercontent.com/kangax/html-minifier/v3.5.7/dist/htmlminifier.js", ]; var results = {}; var remaining = 2 * urls.length; diff --git a/test/compress/functions.js b/test/compress/functions.js index 0b564aa5238..1ba9b3dcbd2 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -1548,3 +1548,27 @@ issue_2647_3: { expect_stdout: "PASS" node_version: ">=4" } + +recursive_inline: { + options = { + inline: true, + reduce_funcs: true, + reduce_vars: true, + sequences: true, + toplevel: true, + unused: true, + } + input: { + function f() { + h(); + } + function g(a) { + a(); + } + function h(b) { + g(); + if (b) x(); + } + } + expect: {} +}