| 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.common.collect.ImmutableList; |
| 20 |
import com.google.common.collect.Lists; |
| 21 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 22 |
import com.google.javascript.rhino.Node; |
| 23 |
|
| 24 |
public class VarCheckTest extends CompilerTestCase { |
| 25 |
private static final String EXTERNS = "var window; function alert() {}"; |
| 26 |
|
| 27 |
private CheckLevel strictModuleDepErrorLevel; |
| 28 |
private boolean sanityCheck = false; |
| 29 |
|
| 30 |
private CheckLevel externValidationErrorLevel; |
| 31 |
|
| 32 |
private CompilerPass testSetupPass; |
| 33 |
|
| 34 |
public VarCheckTest() { |
| 35 |
super(EXTERNS); |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
protected void setUp() throws Exception { |
| 40 |
super.setUp(); |
| 41 |
|
| 42 |
super.allowExternsChanges(true); |
| 43 |
super.enableAstValidation(true); |
| 44 |
strictModuleDepErrorLevel = CheckLevel.OFF; |
| 45 |
externValidationErrorLevel = null; |
| 46 |
sanityCheck = false; |
| 47 |
testSetupPass = null; |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
protected CompilerOptions getOptions() { |
| 52 |
CompilerOptions options = super.getOptions(); |
| 53 |
options.setWarningLevel(DiagnosticGroups.STRICT_MODULE_DEP_CHECK, |
| 54 |
strictModuleDepErrorLevel); |
| 55 |
if (externValidationErrorLevel != null) { |
| 56 |
options.setWarningLevel(DiagnosticGroups.EXTERNS_VALIDATION, |
| 57 |
externValidationErrorLevel); |
| 58 |
} |
| 59 |
return options; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
protected CompilerPass getProcessor(final Compiler compiler) { |
| 64 |
if (!sanityCheck) { |
| 65 |
return new CompilerPass() { |
| 66 |
@Override public void process(Node externs, Node root) { |
| 67 |
if (testSetupPass != null) { |
| 68 |
testSetupPass.process(externs, root); |
| 69 |
} |
| 70 |
new VarCheck(compiler, false).process(externs, root); |
| 71 |
if (!compiler.hasErrors()) { |
| 72 |
new VarCheck(compiler, true).process(externs, root); |
| 73 |
} |
| 74 |
} |
| 75 |
}; |
| 76 |
} |
| 77 |
return new VarCheck(compiler, sanityCheck); |
| 78 |
} |
| 79 |
|
| 80 |
@Override |
| 81 |
protected int getNumRepetitions() { |
| 82 |
|
| 83 |
return 1; |
| 84 |
} |
| 85 |
|
| 86 |
public void testBreak() { |
| 87 |
testSame("a: while(1) break a;"); |
| 88 |
} |
| 89 |
|
| 90 |
public void testContinue() { |
| 91 |
testSame("a: while(1) continue a;"); |
| 92 |
} |
| 93 |
|
| 94 |
public void testReferencedVarNotDefined() { |
| 95 |
test("x = 0;", null, VarCheck.UNDEFINED_VAR_ERROR); |
| 96 |
} |
| 97 |
|
| 98 |
public void testReferencedVarDefined1() { |
| 99 |
testSame("var x, y; x=1;"); |
| 100 |
} |
| 101 |
|
| 102 |
public void testReferencedVarDefined2() { |
| 103 |
testSame("var x; function y() {x=1;}"); |
| 104 |
} |
| 105 |
|
| 106 |
public void testReferencedVarsExternallyDefined() { |
| 107 |
testSame("var x = window; alert(x);"); |
| 108 |
} |
| 109 |
|
| 110 |
public void testMultiplyDeclaredVars1() { |
| 111 |
test("var x = 1; var x = 2;", null, |
| 112 |
SyntacticScopeCreator.VAR_MULTIPLY_DECLARED_ERROR); |
| 113 |
} |
| 114 |
|
| 115 |
public void testMultiplyDeclaredVars2() { |
| 116 |
test("var y; try { y=1 } catch (x) {}" + |
| 117 |
"try { y=1 } catch (x) {}", |
| 118 |
"var y;try{y=1}catch(x){}try{y=1}catch(x){}"); |
| 119 |
} |
| 120 |
|
| 121 |
public void testMultiplyDeclaredVars3() { |
| 122 |
test("try { var x = 1; x *=2; } catch (x) {}", null, |
| 123 |
SyntacticScopeCreator.VAR_MULTIPLY_DECLARED_ERROR); |
| 124 |
} |
| 125 |
|
| 126 |
public void testMultiplyDeclaredVars4() { |
| 127 |
testSame("x;", "var x = 1; var x = 2;", |
| 128 |
SyntacticScopeCreator.VAR_MULTIPLY_DECLARED_ERROR, true); |
| 129 |
} |
| 130 |
|
| 131 |
public void testVarReferenceInExterns() { |
| 132 |
testSame("asdf;", "var asdf;", |
| 133 |
VarCheck.NAME_REFERENCE_IN_EXTERNS_ERROR); |
| 134 |
} |
| 135 |
|
| 136 |
public void testCallInExterns() { |
| 137 |
testSame("yz();", "function yz() {}", |
| 138 |
VarCheck.NAME_REFERENCE_IN_EXTERNS_ERROR); |
| 139 |
} |
| 140 |
|
| 141 |
public void testPropReferenceInExterns1() { |
| 142 |
testSame("asdf.foo;", "var asdf;", |
| 143 |
VarCheck.UNDEFINED_EXTERN_VAR_ERROR); |
| 144 |
} |
| 145 |
|
| 146 |
public void testPropReferenceInExterns2() { |
| 147 |
testSame("asdf.foo;", "", |
| 148 |
VarCheck.UNDEFINED_VAR_ERROR, true); |
| 149 |
} |
| 150 |
|
| 151 |
public void testPropReferenceInExterns3() { |
| 152 |
testSame("asdf.foo;", "var asdf;", |
| 153 |
VarCheck.UNDEFINED_EXTERN_VAR_ERROR); |
| 154 |
|
| 155 |
externValidationErrorLevel = CheckLevel.ERROR; |
| 156 |
test( |
| 157 |
"asdf.foo;", "var asdf;", "", |
| 158 |
VarCheck.UNDEFINED_EXTERN_VAR_ERROR, null); |
| 159 |
|
| 160 |
externValidationErrorLevel = CheckLevel.OFF; |
| 161 |
test("asdf.foo;", "var asdf;", "var asdf;", null, null); |
| 162 |
} |
| 163 |
|
| 164 |
public void testVarInWithBlock() { |
| 165 |
test("var a = {b:5}; with (a){b;}", null, VarCheck.UNDEFINED_VAR_ERROR); |
| 166 |
} |
| 167 |
|
| 168 |
public void testValidFunctionExpr() { |
| 169 |
testSame("(function() {});"); |
| 170 |
} |
| 171 |
|
| 172 |
public void testRecursiveFunction() { |
| 173 |
testSame("(function a() { return a(); })();"); |
| 174 |
} |
| 175 |
|
| 176 |
public void testRecursiveFunction2() { |
| 177 |
testSame("var a = 3; (function a() { return a(); })();"); |
| 178 |
} |
| 179 |
|
| 180 |
public void testLegalVarReferenceBetweenModules() { |
| 181 |
testDependentModules("var x = 10;", "var y = x++;", null); |
| 182 |
} |
| 183 |
|
| 184 |
public void testMissingModuleDependencyDefault() { |
| 185 |
testIndependentModules("var x = 10;", "var y = x++;", |
| 186 |
null, VarCheck.MISSING_MODULE_DEP_ERROR); |
| 187 |
} |
| 188 |
|
| 189 |
public void testViolatedModuleDependencyDefault() { |
| 190 |
testDependentModules("var y = x++;", "var x = 10;", |
| 191 |
VarCheck.VIOLATED_MODULE_DEP_ERROR); |
| 192 |
} |
| 193 |
|
| 194 |
public void testMissingModuleDependencySkipNonStrict() { |
| 195 |
sanityCheck = true; |
| 196 |
testIndependentModules("var x = 10;", "var y = x++;", |
| 197 |
null, null); |
| 198 |
} |
| 199 |
|
| 200 |
public void testViolatedModuleDependencySkipNonStrict() { |
| 201 |
sanityCheck = true; |
| 202 |
testDependentModules("var y = x++;", "var x = 10;", |
| 203 |
null); |
| 204 |
} |
| 205 |
|
| 206 |
public void testMissingModuleDependencySkipNonStrictNotPromoted() { |
| 207 |
sanityCheck = true; |
| 208 |
strictModuleDepErrorLevel = CheckLevel.ERROR; |
| 209 |
testIndependentModules("var x = 10;", "var y = x++;", null, null); |
| 210 |
} |
| 211 |
|
| 212 |
public void testViolatedModuleDependencyNonStrictNotPromoted() { |
| 213 |
sanityCheck = true; |
| 214 |
strictModuleDepErrorLevel = CheckLevel.ERROR; |
| 215 |
testDependentModules("var y = x++;", "var x = 10;", null); |
| 216 |
} |
| 217 |
|
| 218 |
public void testDependentStrictModuleDependencyCheck() { |
| 219 |
strictModuleDepErrorLevel = CheckLevel.ERROR; |
| 220 |
testDependentModules("var f = function() {return new B();};", |
| 221 |
"var B = function() {}", |
| 222 |
VarCheck.STRICT_MODULE_DEP_ERROR); |
| 223 |
} |
| 224 |
|
| 225 |
public void testIndependentStrictModuleDependencyCheck() { |
| 226 |
strictModuleDepErrorLevel = CheckLevel.ERROR; |
| 227 |
testIndependentModules("var f = function() {return new B();};", |
| 228 |
"var B = function() {}", |
| 229 |
VarCheck.STRICT_MODULE_DEP_ERROR, null); |
| 230 |
} |
| 231 |
|
| 232 |
public void testStarStrictModuleDependencyCheck() { |
| 233 |
strictModuleDepErrorLevel = CheckLevel.WARNING; |
| 234 |
testSame(createModuleStar("function a() {}", "function b() { a(); c(); }", |
| 235 |
"function c() { a(); }"), |
| 236 |
VarCheck.STRICT_MODULE_DEP_ERROR); |
| 237 |
} |
| 238 |
|
| 239 |
public void testForwardVarReferenceInLocalScope1() { |
| 240 |
testDependentModules("var x = 10; function a() {y++;}", |
| 241 |
"var y = 11; a();", null); |
| 242 |
} |
| 243 |
|
| 244 |
public void testForwardVarReferenceInLocalScope2() { |
| 245 |
|
| 246 |
|
| 247 |
testDependentModules("var x = 10; function a() {y++;} a();", |
| 248 |
"var y = 11;", null); |
| 249 |
} |
| 250 |
|
| 251 |
private void testDependentModules(String code1, String code2, |
| 252 |
DiagnosticType error) { |
| 253 |
testDependentModules(code1, code2, error, null); |
| 254 |
} |
| 255 |
|
| 256 |
private void testDependentModules(String code1, String code2, |
| 257 |
DiagnosticType error, |
| 258 |
DiagnosticType warning) { |
| 259 |
testTwoModules(code1, code2, true, error, warning); |
| 260 |
} |
| 261 |
|
| 262 |
private void testIndependentModules(String code1, String code2, |
| 263 |
DiagnosticType error, |
| 264 |
DiagnosticType warning) { |
| 265 |
testTwoModules(code1, code2, false, error, warning); |
| 266 |
} |
| 267 |
|
| 268 |
private void testTwoModules(String code1, String code2, boolean m2DependsOnm1, |
| 269 |
DiagnosticType error, DiagnosticType warning) { |
| 270 |
JSModule m1 = new JSModule("m1"); |
| 271 |
m1.add(SourceFile.fromCode("input1", code1)); |
| 272 |
JSModule m2 = new JSModule("m2"); |
| 273 |
m2.add(SourceFile.fromCode("input2", code2)); |
| 274 |
if (m2DependsOnm1) { |
| 275 |
m2.addDependency(m1); |
| 276 |
} |
| 277 |
test(new JSModule[] { m1, m2 }, |
| 278 |
new String[] { code1, code2 }, error, warning); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public void testSimple() { |
| 285 |
checkSynthesizedExtern("x", "var x;"); |
| 286 |
checkSynthesizedExtern("var x", ""); |
| 287 |
} |
| 288 |
|
| 289 |
public void testSimpleSanityCheck() { |
| 290 |
sanityCheck = true; |
| 291 |
try { |
| 292 |
checkSynthesizedExtern("x", ""); |
| 293 |
} catch (RuntimeException e) { |
| 294 |
assertTrue(e.getMessage().indexOf("Unexpected variable x") != -1); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
public void testParameter() { |
| 299 |
checkSynthesizedExtern("function f(x){}", ""); |
| 300 |
} |
| 301 |
|
| 302 |
public void testLocalVar() { |
| 303 |
checkSynthesizedExtern("function f(){x}", "var x"); |
| 304 |
} |
| 305 |
|
| 306 |
public void testTwoLocalVars() { |
| 307 |
checkSynthesizedExtern("function f(){x}function g() {x}", "var x"); |
| 308 |
} |
| 309 |
|
| 310 |
public void testInnerFunctionLocalVar() { |
| 311 |
checkSynthesizedExtern("function f(){function g() {x}}", "var x"); |
| 312 |
} |
| 313 |
|
| 314 |
public void testNoCreateVarsForLabels() { |
| 315 |
checkSynthesizedExtern("x:var y", ""); |
| 316 |
} |
| 317 |
|
| 318 |
public void testVariableInNormalCodeUsedInExterns1() { |
| 319 |
checkSynthesizedExtern( |
| 320 |
"x.foo;", "var x;", "var x; x.foo;"); |
| 321 |
} |
| 322 |
|
| 323 |
public void testVariableInNormalCodeUsedInExterns2() { |
| 324 |
checkSynthesizedExtern( |
| 325 |
"x;", "var x;", "var x; x;"); |
| 326 |
} |
| 327 |
|
| 328 |
public void testVariableInNormalCodeUsedInExterns3() { |
| 329 |
checkSynthesizedExtern( |
| 330 |
"x.foo;", "function x() {}", "var x; x.foo; "); |
| 331 |
} |
| 332 |
|
| 333 |
public void testVariableInNormalCodeUsedInExterns4() { |
| 334 |
checkSynthesizedExtern( |
| 335 |
"x;", "function x() {}", "var x; x; "); |
| 336 |
} |
| 337 |
|
| 338 |
private final static class VariableTestCheck implements CompilerPass { |
| 339 |
|
| 340 |
final AbstractCompiler compiler; |
| 341 |
VariableTestCheck(AbstractCompiler compiler) { |
| 342 |
this.compiler = compiler; |
| 343 |
} |
| 344 |
|
| 345 |
@Override |
| 346 |
public void process(Node externs, Node root) { |
| 347 |
NodeTraversal.traverseRoots(compiler, Lists.newArrayList(externs, root), |
| 348 |
new AbstractPostOrderCallback() { |
| 349 |
@Override |
| 350 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 351 |
if (n.isName() && !parent.isFunction() |
| 352 |
&& !parent.isLabel()) { |
| 353 |
assertTrue("Variable " + n.getString() + " should have be declared", |
| 354 |
t.getScope().isDeclared(n.getString(), true)); |
| 355 |
} |
| 356 |
} |
| 357 |
}); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
public void checkSynthesizedExtern( |
| 362 |
String input, String expectedExtern) { |
| 363 |
checkSynthesizedExtern("", input, expectedExtern); |
| 364 |
} |
| 365 |
|
| 366 |
public void checkSynthesizedExtern( |
| 367 |
String extern, String input, String expectedExtern) { |
| 368 |
Compiler compiler = new Compiler(); |
| 369 |
CompilerOptions options = new CompilerOptions(); |
| 370 |
options.setWarningLevel( |
| 371 |
DiagnosticGroup.forType(VarCheck.UNDEFINED_VAR_ERROR), |
| 372 |
CheckLevel.OFF); |
| 373 |
compiler.init( |
| 374 |
ImmutableList.of(SourceFile.fromCode("extern", extern)), |
| 375 |
ImmutableList.of(SourceFile.fromCode("input", input)), |
| 376 |
options); |
| 377 |
compiler.parseInputs(); |
| 378 |
assertFalse(compiler.hasErrors()); |
| 379 |
|
| 380 |
Node externsAndJs = compiler.getRoot(); |
| 381 |
Node root = externsAndJs.getLastChild(); |
| 382 |
|
| 383 |
Node externs = externsAndJs.getFirstChild(); |
| 384 |
|
| 385 |
Node expected = compiler.parseTestCode(expectedExtern); |
| 386 |
assertFalse(compiler.hasErrors()); |
| 387 |
|
| 388 |
(new VarCheck(compiler, sanityCheck)) |
| 389 |
.process(externs, root); |
| 390 |
if (!sanityCheck) { |
| 391 |
(new VariableTestCheck(compiler)).process(externs, root); |
| 392 |
} |
| 393 |
|
| 394 |
String externsCode = compiler.toSource(externs); |
| 395 |
String expectedCode = compiler.toSource(expected); |
| 396 |
|
| 397 |
assertEquals(expectedCode, externsCode); |
| 398 |
} |
| 399 |
} |