| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class VariableReferenceCheckTest extends CompilerTestCase { |
| 27 |
|
| 28 |
private static final String VARIABLE_RUN = |
| 29 |
"var a = 1; var b = 2; var c = a + b, d = c;"; |
| 30 |
|
| 31 |
private boolean enableAmbiguousFunctionCheck = false; |
| 32 |
|
| 33 |
@Override |
| 34 |
public CompilerOptions getOptions() { |
| 35 |
CompilerOptions options = super.getOptions(); |
| 36 |
if (enableAmbiguousFunctionCheck) { |
| 37 |
options.setWarningLevel( |
| 38 |
DiagnosticGroups.AMBIGUOUS_FUNCTION_DECL, CheckLevel.WARNING); |
| 39 |
} |
| 40 |
return options; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public CompilerPass getProcessor(Compiler compiler) { |
| 45 |
|
| 46 |
return new VariableReferenceCheck(compiler, CheckLevel.WARNING); |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public void setUp() throws Exception { |
| 51 |
super.setUp(); |
| 52 |
enableAmbiguousFunctionCheck = false; |
| 53 |
} |
| 54 |
|
| 55 |
public void testCorrectCode() { |
| 56 |
assertNoWarning("function foo(d) { (function() { d.foo(); }); d.bar(); } "); |
| 57 |
assertNoWarning("function foo() { bar(); } function bar() { foo(); } "); |
| 58 |
assertNoWarning("function f(d) { d = 3; }"); |
| 59 |
assertNoWarning(VARIABLE_RUN); |
| 60 |
assertNoWarning("function f() { " + VARIABLE_RUN + "}"); |
| 61 |
} |
| 62 |
|
| 63 |
public void testCorrectShadowing() { |
| 64 |
assertNoWarning(VARIABLE_RUN + "function f() { " + VARIABLE_RUN + "}"); |
| 65 |
} |
| 66 |
|
| 67 |
public void testCorrectRedeclare() { |
| 68 |
assertNoWarning( |
| 69 |
"function f() { if (1) { var a = 2; } else { var a = 3; } }"); |
| 70 |
} |
| 71 |
|
| 72 |
public void testCorrectRecursion() { |
| 73 |
assertNoWarning("function f() { var x = function() { x(); }; }"); |
| 74 |
} |
| 75 |
|
| 76 |
public void testCorrectCatch() { |
| 77 |
assertNoWarning("function f() { try { var x = 2; } catch (x) {} }"); |
| 78 |
} |
| 79 |
|
| 80 |
public void testRedeclare() { |
| 81 |
|
| 82 |
assertRedeclare("function f() { var a = 2; var a = 3; }"); |
| 83 |
assertRedeclare("function f(a) { var a = 2; }"); |
| 84 |
} |
| 85 |
|
| 86 |
public void testEarlyReference() { |
| 87 |
assertUndeclared("function f() { a = 2; var a = 3; }"); |
| 88 |
} |
| 89 |
|
| 90 |
public void testCorrectEarlyReference() { |
| 91 |
assertNoWarning("var goog = goog || {}"); |
| 92 |
assertNoWarning("function f() { a = 2; } var a = 2;"); |
| 93 |
} |
| 94 |
|
| 95 |
public void testUnreferencedBleedingFunction() { |
| 96 |
assertNoWarning("var x = function y() {}"); |
| 97 |
} |
| 98 |
|
| 99 |
public void testReferencedBleedingFunction() { |
| 100 |
assertNoWarning("var x = function y() { return y(); }"); |
| 101 |
} |
| 102 |
|
| 103 |
public void testDoubleDeclaration() { |
| 104 |
assertRedeclare("function x(y) { if (true) { var y; } }"); |
| 105 |
} |
| 106 |
|
| 107 |
public void testDoubleDeclaration2() { |
| 108 |
assertRedeclare("function x() { var y; if (true) { var y; } }"); |
| 109 |
} |
| 110 |
|
| 111 |
public void testHoistedFunction1() { |
| 112 |
enableAmbiguousFunctionCheck = true; |
| 113 |
assertNoWarning("f(); function f() {}"); |
| 114 |
} |
| 115 |
|
| 116 |
public void testHoistedFunction2() { |
| 117 |
enableAmbiguousFunctionCheck = true; |
| 118 |
assertNoWarning("function g() { f(); function f() {} }"); |
| 119 |
} |
| 120 |
|
| 121 |
public void testNonHoistedFunction() { |
| 122 |
enableAmbiguousFunctionCheck = true; |
| 123 |
assertUndeclared("if (true) { f(); function f() {} }"); |
| 124 |
} |
| 125 |
|
| 126 |
public void testNonHoistedFunction2() { |
| 127 |
enableAmbiguousFunctionCheck = true; |
| 128 |
assertNoWarning("if (false) { function f() {} f(); }"); |
| 129 |
} |
| 130 |
|
| 131 |
public void testNonHoistedFunction3() { |
| 132 |
enableAmbiguousFunctionCheck = true; |
| 133 |
assertNoWarning("function g() { if (false) { function f() {} f(); }}"); |
| 134 |
} |
| 135 |
|
| 136 |
public void testNonHoistedFunction4() { |
| 137 |
enableAmbiguousFunctionCheck = true; |
| 138 |
assertAmbiguous("if (false) { function f() {} } f();"); |
| 139 |
} |
| 140 |
|
| 141 |
public void testNonHoistedFunction5() { |
| 142 |
enableAmbiguousFunctionCheck = true; |
| 143 |
assertAmbiguous("function g() { if (false) { function f() {} } f(); }"); |
| 144 |
} |
| 145 |
|
| 146 |
public void testNonHoistedFunction6() { |
| 147 |
enableAmbiguousFunctionCheck = true; |
| 148 |
assertUndeclared("if (false) { f(); function f() {} }"); |
| 149 |
} |
| 150 |
|
| 151 |
public void testNonHoistedFunction7() { |
| 152 |
enableAmbiguousFunctionCheck = true; |
| 153 |
assertUndeclared("function g() { if (false) { f(); function f() {} }}"); |
| 154 |
} |
| 155 |
|
| 156 |
public void testNonHoistedRecursiveFunction1() { |
| 157 |
enableAmbiguousFunctionCheck = true; |
| 158 |
assertNoWarning("if (false) { function f() { f(); }}"); |
| 159 |
} |
| 160 |
|
| 161 |
public void testNonHoistedRecursiveFunction2() { |
| 162 |
enableAmbiguousFunctionCheck = true; |
| 163 |
assertNoWarning("function g() { if (false) { function f() { f(); }}}"); |
| 164 |
} |
| 165 |
|
| 166 |
public void testNonHoistedRecursiveFunction3() { |
| 167 |
enableAmbiguousFunctionCheck = true; |
| 168 |
assertNoWarning("function g() { if (false) { function f() { f(); g(); }}}"); |
| 169 |
} |
| 170 |
|
| 171 |
public void testNoWarnInExterns1() { |
| 172 |
|
| 173 |
String externs = |
| 174 |
"var google;" + |
| 175 |
"/** @suppress {duplicate} */ var google"; |
| 176 |
String code = ""; |
| 177 |
test(externs, code, code, null, null); |
| 178 |
} |
| 179 |
|
| 180 |
public void testNoWarnInExterns2() { |
| 181 |
|
| 182 |
String externs = |
| 183 |
"window;" + |
| 184 |
"var window;"; |
| 185 |
String code = ""; |
| 186 |
test(externs, code, code, null, null); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
private void assertRedeclare(String js) { |
| 193 |
testSame(js, VariableReferenceCheck.REDECLARED_VARIABLE); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private void assertUndeclared(String js) { |
| 200 |
testSame(js, VariableReferenceCheck.UNDECLARED_REFERENCE); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
private void assertAmbiguous(String js) { |
| 207 |
testSame(js, VariableReferenceCheck.AMBIGUOUS_FUNCTION_DECL); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
private void assertNoWarning(String js) { |
| 215 |
testSame(js); |
| 216 |
} |
| 217 |
} |