diff --git a/lib/compress.js b/lib/compress.js index 22db761c..4bf3006d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -579,6 +579,7 @@ merge(Compressor.prototype, { } function is_lhs_read_only(lhs) { + if (lhs instanceof AST_This) return true; if (lhs instanceof AST_SymbolRef) return lhs.definition().orig[0] instanceof AST_SymbolLambda; if (lhs instanceof AST_PropAccess) { lhs = lhs.expression; @@ -782,7 +783,7 @@ merge(Compressor.prototype, { while (candidates.length > 0) { var candidate = candidates.pop(); var lhs = get_lhs(candidate); - if (!lhs || is_lhs_read_only(lhs)) continue; + if (!lhs || is_lhs_read_only(lhs) || lhs.has_side_effects(compressor)) continue; // Locate symbols which may execute code outside of scanning range var lvalues = get_lvalues(candidate); if (lhs instanceof AST_SymbolRef) lvalues[lhs.name] = false; @@ -3710,7 +3711,10 @@ merge(Compressor.prototype, { && (left.operator == "++" || left.operator == "--")) { left = left.expression; } else left = null; - if (!left || is_lhs_read_only(left) || is_ref_of(left, AST_SymbolConst)) { + if (!left + || is_lhs_read_only(left) + || left.has_side_effects(compressor) + || is_ref_of(left, AST_SymbolConst)) { expressions[++i] = cdr; continue; } diff --git a/lib/output.js b/lib/output.js index 8919a392..339faf9e 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1398,7 +1398,7 @@ function OutputStream(options) { self.expression.print(output); if (self instanceof AST_New && !need_constructor_parens(self, output)) return; - if (self.expression instanceof AST_Lambda) { + if (self.expression instanceof AST_Call || self.expression instanceof AST_Lambda) { output.add_mapping(self.start); } output.with_parens(function(){ diff --git a/package.json b/package.json index 383b78cb..381756d3 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.1.0", + "version": "3.1.1", "engines": { "node": ">=0.8.0" }, diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 0bdf8624..55fc1753 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -2658,3 +2658,70 @@ issue_2298: { } expect_stdout: "PASS" } + +issue_2313_1: { + options = { + collapse_vars: true, + conditionals: true, + } + input: { + var a = 0, b = 0; + var foo = { + get c() { + a++; + return 42; + }, + set c(c) { + b++; + }, + d: function() { + this.c++; + if (this.c) console.log(a, b); + } + } + foo.d(); + } + expect: { + var a = 0, b = 0; + var foo = { + get c() { + a++; + return 42; + }, + set c(c) { + b++; + }, + d: function() { + this.c++; + this.c && console.log(a, b); + } + } + foo.d(); + } + expect_stdout: "2 1" +} + +issue_2313_2: { + options = { + collapse_vars: true, + } + input: { + var c = 0; + !function a() { + a && c++; + var a = 0; + a && c++; + }(); + console.log(c); + } + expect: { + var c = 0; + !function a() { + a && c++; + var a = 0; + a && c++; + }(); + console.log(c); + } + expect_stdout: "0" +} diff --git a/test/compress/pure_getters.js b/test/compress/pure_getters.js index d7d3f381..b6d3880b 100644 --- a/test/compress/pure_getters.js +++ b/test/compress/pure_getters.js @@ -458,3 +458,217 @@ issue_2265_4: { } expect: {} } + +issue_2313_1: { + options = { + cascade: true, + conditionals: true, + pure_getters: "strict", + sequences: true, + side_effects: true, + } + input: { + function x() { + console.log(1); + return { + y: function() { + console.log(2); + return { + z: 0 + }; + } + }; + } + x().y().z++; + if (x().y().z) { + console.log(3); + } + } + expect: { + function x() { + return console.log(1), { + y: function() { + return console.log(2), { + z: 0 + }; + } + }; + } + x().y().z++, + x().y().z && console.log(3); + } + expect_stdout: [ + "1", + "2", + "1", + "2", + ] +} + +issue_2313_2: { + options = { + cascade: true, + conditionals: true, + pure_getters: true, + sequences: true, + side_effects: true, + } + input: { + function x() { + console.log(1); + return { + y: function() { + console.log(2); + return { + z: 0 + }; + } + }; + } + x().y().z++; + if (x().y().z) { + console.log(3); + } + } + expect: { + function x() { + return console.log(1), { + y: function() { + return console.log(2), { + z: 0 + }; + } + }; + } + x().y().z++, + x().y().z && console.log(3); + } + expect_stdout: [ + "1", + "2", + "1", + "2", + ] +} + +issue_2313_3: { + options = { + collapse_vars: true, + conditionals: true, + pure_getters: "strict", + } + input: { + function x() { + console.log(1); + return { + y: function() { + console.log(2); + return { + z: 0 + }; + } + }; + } + x().y().z++; + if (x().y().z) { + console.log(3); + } + } + expect: { + function x() { + console.log(1); + return { + y: function() { + console.log(2); + return { + z: 0 + }; + } + }; + } + x().y().z++; + x().y().z && console.log(3); + } + expect_stdout: [ + "1", + "2", + "1", + "2", + ] +} + +issue_2313_4: { + options = { + collapse_vars: true, + conditionals: true, + pure_getters: true, + } + input: { + function x() { + console.log(1); + return { + y: function() { + console.log(2); + return { + z: 0 + }; + } + }; + } + x().y().z++; + if (x().y().z) { + console.log(3); + } + } + expect: { + function x() { + console.log(1); + return { + y: function() { + console.log(2); + return { + z: 0 + }; + } + }; + } + x().y().z++; + x().y().z && console.log(3); + } + expect_stdout: [ + "1", + "2", + "1", + "2", + ] +} + +issue_2313_5: { + options = { + pure_getters: "strict", + side_effects: true, + } + input: { + x().y++; + x().y; + } + expect: { + x().y++; + x().y; + } +} + +issue_2313_6: { + options = { + pure_getters: true, + side_effects: true, + } + input: { + x().y++; + x().y; + } + expect: { + x().y++; + x(); + } +} diff --git a/test/compress/sequences.js b/test/compress/sequences.js index 829c10fe..d65f22a6 100644 --- a/test/compress/sequences.js +++ b/test/compress/sequences.js @@ -763,3 +763,44 @@ issue_2062: { } expect_stdout: "1" } + +issue_2313: { + options = { + cascade: true, + sequences: true, + side_effects: true, + } + input: { + var a = 0, b = 0; + var foo = { + get c() { + a++; + return 42; + }, + set c(c) { + b++; + }, + d: function() { + this.c++; + if (this.c) console.log(a, b); + } + } + foo.d(); + } + expect: { + var a = 0, b = 0; + var foo = { + get c() { + return a++, 42; + }, + set c(c) { + b++; + }, + d: function() { + if (this.c++, this.c) console.log(a, b); + } + } + foo.d(); + } + expect_stdout: "2 1" +} diff --git a/test/input/issue-2310/input.js b/test/input/issue-2310/input.js new file mode 100644 index 00000000..ea7c1f33 --- /dev/null +++ b/test/input/issue-2310/input.js @@ -0,0 +1,10 @@ +function foo() { + return function() { + console.log("PASS"); + }; +} + +(function() { + var f = foo(); + f(); +})(); diff --git a/test/mocha/cli.js b/test/mocha/cli.js index 40cb7044..4acf03d6 100644 --- a/test/mocha/cli.js +++ b/test/mocha/cli.js @@ -666,6 +666,25 @@ describe("bin/uglifyjs", function () { return JSON.stringify(map).replace(/"/g, '\\"'); } }); + it("Should include function calls in source map", function(done) { + var command = [ + uglifyjscmd, + "test/input/issue-2310/input.js", + "-c", + "--source-map", "url=inline", + ].join(" "); + + exec(command, function(err, stdout, stderr) { + if (err) throw err; + + assert.strictEqual(stdout, [ + 'function foo(){return function(){console.log("PASS")}}foo()();', + "//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInRlc3QvaW5wdXQvaXNzdWUtMjMxMC9pbnB1dC5qcyJdLCJuYW1lcyI6WyJmb28iLCJjb25zb2xlIiwibG9nIiwiZiJdLCJtYXBwaW5ncyI6IkFBQUEsU0FBU0EsTUFDTCxPQUFPLFdBQ0hDLFFBQVFDLElBQUksU0FLUkYsS0FDUkcifQ==", + "" + ].join("\n")); + done(); + }); + }); it("Should dump AST as JSON", function(done) { var command = uglifyjscmd + " test/input/global_defs/simple.js -mco ast"; exec(command, function (err, stdout) {