});
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;
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;
&& (left.operator == "++" || left.operator == "--")) {
left = left.expression;
} else left = null;
- if (!left || is_lhs_read_only(left)) {
+ if (!left || is_lhs_read_only(left) || left.has_side_effects(compressor)) {
expressions[++i] = cdr;
continue;
}
}
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"
+}
}
expect_stdout: "PASS"
}
+
+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();
+ }
+}
}
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"
+}