avoid substitution of global variables (#1557)

- unless `toplevel` is enabled
- global `const` works as before
This commit is contained in:
Alex Lam S.L
2017-03-07 03:11:03 +08:00
committed by GitHub
parent 3ac2421932
commit d787d70127
3 changed files with 306 additions and 93 deletions

View File

@@ -223,6 +223,7 @@ merge(Compressor.prototype, {
AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan){ AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan){
var reduce_vars = rescan && compressor.option("reduce_vars"); var reduce_vars = rescan && compressor.option("reduce_vars");
var toplevel = compressor.option("toplevel");
var ie8 = !compressor.option("screw_ie8"); var ie8 = !compressor.option("screw_ie8");
var safe_ids = []; var safe_ids = [];
push(); push();
@@ -334,7 +335,11 @@ merge(Compressor.prototype, {
} }
function reset_def(def) { function reset_def(def) {
if (toplevel || !def.global || def.orig[0] instanceof AST_SymbolConst) {
def.fixed = undefined; def.fixed = undefined;
} else {
def.fixed = false;
}
def.references = []; def.references = [];
def.should_replace = undefined; def.should_replace = undefined;
} }

View File

@@ -123,6 +123,7 @@ dead_code_const_annotation: {
conditionals : true, conditionals : true,
evaluate : true, evaluate : true,
reduce_vars : true, reduce_vars : true,
toplevel : true,
}; };
input: { input: {
var unused; var unused;
@@ -172,6 +173,7 @@ dead_code_const_annotation_complex_scope: {
conditionals : true, conditionals : true,
evaluate : true, evaluate : true,
reduce_vars : true, reduce_vars : true,
toplevel : true,
}; };
input: { input: {
var unused_var; var unused_var;

View File

@@ -6,6 +6,7 @@ reduce_vars: {
C : 0 C : 0
}, },
reduce_vars : true, reduce_vars : true,
toplevel : true,
unused : true unused : true
} }
input: { input: {
@@ -452,6 +453,7 @@ multi_def_2: {
reduce_vars: true, reduce_vars: true,
} }
input: { input: {
function f(){
if (code == 16) if (code == 16)
var bitsLength = 2, bitsOffset = 3, what = len; var bitsLength = 2, bitsOffset = 3, what = len;
else if (code == 17) else if (code == 17)
@@ -460,7 +462,9 @@ multi_def_2: {
var bitsLength = 7, bitsOffset = 11, what = (len = 0); var bitsLength = 7, bitsOffset = 11, what = (len = 0);
var repeatLength = this.getBits(bitsLength) + bitsOffset; var repeatLength = this.getBits(bitsLength) + bitsOffset;
} }
}
expect: { expect: {
function f(){
if (16 == code) if (16 == code)
var bitsLength = 2, bitsOffset = 3, what = len; var bitsLength = 2, bitsOffset = 3, what = len;
else if (17 == code) else if (17 == code)
@@ -469,6 +473,7 @@ multi_def_2: {
var bitsLength = 7, bitsOffset = 11, what = (len = 0); var bitsLength = 7, bitsOffset = 11, what = (len = 0);
var repeatLength = this.getBits(bitsLength) + bitsOffset; var repeatLength = this.getBits(bitsLength) + bitsOffset;
} }
}
} }
use_before_var: { use_before_var: {
@@ -477,13 +482,17 @@ use_before_var: {
reduce_vars: true, reduce_vars: true,
} }
input: { input: {
function f(){
console.log(t); console.log(t);
var t = 1; var t = 1;
} }
}
expect: { expect: {
function f(){
console.log(t); console.log(t);
var t = 1; var t = 1;
} }
}
} }
inner_var_if: { inner_var_if: {
@@ -492,23 +501,21 @@ inner_var_if: {
reduce_vars: true, reduce_vars: true,
} }
input: { input: {
function f(){ function f(a){
return 0; if (a)
}
if (f())
var t = 1; var t = 1;
if (!t) if (!t)
console.log(t); console.log(t);
} }
}
expect: { expect: {
function f(){ function f(a){
return 0; if (a)
}
if (f())
var t = 1; var t = 1;
if (!t) if (!t)
console.log(t); console.log(t);
} }
}
} }
inner_var_label: { inner_var_label: {
@@ -517,25 +524,23 @@ inner_var_label: {
reduce_vars: true, reduce_vars: true,
} }
input: { input: {
function f(){ function f(a){
return 1;
}
l: { l: {
if (f()) break l; if (a) break l;
var t = 1; var t = 1;
} }
console.log(t); console.log(t);
} }
}
expect: { expect: {
function f(){ function f(a){
return 1;
}
l: { l: {
if (f()) break l; if (a) break l;
var t = 1; var t = 1;
} }
console.log(t); console.log(t);
} }
}
} }
inner_var_for: { inner_var_for: {
@@ -544,15 +549,18 @@ inner_var_for: {
reduce_vars: true, reduce_vars: true,
} }
input: { input: {
function f() {
var a = 1; var a = 1;
x(a, b, d); x(a, b, d);
for (var b = 2, c = 3; x(a, b, c, d); x(a, b, c, d)) { for (var b = 2, c = 3; x(a, b, c, d); x(a, b, c, d)) {
var d = 4, e = 5; var d = 4, e = 5;
x(a, b, c, d, e); x(a, b, c, d, e);
} }
x(a, b, c, d, e) x(a, b, c, d, e);
}
} }
expect: { expect: {
function f() {
var a = 1; var a = 1;
x(1, b, d); x(1, b, d);
for (var b = 2, c = 3; x(1, b, 3, d); x(1, b, 3, d)) { for (var b = 2, c = 3; x(1, b, 3, d); x(1, b, 3, d)) {
@@ -561,6 +569,7 @@ inner_var_for: {
} }
x(1, b, 3, d, e); x(1, b, 3, d, e);
} }
}
} }
inner_var_for_in_1: { inner_var_for_in_1: {
@@ -569,6 +578,7 @@ inner_var_for_in_1: {
reduce_vars: true, reduce_vars: true,
} }
input: { input: {
function f() {
var a = 1, b = 2; var a = 1, b = 2;
for (b in (function() { for (b in (function() {
return x(a, b, c); return x(a, b, c);
@@ -578,7 +588,9 @@ inner_var_for_in_1: {
} }
x(a, b, c, d); x(a, b, c, d);
} }
}
expect: { expect: {
function f() {
var a = 1, b = 2; var a = 1, b = 2;
for (b in (function() { for (b in (function() {
return x(1, b, c); return x(1, b, c);
@@ -588,6 +600,7 @@ inner_var_for_in_1: {
} }
x(1, b, c, d); x(1, b, c, d);
} }
}
} }
inner_var_for_in_2: { inner_var_for_in_2: {
@@ -596,13 +609,17 @@ inner_var_for_in_2: {
reduce_vars: true, reduce_vars: true,
} }
input: { input: {
function f() {
for (var long_name in {}) for (var long_name in {})
console.log(long_name); console.log(long_name);
} }
}
expect: { expect: {
function f() {
for (var long_name in {}) for (var long_name in {})
console.log(long_name); console.log(long_name);
} }
}
} }
inner_var_catch: { inner_var_catch: {
@@ -611,6 +628,7 @@ inner_var_catch: {
reduce_vars: true, reduce_vars: true,
} }
input: { input: {
function f() {
try { try {
a(); a();
} catch (e) { } catch (e) {
@@ -618,7 +636,9 @@ inner_var_catch: {
} }
console.log(b); console.log(b);
} }
}
expect: { expect: {
function f() {
try { try {
a(); a();
} catch (e) { } catch (e) {
@@ -626,6 +646,7 @@ inner_var_catch: {
} }
console.log(b); console.log(b);
} }
}
} }
issue_1533_1: { issue_1533_1: {
@@ -634,15 +655,19 @@ issue_1533_1: {
reduce_vars: true, reduce_vars: true,
} }
input: { input: {
function f() {
var id = ""; var id = "";
for (id in {break: "me"}) for (id in {break: "me"})
console.log(id); console.log(id);
} }
}
expect: { expect: {
function f() {
var id = ""; var id = "";
for (id in {break: "me"}) for (id in {break: "me"})
console.log(id); console.log(id);
} }
}
} }
issue_1533_2: { issue_1533_2: {
@@ -651,15 +676,196 @@ issue_1533_2: {
reduce_vars: true, reduce_vars: true,
} }
input: { input: {
function f() {
var id = ""; var id = "";
for (var id in {break: "me"}) for (var id in {break: "me"})
console.log(id); console.log(id);
console.log(id); console.log(id);
} }
}
expect: { expect: {
function f() {
var id = ""; var id = "";
for (var id in {break: "me"}) for (var id in {break: "me"})
console.log(id); console.log(id);
console.log(id); console.log(id);
} }
}
}
toplevel_on: {
options = {
evaluate: true,
reduce_vars: true,
toplevel:true,
unused: true,
}
input: {
var x = 3;
console.log(x);
}
expect: {
console.log(3);
}
}
toplevel_off: {
options = {
evaluate: true,
reduce_vars: true,
toplevel:false,
unused: true,
}
input: {
var x = 3;
console.log(x);
}
expect: {
var x = 3;
console.log(x);
}
}
toplevel_on_loops_1: {
options = {
evaluate: true,
loops: true,
reduce_vars: true,
toplevel:true,
unused: true,
}
input: {
function bar() {
console.log("bar:", --x);
}
var x = 3;
do
bar();
while (x);
}
expect: {
function bar() {
console.log("bar:", --x);
}
var x = 3;
do
bar();
while (x);
}
}
toplevel_off_loops_1: {
options = {
evaluate: true,
loops: true,
reduce_vars: true,
toplevel:false,
unused: true,
}
input: {
function bar() {
console.log("bar:", --x);
}
var x = 3;
do
bar();
while (x);
}
expect: {
function bar() {
console.log("bar:", --x);
}
var x = 3;
do
bar();
while (x);
}
}
toplevel_on_loops_2: {
options = {
evaluate: true,
loops: true,
reduce_vars: true,
toplevel:true,
unused: true,
}
input: {
function bar() {
console.log("bar:");
}
var x = 3;
do
bar();
while (x);
}
expect: {
function bar() {
console.log("bar:");
}
for (;;) bar();
}
}
toplevel_off_loops_2: {
options = {
evaluate: true,
loops: true,
reduce_vars: true,
toplevel:false,
unused: true,
}
input: {
function bar() {
console.log("bar:");
}
var x = 3;
do
bar();
while (x);
}
expect: {
function bar() {
console.log("bar:");
}
var x = 3;
do
bar();
while (x);
}
}
toplevel_on_loops_3: {
options = {
evaluate: true,
loops: true,
reduce_vars: true,
toplevel:true,
unused: true,
}
input: {
var x = 3;
while (x) bar();
}
expect: {
for (;;) bar();
}
}
toplevel_off_loops_3: {
options = {
evaluate: true,
loops: true,
reduce_vars: true,
toplevel:false,
unused: true,
}
input: {
var x = 3;
while (x) bar();
}
expect: {
var x = 3;
for (;x;) bar();
}
} }