Referencing a global is assumed to have side effects.

Close #550
This commit is contained in:
Mihai Bazon
2014-09-28 11:16:51 +03:00
parent f0c1a01bc2
commit 01d19b4b52
3 changed files with 30 additions and 1 deletions

View File

@@ -897,7 +897,9 @@ merge(Compressor.prototype, {
|| this.operator == "--"
|| this.expression.has_side_effects(compressor);
});
def(AST_SymbolRef, function(compressor){ return false });
def(AST_SymbolRef, function(compressor){
return this.global() && this.undeclared();
});
def(AST_Object, function(compressor){
for (var i = this.properties.length; --i >= 0;)
if (this.properties[i].has_side_effects(compressor))

View File

@@ -239,6 +239,7 @@ cond_7: {
evaluate : true
};
input: {
var x, y, z, a, b;
// compress these
if (y) {
x = 1+1;
@@ -281,6 +282,7 @@ cond_7: {
x = y ? 'foo' : 'fo';
}
expect: {
var x, y, z, a, b;
x = 2;
x = 2;
x = 'foo';
@@ -293,3 +295,20 @@ cond_7: {
}
}
cond_7_1: {
options = {
conditionals: true,
evaluate : true
};
input: {
// access to global should be assumed to have side effects
if (y) {
x = 1+1;
} else {
x = 2;
}
}
expect: {
x = (y, 2);
}
}

View File

@@ -91,9 +91,11 @@ make_sequences_4: {
lift_sequences_1: {
options = { sequences: true };
input: {
var foo, x, y, bar;
foo = !(x(), y(), bar());
}
expect: {
var foo, x, y, bar;
x(), y(), foo = !bar();
}
}
@@ -101,10 +103,12 @@ lift_sequences_1: {
lift_sequences_2: {
options = { sequences: true, evaluate: true };
input: {
var foo, bar;
foo.x = (foo = {}, 10);
bar = (bar = {}, 10);
}
expect: {
var foo, bar;
foo.x = (foo = {}, 10),
bar = {}, bar = 10;
}
@@ -113,9 +117,11 @@ lift_sequences_2: {
lift_sequences_3: {
options = { sequences: true, conditionals: true };
input: {
var x, foo, bar, baz;
x = (foo(), bar(), baz()) ? 10 : 20;
}
expect: {
var x, foo, bar, baz;
foo(), bar(), x = baz() ? 10 : 20;
}
}
@@ -123,9 +129,11 @@ lift_sequences_3: {
lift_sequences_4: {
options = { side_effects: true };
input: {
var x, foo, bar, baz;
x = (foo, bar, baz);
}
expect: {
var x, foo, bar, baz;
x = baz;
}
}