| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.javascript.jscomp.CheckLevel; |
| 20 |
|
| 21 |
public class CheckSideEffectsTest extends CompilerTestCase { |
| 22 |
public CheckSideEffectsTest() { |
| 23 |
this.parseTypeInfo = true; |
| 24 |
allowExternsChanges(true); |
| 25 |
} |
| 26 |
|
| 27 |
@Override |
| 28 |
protected int getNumRepetitions() { |
| 29 |
return 1; |
| 30 |
} |
| 31 |
|
| 32 |
@Override |
| 33 |
protected CompilerPass getProcessor(Compiler compiler) { |
| 34 |
return new CheckSideEffects(compiler, CheckLevel.WARNING, true); |
| 35 |
} |
| 36 |
|
| 37 |
@Override |
| 38 |
public void test(String js, String expected, DiagnosticType warning) { |
| 39 |
test(js, expected, null, warning); |
| 40 |
} |
| 41 |
|
| 42 |
public void test(String js, DiagnosticType warning) { |
| 43 |
test(js, js, null, warning); |
| 44 |
} |
| 45 |
|
| 46 |
final DiagnosticType e = CheckSideEffects.USELESS_CODE_ERROR; |
| 47 |
final DiagnosticType ok = null; |
| 48 |
|
| 49 |
public void testUselessCode() { |
| 50 |
test("function f(x) { if(x) return; }", ok); |
| 51 |
test("function f(x) { if(x); }", "function f(x) { if(x); }", e); |
| 52 |
|
| 53 |
test("if(x) x = y;", ok); |
| 54 |
test("if(x) x == bar();", "if(x) JSCOMPILER_PRESERVE(x == bar());", e); |
| 55 |
|
| 56 |
test("x = 3;", ok); |
| 57 |
test("x == 3;", "JSCOMPILER_PRESERVE(x == 3);", e); |
| 58 |
|
| 59 |
test("var x = 'test'", ok); |
| 60 |
test("var x = 'test'\n'str'", |
| 61 |
"var x = 'test'\nJSCOMPILER_PRESERVE('str')", e); |
| 62 |
|
| 63 |
test("", ok); |
| 64 |
test("foo();;;;bar();;;;", ok); |
| 65 |
|
| 66 |
test("var a, b; a = 5, b = 6", ok); |
| 67 |
test("var a, b; a = 5, b == 6", |
| 68 |
"var a, b; a = 5, JSCOMPILER_PRESERVE(b == 6)", e); |
| 69 |
test("var a, b; a = (5, 6)", |
| 70 |
"var a, b; a = (JSCOMPILER_PRESERVE(5), 6)", e); |
| 71 |
test("var a, b; a = (bar(), 6, 7)", |
| 72 |
"var a, b; a = (bar(), JSCOMPILER_PRESERVE(6), 7)", e); |
| 73 |
test("var a, b; a = (bar(), bar(), 7, 8)", |
| 74 |
"var a, b; a = (bar(), bar(), JSCOMPILER_PRESERVE(7), 8)", e); |
| 75 |
test("var a, b; a = (b = 7, 6)", ok); |
| 76 |
test("function x(){}\nfunction f(a, b){}\nf(1,(x(), 2));", ok); |
| 77 |
test("function x(){}\nfunction f(a, b){}\nf(1,(2, 3));", |
| 78 |
"function x(){}\nfunction f(a, b){}\n" + |
| 79 |
"f(1,(JSCOMPILER_PRESERVE(2), 3));", e); |
| 80 |
} |
| 81 |
|
| 82 |
public void testUselessCodeInFor() { |
| 83 |
test("for(var x = 0; x < 100; x++) { foo(x) }", ok); |
| 84 |
test("for(; true; ) { bar() }", ok); |
| 85 |
test("for(foo(); true; foo()) { bar() }", ok); |
| 86 |
test("for(void 0; true; foo()) { bar() }", |
| 87 |
"for(JSCOMPILER_PRESERVE(void 0); true; foo()) { bar() }", e); |
| 88 |
test("for(foo(); true; void 0) { bar() }", |
| 89 |
"for(foo(); true; JSCOMPILER_PRESERVE(void 0)) { bar() }", e); |
| 90 |
test("for(foo(); true; (1, bar())) { bar() }", |
| 91 |
"for(foo(); true; (JSCOMPILER_PRESERVE(1), bar())) { bar() }", e); |
| 92 |
|
| 93 |
test("for(foo in bar) { foo() }", ok); |
| 94 |
test("for (i = 0; el = el.previousSibling; i++) {}", ok); |
| 95 |
test("for (i = 0; el = el.previousSibling; i++);", ok); |
| 96 |
} |
| 97 |
|
| 98 |
public void testTypeAnnotations() { |
| 99 |
test("x;", "JSCOMPILER_PRESERVE(x);", e); |
| 100 |
test("a.b.c.d;", "JSCOMPILER_PRESERVE(a.b.c.d);", e); |
| 101 |
test("/** @type Number */ a.b.c.d;", ok); |
| 102 |
test("if (true) { /** @type Number */ a.b.c.d; }", ok); |
| 103 |
|
| 104 |
test("function A() { this.foo; }", |
| 105 |
"function A() { JSCOMPILER_PRESERVE(this.foo); }", e); |
| 106 |
test("function A() { /** @type Number */ this.foo; }", ok); |
| 107 |
} |
| 108 |
|
| 109 |
public void testJSDocComments() { |
| 110 |
test("function A() { /** This is a JsDoc comment */ this.foo; }", ok); |
| 111 |
test("function A() { /* This is a normal comment */ this.foo; }", |
| 112 |
"function A() { " + |
| 113 |
" /* This is a normal comment */ JSCOMPILER_PRESERVE(this.foo); }", e); |
| 114 |
} |
| 115 |
|
| 116 |
public void testIssue80() { |
| 117 |
test("(0, eval)('alert');", ok); |
| 118 |
test("(0, foo)('alert');", "(JSCOMPILER_PRESERVE(0), foo)('alert');", e); |
| 119 |
} |
| 120 |
|
| 121 |
public void testIsue504() { |
| 122 |
test("void f();", "JSCOMPILER_PRESERVE(void f());", null, e, |
| 123 |
"Suspicious code. The result of the 'void' operator is not being used."); |
| 124 |
} |
| 125 |
} |