| 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.Iterables; |
| 20 |
import com.google.common.collect.Lists; |
| 21 |
import com.google.common.collect.Ordering; |
| 22 |
import com.google.common.collect.Sets; |
| 23 |
import com.google.javascript.jscomp.SymbolTable.Reference; |
| 24 |
import com.google.javascript.jscomp.SymbolTable.Symbol; |
| 25 |
import com.google.javascript.jscomp.SymbolTable.SymbolScope; |
| 26 |
import com.google.javascript.rhino.JSDocInfo; |
| 27 |
import com.google.javascript.rhino.Token; |
| 28 |
|
| 29 |
import junit.framework.TestCase; |
| 30 |
|
| 31 |
import java.util.List; |
| 32 |
import java.util.Set; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class SymbolTableTest extends TestCase { |
| 38 |
|
| 39 |
private static final String EXTERNS = CompilerTypeTestCase.DEFAULT_EXTERNS + |
| 40 |
"\nfunction customExternFn(customExternArg) {}"; |
| 41 |
|
| 42 |
private CompilerOptions options; |
| 43 |
|
| 44 |
@Override |
| 45 |
public void setUp() throws Exception { |
| 46 |
super.setUp(); |
| 47 |
|
| 48 |
options = new CompilerOptions(); |
| 49 |
options.setCodingConvention(new ClosureCodingConvention()); |
| 50 |
CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel( |
| 51 |
options); |
| 52 |
WarningLevel.VERBOSE.setOptionsForWarningLevel(options); |
| 53 |
options.ideMode = true; |
| 54 |
} |
| 55 |
|
| 56 |
public void testGlobalVar() throws Exception { |
| 57 |
SymbolTable table = createSymbolTable( |
| 58 |
"/** @type {number} */ var x = 5;"); |
| 59 |
assertNull(getGlobalVar(table, "y")); |
| 60 |
assertNotNull(getGlobalVar(table, "x")); |
| 61 |
assertEquals("number", getGlobalVar(table, "x").getType().toString()); |
| 62 |
|
| 63 |
|
| 64 |
assertEquals(2, getVars(table).size()); |
| 65 |
} |
| 66 |
|
| 67 |
public void testGlobalThisReferences() throws Exception { |
| 68 |
SymbolTable table = createSymbolTable( |
| 69 |
"var x = this; function f() { return this + this + this; }"); |
| 70 |
|
| 71 |
Symbol global = getGlobalVar(table, "*global*"); |
| 72 |
assertNotNull(global); |
| 73 |
|
| 74 |
List<Reference> refs = table.getReferenceList(global); |
| 75 |
assertEquals(1, refs.size()); |
| 76 |
} |
| 77 |
|
| 78 |
public void testGlobalThisReferences2() throws Exception { |
| 79 |
|
| 80 |
SymbolTable table = createSymbolTable(""); |
| 81 |
|
| 82 |
Symbol global = getGlobalVar(table, "*global*"); |
| 83 |
assertNotNull(global); |
| 84 |
|
| 85 |
List<Reference> refs = table.getReferenceList(global); |
| 86 |
assertEquals(0, refs.size()); |
| 87 |
} |
| 88 |
|
| 89 |
public void testGlobalThisReferences3() throws Exception { |
| 90 |
SymbolTable table = createSymbolTable("this.foo = {}; this.foo.bar = {};"); |
| 91 |
|
| 92 |
Symbol global = getGlobalVar(table, "*global*"); |
| 93 |
assertNotNull(global); |
| 94 |
|
| 95 |
List<Reference> refs = table.getReferenceList(global); |
| 96 |
assertEquals(2, refs.size()); |
| 97 |
} |
| 98 |
|
| 99 |
public void testGlobalThisPropertyReferences() throws Exception { |
| 100 |
SymbolTable table = createSymbolTable( |
| 101 |
"/** @constructor */ function Foo() {} this.Foo;"); |
| 102 |
|
| 103 |
Symbol foo = getGlobalVar(table, "Foo"); |
| 104 |
assertNotNull(foo); |
| 105 |
|
| 106 |
List<Reference> refs = table.getReferenceList(foo); |
| 107 |
assertEquals(2, refs.size()); |
| 108 |
} |
| 109 |
|
| 110 |
public void testGlobalVarReferences() throws Exception { |
| 111 |
SymbolTable table = createSymbolTable( |
| 112 |
"/** @type {number} */ var x = 5; x = 6;"); |
| 113 |
Symbol x = getGlobalVar(table, "x"); |
| 114 |
List<Reference> refs = table.getReferenceList(x); |
| 115 |
|
| 116 |
assertEquals(2, refs.size()); |
| 117 |
assertEquals(x.getDeclaration(), refs.get(0)); |
| 118 |
assertEquals(Token.VAR, refs.get(0).getNode().getParent().getType()); |
| 119 |
assertEquals(Token.ASSIGN, refs.get(1).getNode().getParent().getType()); |
| 120 |
} |
| 121 |
|
| 122 |
public void testLocalVarReferences() throws Exception { |
| 123 |
SymbolTable table = createSymbolTable( |
| 124 |
"function f(x) { return x; }"); |
| 125 |
Symbol x = getLocalVar(table, "x"); |
| 126 |
List<Reference> refs = table.getReferenceList(x); |
| 127 |
|
| 128 |
assertEquals(2, refs.size()); |
| 129 |
assertEquals(x.getDeclaration(), refs.get(0)); |
| 130 |
assertEquals(Token.PARAM_LIST, refs.get(0).getNode().getParent().getType()); |
| 131 |
assertEquals(Token.RETURN, refs.get(1).getNode().getParent().getType()); |
| 132 |
} |
| 133 |
|
| 134 |
public void testLocalThisReferences() throws Exception { |
| 135 |
SymbolTable table = createSymbolTable( |
| 136 |
"/** @constructor */ function F() { this.foo = 3; this.bar = 5; }"); |
| 137 |
|
| 138 |
Symbol f = getGlobalVar(table, "F"); |
| 139 |
assertNotNull(f); |
| 140 |
|
| 141 |
Symbol t = table.getParameterInFunction(f, "this"); |
| 142 |
assertNotNull(t); |
| 143 |
|
| 144 |
List<Reference> refs = table.getReferenceList(t); |
| 145 |
assertEquals(2, refs.size()); |
| 146 |
} |
| 147 |
|
| 148 |
public void testLocalThisReferences2() throws Exception { |
| 149 |
SymbolTable table = createSymbolTable( |
| 150 |
"/** @constructor */ function F() {}" + |
| 151 |
"F.prototype.baz = " + |
| 152 |
" function() { this.foo = 3; this.bar = 5; };"); |
| 153 |
|
| 154 |
Symbol baz = getGlobalVar(table, "F.prototype.baz"); |
| 155 |
assertNotNull(baz); |
| 156 |
|
| 157 |
Symbol t = table.getParameterInFunction(baz, "this"); |
| 158 |
assertNotNull(t); |
| 159 |
|
| 160 |
List<Reference> refs = table.getReferenceList(t); |
| 161 |
assertEquals(2, refs.size()); |
| 162 |
} |
| 163 |
|
| 164 |
public void testLocalThisReferences3() throws Exception { |
| 165 |
SymbolTable table = createSymbolTable( |
| 166 |
"/** @constructor */ function F() {}"); |
| 167 |
|
| 168 |
Symbol baz = getGlobalVar(table, "F"); |
| 169 |
assertNotNull(baz); |
| 170 |
|
| 171 |
Symbol t = table.getParameterInFunction(baz, "this"); |
| 172 |
assertNotNull(t); |
| 173 |
|
| 174 |
List<Reference> refs = table.getReferenceList(t); |
| 175 |
assertEquals(0, refs.size()); |
| 176 |
} |
| 177 |
|
| 178 |
public void testNamespacedReferences() throws Exception { |
| 179 |
|
| 180 |
|
| 181 |
SymbolTable table = createSymbolTable( |
| 182 |
"var goog = {};" + |
| 183 |
"goog.dom = {};" + |
| 184 |
"goog.dom.DomHelper = function(){};"); |
| 185 |
Symbol goog = getGlobalVar(table, "goog"); |
| 186 |
assertNotNull(goog); |
| 187 |
assertEquals(3, Iterables.size(table.getReferences(goog))); |
| 188 |
|
| 189 |
Symbol googDom = getGlobalVar(table, "goog.dom"); |
| 190 |
assertNotNull(googDom); |
| 191 |
assertEquals(2, Iterables.size(table.getReferences(googDom))); |
| 192 |
|
| 193 |
Symbol googDomHelper = getGlobalVar(table, "goog.dom.DomHelper"); |
| 194 |
assertNotNull(googDomHelper); |
| 195 |
assertEquals(1, Iterables.size(table.getReferences(googDomHelper))); |
| 196 |
} |
| 197 |
|
| 198 |
public void testIncompleteNamespacedReferences() throws Exception { |
| 199 |
SymbolTable table = createSymbolTable( |
| 200 |
"/** @constructor */\n" + |
| 201 |
"goog.dom.DomHelper = function(){};\n" + |
| 202 |
"var y = goog.dom.DomHelper;\n"); |
| 203 |
Symbol goog = getGlobalVar(table, "goog"); |
| 204 |
assertNotNull(goog); |
| 205 |
assertEquals(2, table.getReferenceList(goog).size()); |
| 206 |
|
| 207 |
Symbol googDom = getGlobalVar(table, "goog.dom"); |
| 208 |
assertNotNull(googDom); |
| 209 |
assertEquals(2, table.getReferenceList(googDom).size()); |
| 210 |
|
| 211 |
Symbol googDomHelper = getGlobalVar(table, "goog.dom.DomHelper"); |
| 212 |
assertNotNull(googDomHelper); |
| 213 |
assertEquals(2, Iterables.size(table.getReferences(googDomHelper))); |
| 214 |
} |
| 215 |
|
| 216 |
public void testGlobalRichObjectReference() throws Exception { |
| 217 |
SymbolTable table = createSymbolTable( |
| 218 |
"/** @constructor */\n" + |
| 219 |
"function A(){};\n" + |
| 220 |
"/** @type {?A} */ A.prototype.b;\n" + |
| 221 |
"/** @type {A} */ var a = new A();\n" + |
| 222 |
"function g() {\n" + |
| 223 |
" return a.b ? 'x' : 'y';\n" + |
| 224 |
"}\n" + |
| 225 |
"(function() {\n" + |
| 226 |
" var x; if (x) { x = a.b.b; } else { x = a.b.c; }\n" + |
| 227 |
" return x;\n" + |
| 228 |
"})();\n"); |
| 229 |
|
| 230 |
Symbol ab = getGlobalVar(table, "a.b"); |
| 231 |
assertNull(ab); |
| 232 |
|
| 233 |
Symbol propB = getGlobalVar(table, "A.prototype.b"); |
| 234 |
assertNotNull(propB); |
| 235 |
assertEquals(5, table.getReferenceList(propB).size()); |
| 236 |
} |
| 237 |
|
| 238 |
public void testRemovalOfNamespacedReferencesOfProperties() |
| 239 |
throws Exception { |
| 240 |
SymbolTable table = createSymbolTable( |
| 241 |
"/** @constructor */ var DomHelper = function(){};" + |
| 242 |
"/** method */ DomHelper.method = function() {};"); |
| 243 |
|
| 244 |
Symbol domHelper = getGlobalVar(table, "DomHelper"); |
| 245 |
assertNotNull(domHelper); |
| 246 |
|
| 247 |
Symbol domHelperNamespacedMethod = getGlobalVar(table, "DomHelper.method"); |
| 248 |
assertEquals("method", domHelperNamespacedMethod.getName()); |
| 249 |
|
| 250 |
Symbol domHelperMethod = domHelper.getPropertyScope().getSlot("method"); |
| 251 |
assertNotNull(domHelperMethod); |
| 252 |
} |
| 253 |
|
| 254 |
public void testGoogScopeReferences() throws Exception { |
| 255 |
SymbolTable table = createSymbolTable( |
| 256 |
"var goog = {};" + |
| 257 |
"goog.scope = function() {};" + |
| 258 |
"goog.scope(function() {});"); |
| 259 |
Symbol googScope = getGlobalVar(table, "goog.scope"); |
| 260 |
assertNotNull(googScope); |
| 261 |
assertEquals(2, Iterables.size(table.getReferences(googScope))); |
| 262 |
} |
| 263 |
|
| 264 |
public void testGoogRequireReferences() throws Exception { |
| 265 |
SymbolTable table = createSymbolTable( |
| 266 |
"var goog = {};" + |
| 267 |
"goog.provide = function() {};" + |
| 268 |
"goog.require = function() {};" + |
| 269 |
"goog.provide('goog.dom');" + |
| 270 |
"goog.require('goog.dom');"); |
| 271 |
Symbol goog = getGlobalVar(table, "goog"); |
| 272 |
assertNotNull(goog); |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
assertEquals(8, Iterables.size(table.getReferences(goog))); |
| 283 |
} |
| 284 |
|
| 285 |
public void testGoogRequireReferences2() throws Exception { |
| 286 |
options.brokenClosureRequiresLevel = CheckLevel.OFF; |
| 287 |
SymbolTable table = createSymbolTable( |
| 288 |
"foo.bar = function(){}; // definition\n" |
| 289 |
+ "goog.require('foo.bar')\n"); |
| 290 |
Symbol fooBar = getGlobalVar(table, "foo.bar"); |
| 291 |
assertNotNull(fooBar); |
| 292 |
assertEquals(2, Iterables.size(table.getReferences(fooBar))); |
| 293 |
} |
| 294 |
|
| 295 |
public void testGlobalVarInExterns() throws Exception { |
| 296 |
SymbolTable table = createSymbolTable("customExternFn(1);"); |
| 297 |
Symbol fn = getGlobalVar(table, "customExternFn"); |
| 298 |
List<Reference> refs = table.getReferenceList(fn); |
| 299 |
assertEquals(2, refs.size()); |
| 300 |
|
| 301 |
SymbolScope scope = table.getEnclosingScope(refs.get(0).getNode()); |
| 302 |
assertTrue(scope.isGlobalScope()); |
| 303 |
assertEquals(SymbolTable.GLOBAL_THIS, |
| 304 |
table.getSymbolForScope(scope).getName()); |
| 305 |
} |
| 306 |
|
| 307 |
public void testLocalVarInExterns() throws Exception { |
| 308 |
SymbolTable table = createSymbolTable(""); |
| 309 |
Symbol arg = getLocalVar(table, "customExternArg"); |
| 310 |
List<Reference> refs = table.getReferenceList(arg); |
| 311 |
assertEquals(1, refs.size()); |
| 312 |
|
| 313 |
Symbol fn = getGlobalVar(table, "customExternFn"); |
| 314 |
SymbolScope scope = table.getEnclosingScope(refs.get(0).getNode()); |
| 315 |
assertFalse(scope.isGlobalScope()); |
| 316 |
assertEquals(fn, table.getSymbolForScope(scope)); |
| 317 |
} |
| 318 |
|
| 319 |
public void testSymbolsForType() throws Exception { |
| 320 |
SymbolTable table = createSymbolTable( |
| 321 |
"function random() { return 1; }" + |
| 322 |
"/** @constructor */ function Foo() {}" + |
| 323 |
"/** @constructor */ function Bar() {}" + |
| 324 |
"var x = random() ? new Foo() : new Bar();"); |
| 325 |
|
| 326 |
Symbol x = getGlobalVar(table, "x"); |
| 327 |
Symbol foo = getGlobalVar(table, "Foo"); |
| 328 |
Symbol bar = getGlobalVar(table, "Bar"); |
| 329 |
Symbol fooPrototype = getGlobalVar(table, "Foo.prototype"); |
| 330 |
Symbol fn = getGlobalVar(table, "Function"); |
| 331 |
assertEquals( |
| 332 |
Lists.newArrayList(foo, bar), table.getAllSymbolsForTypeOf(x)); |
| 333 |
assertEquals( |
| 334 |
Lists.newArrayList(fn), table.getAllSymbolsForTypeOf(foo)); |
| 335 |
assertEquals( |
| 336 |
Lists.newArrayList(foo), table.getAllSymbolsForTypeOf(fooPrototype)); |
| 337 |
assertEquals( |
| 338 |
foo, |
| 339 |
table.getSymbolDeclaredBy( |
| 340 |
foo.getType().toMaybeFunctionType())); |
| 341 |
} |
| 342 |
|
| 343 |
public void testStaticMethodReferences() throws Exception { |
| 344 |
SymbolTable table = createSymbolTable( |
| 345 |
"/** @constructor */ var DomHelper = function(){};" + |
| 346 |
"/** method */ DomHelper.method = function() {};" + |
| 347 |
"function f() { var x = DomHelper; x.method() + x.method(); }"); |
| 348 |
|
| 349 |
Symbol method = |
| 350 |
getGlobalVar(table, "DomHelper").getPropertyScope().getSlot("method"); |
| 351 |
assertEquals( |
| 352 |
3, Iterables.size(table.getReferences(method))); |
| 353 |
} |
| 354 |
|
| 355 |
public void testMethodReferences() throws Exception { |
| 356 |
SymbolTable table = createSymbolTable( |
| 357 |
"/** @constructor */ var DomHelper = function(){};" + |
| 358 |
"/** method */ DomHelper.prototype.method = function() {};" + |
| 359 |
"function f() { " + |
| 360 |
" (new DomHelper()).method(); (new DomHelper()).method(); };"); |
| 361 |
|
| 362 |
Symbol method = |
| 363 |
getGlobalVar(table, "DomHelper.prototype.method"); |
| 364 |
assertEquals( |
| 365 |
3, Iterables.size(table.getReferences(method))); |
| 366 |
} |
| 367 |
|
| 368 |
public void testSuperClassMethodReferences() throws Exception { |
| 369 |
SymbolTable table = createSymbolTable( |
| 370 |
"var goog = {};" + |
| 371 |
"goog.inherits = function(a, b) {};" + |
| 372 |
"/** @constructor */ var A = function(){};" + |
| 373 |
"/** method */ A.prototype.method = function() {};" + |
| 374 |
"/**\n" + |
| 375 |
" * @constructor\n" + |
| 376 |
" * @extends {A}\n" + |
| 377 |
" */\n" + |
| 378 |
"var B = function(){};\n" + |
| 379 |
"goog.inherits(B, A);" + |
| 380 |
"/** method */ B.prototype.method = function() {" + |
| 381 |
" B.superClass_.method();" + |
| 382 |
"};"); |
| 383 |
|
| 384 |
Symbol methodA = |
| 385 |
getGlobalVar(table, "A.prototype.method"); |
| 386 |
assertEquals( |
| 387 |
2, Iterables.size(table.getReferences(methodA))); |
| 388 |
} |
| 389 |
|
| 390 |
public void testMethodReferencesMissingTypeInfo() throws Exception { |
| 391 |
SymbolTable table = createSymbolTable( |
| 392 |
"/**\n" + |
| 393 |
" * @constructor\n" + |
| 394 |
" * @extends {Missing}\n" + |
| 395 |
" */ var DomHelper = function(){};\n" + |
| 396 |
"/** method */ DomHelper.prototype.method = function() {\n" + |
| 397 |
" this.method();\n" + |
| 398 |
"};\n" + |
| 399 |
"function f() { " + |
| 400 |
" (new DomHelper()).method();\n" + |
| 401 |
"};"); |
| 402 |
|
| 403 |
Symbol method = |
| 404 |
getGlobalVar(table, "DomHelper.prototype.method"); |
| 405 |
assertEquals( |
| 406 |
3, Iterables.size(table.getReferences(method))); |
| 407 |
} |
| 408 |
|
| 409 |
public void testFieldReferencesMissingTypeInfo() throws Exception { |
| 410 |
SymbolTable table = createSymbolTable( |
| 411 |
"/**\n" + |
| 412 |
" * @constructor\n" + |
| 413 |
" * @extends {Missing}\n" + |
| 414 |
" */ var DomHelper = function(){ this.prop = 1; };\n" + |
| 415 |
"/** @type {number} */ DomHelper.prototype.prop = 2;\n" + |
| 416 |
"function f() {\n" + |
| 417 |
" return (new DomHelper()).prop;\n" + |
| 418 |
"};"); |
| 419 |
|
| 420 |
Symbol prop = |
| 421 |
getGlobalVar(table, "DomHelper.prototype.prop"); |
| 422 |
assertEquals(3, table.getReferenceList(prop).size()); |
| 423 |
|
| 424 |
assertNull(getLocalVar(table, "this.prop")); |
| 425 |
} |
| 426 |
|
| 427 |
public void testFieldReferences() throws Exception { |
| 428 |
SymbolTable table = createSymbolTable( |
| 429 |
"/** @constructor */ var DomHelper = function(){" + |
| 430 |
" /** @type {number} */ this.field = 3;" + |
| 431 |
"};" + |
| 432 |
"function f() { " + |
| 433 |
" return (new DomHelper()).field + (new DomHelper()).field; };"); |
| 434 |
|
| 435 |
Symbol field = getGlobalVar(table, "DomHelper.prototype.field"); |
| 436 |
assertEquals( |
| 437 |
3, Iterables.size(table.getReferences(field))); |
| 438 |
} |
| 439 |
|
| 440 |
public void testUndeclaredFieldReferences() throws Exception { |
| 441 |
|
| 442 |
|
| 443 |
SymbolTable table = createSymbolTable( |
| 444 |
"/** @constructor */ var DomHelper = function(){};" + |
| 445 |
"DomHelper.prototype.method = function() { " + |
| 446 |
" this.field = 3;" + |
| 447 |
" return x.field;" + |
| 448 |
"}"); |
| 449 |
|
| 450 |
Symbol field = getGlobalVar(table, "DomHelper.prototype.field"); |
| 451 |
assertNull(field); |
| 452 |
} |
| 453 |
|
| 454 |
public void testPrototypeReferences() throws Exception { |
| 455 |
SymbolTable table = createSymbolTable( |
| 456 |
"/** @constructor */ function DomHelper() {}" + |
| 457 |
"DomHelper.prototype.method = function() {};"); |
| 458 |
Symbol prototype = |
| 459 |
getGlobalVar(table, "DomHelper.prototype"); |
| 460 |
assertNotNull(prototype); |
| 461 |
|
| 462 |
List<Reference> refs = table.getReferenceList(prototype); |
| 463 |
|
| 464 |
|
| 465 |
assertEquals(refs.toString(), 2, refs.size()); |
| 466 |
} |
| 467 |
|
| 468 |
public void testPrototypeReferences2() throws Exception { |
| 469 |
SymbolTable table = createSymbolTable( |
| 470 |
"/** @constructor */\n" |
| 471 |
+ "function Snork() {}\n" |
| 472 |
+ "Snork.prototype.baz = 3;\n"); |
| 473 |
Symbol prototype = |
| 474 |
getGlobalVar(table, "Snork.prototype"); |
| 475 |
assertNotNull(prototype); |
| 476 |
|
| 477 |
List<Reference> refs = table.getReferenceList(prototype); |
| 478 |
assertEquals(2, refs.size()); |
| 479 |
} |
| 480 |
|
| 481 |
public void testPrototypeReferences3() throws Exception { |
| 482 |
SymbolTable table = createSymbolTable( |
| 483 |
"/** @constructor */ function Foo() {}"); |
| 484 |
Symbol fooPrototype = getGlobalVar(table, "Foo.prototype"); |
| 485 |
assertNotNull(fooPrototype); |
| 486 |
|
| 487 |
List<Reference> refs = table.getReferenceList(fooPrototype); |
| 488 |
assertEquals(1, refs.size()); |
| 489 |
assertEquals(Token.NAME, refs.get(0).getNode().getType()); |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
assertEquals( |
| 494 |
refs.get(0).getNode(), |
| 495 |
table.getReferenceList(getGlobalVar(table, "Foo")).get(0).getNode()); |
| 496 |
} |
| 497 |
|
| 498 |
public void testPrototypeReferences4() throws Exception { |
| 499 |
SymbolTable table = createSymbolTable( |
| 500 |
"/** @constructor */ function Foo() {}" + |
| 501 |
"Foo.prototype = {bar: 3}"); |
| 502 |
Symbol fooPrototype = getGlobalVar(table, "Foo.prototype"); |
| 503 |
assertNotNull(fooPrototype); |
| 504 |
|
| 505 |
List<Reference> refs = Lists.newArrayList( |
| 506 |
table.getReferences(fooPrototype)); |
| 507 |
assertEquals(1, refs.size()); |
| 508 |
assertEquals(Token.GETPROP, refs.get(0).getNode().getType()); |
| 509 |
assertEquals("Foo.prototype", refs.get(0).getNode().getQualifiedName()); |
| 510 |
} |
| 511 |
|
| 512 |
public void testPrototypeReferences5() throws Exception { |
| 513 |
SymbolTable table = createSymbolTable( |
| 514 |
"var goog = {}; /** @constructor */ goog.Foo = function() {};"); |
| 515 |
Symbol fooPrototype = getGlobalVar(table, "goog.Foo.prototype"); |
| 516 |
assertNotNull(fooPrototype); |
| 517 |
|
| 518 |
List<Reference> refs = table.getReferenceList(fooPrototype); |
| 519 |
assertEquals(1, refs.size()); |
| 520 |
assertEquals(Token.GETPROP, refs.get(0).getNode().getType()); |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
assertEquals( |
| 525 |
refs.get(0).getNode(), |
| 526 |
table.getReferenceList( |
| 527 |
getGlobalVar(table, "goog.Foo")).get(0).getNode()); |
| 528 |
} |
| 529 |
|
| 530 |
public void testReferencesInJSDocType() { |
| 531 |
SymbolTable table = createSymbolTable( |
| 532 |
"/** @constructor */ function Foo() {}\n" + |
| 533 |
"/** @type {Foo} */ var x;\n" + |
| 534 |
"/** @param {Foo} x */ function f(x) {}\n" + |
| 535 |
"/** @return {function(): Foo} */ function g() {}\n" + |
| 536 |
"/**\n" + |
| 537 |
" * @constructor\n" + |
| 538 |
" * @extends {Foo}\n" + |
| 539 |
" */ function Sub() {}"); |
| 540 |
Symbol foo = getGlobalVar(table, "Foo"); |
| 541 |
assertNotNull(foo); |
| 542 |
|
| 543 |
List<Reference> refs = table.getReferenceList(foo); |
| 544 |
assertEquals(5, refs.size()); |
| 545 |
|
| 546 |
assertEquals(1, refs.get(0).getNode().getLineno()); |
| 547 |
assertEquals(29, refs.get(0).getNode().getCharno()); |
| 548 |
assertEquals(3, refs.get(0).getNode().getLength()); |
| 549 |
|
| 550 |
assertEquals(2, refs.get(1).getNode().getLineno()); |
| 551 |
assertEquals(11, refs.get(1).getNode().getCharno()); |
| 552 |
|
| 553 |
assertEquals(3, refs.get(2).getNode().getLineno()); |
| 554 |
assertEquals(12, refs.get(2).getNode().getCharno()); |
| 555 |
|
| 556 |
assertEquals(4, refs.get(3).getNode().getLineno()); |
| 557 |
assertEquals(25, refs.get(3).getNode().getCharno()); |
| 558 |
|
| 559 |
assertEquals(7, refs.get(4).getNode().getLineno()); |
| 560 |
assertEquals(13, refs.get(4).getNode().getCharno()); |
| 561 |
} |
| 562 |
|
| 563 |
public void testReferencesInJSDocType2() { |
| 564 |
SymbolTable table = createSymbolTable( |
| 565 |
"/** @param {string} x */ function f(x) {}\n"); |
| 566 |
Symbol str = getGlobalVar(table, "String"); |
| 567 |
assertNotNull(str); |
| 568 |
|
| 569 |
List<Reference> refs = table.getReferenceList(str); |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
assertTrue(refs.size() > 1); |
| 576 |
|
| 577 |
int last = refs.size() - 1; |
| 578 |
for (int i = 0; i < refs.size(); i++) { |
| 579 |
Reference ref = refs.get(i); |
| 580 |
assertEquals(i != last, ref.getNode().isFromExterns()); |
| 581 |
if (!ref.getNode().isFromExterns()) { |
| 582 |
assertEquals("in1", ref.getNode().getSourceFileName()); |
| 583 |
} |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
public void testReferencesInJSDocName() { |
| 588 |
String code = "/** @param {Object} x */ function f(x) {}\n"; |
| 589 |
SymbolTable table = createSymbolTable(code); |
| 590 |
Symbol x = getLocalVar(table, "x"); |
| 591 |
assertNotNull(x); |
| 592 |
|
| 593 |
List<Reference> refs = table.getReferenceList(x); |
| 594 |
assertEquals(2, refs.size()); |
| 595 |
|
| 596 |
assertEquals(code.indexOf("x) {"), refs.get(0).getNode().getCharno()); |
| 597 |
assertEquals(code.indexOf("x */"), refs.get(1).getNode().getCharno()); |
| 598 |
assertEquals("in1", |
| 599 |
refs.get(0).getNode().getSourceFileName()); |
| 600 |
} |
| 601 |
|
| 602 |
public void testLocalQualifiedNamesInLocalScopes() { |
| 603 |
SymbolTable table = createSymbolTable( |
| 604 |
"function f() { var x = {}; x.number = 3; }"); |
| 605 |
Symbol xNumber = getLocalVar(table, "x.number"); |
| 606 |
assertNotNull(xNumber); |
| 607 |
assertFalse(table.getScope(xNumber).isGlobalScope()); |
| 608 |
|
| 609 |
assertEquals("number", xNumber.getType().toString()); |
| 610 |
} |
| 611 |
|
| 612 |
public void testNaturalSymbolOrdering() { |
| 613 |
SymbolTable table = createSymbolTable( |
| 614 |
"/** @const */ var a = {};" + |
| 615 |
"/** @const */ a.b = {};" + |
| 616 |
"/** @param {number} x */ function f(x) {}"); |
| 617 |
Symbol a = getGlobalVar(table, "a"); |
| 618 |
Symbol ab = getGlobalVar(table, "a.b"); |
| 619 |
Symbol f = getGlobalVar(table, "f"); |
| 620 |
Symbol x = getLocalVar(table, "x"); |
| 621 |
Ordering<Symbol> ordering = table.getNaturalSymbolOrdering(); |
| 622 |
assertSymmetricOrdering(ordering, a, ab); |
| 623 |
assertSymmetricOrdering(ordering, a, f); |
| 624 |
assertSymmetricOrdering(ordering, f, ab); |
| 625 |
assertSymmetricOrdering(ordering, f, x); |
| 626 |
} |
| 627 |
|
| 628 |
public void testDeclarationDisagreement() { |
| 629 |
SymbolTable table = createSymbolTable( |
| 630 |
"/** @const */ var goog = goog || {};\n" + |
| 631 |
"/** @param {!Function} x */\n" + |
| 632 |
"goog.addSingletonGetter2 = function(x) {};\n" + |
| 633 |
"/** Wakka wakka wakka */\n" + |
| 634 |
"goog.addSingletonGetter = goog.addSingletonGetter2;\n" + |
| 635 |
"/** @param {!Function} x */\n" + |
| 636 |
"goog.addSingletonGetter = function(x) {};\n"); |
| 637 |
|
| 638 |
Symbol method = getGlobalVar(table, "goog.addSingletonGetter"); |
| 639 |
List<Reference> refs = table.getReferenceList(method); |
| 640 |
assertEquals(2, refs.size()); |
| 641 |
|
| 642 |
|
| 643 |
assertEquals(7, method.getDeclaration().getNode().getLineno()); |
| 644 |
assertEquals(5, refs.get(1).getNode().getLineno()); |
| 645 |
} |
| 646 |
|
| 647 |
public void testMultipleExtends() { |
| 648 |
SymbolTable table = createSymbolTable( |
| 649 |
"/** @const */ var goog = goog || {};\n" + |
| 650 |
"goog.inherits = function(x, y) {};\n" + |
| 651 |
"/** @constructor */\n" + |
| 652 |
"goog.A = function() { this.fieldA = this.constructor; };\n" + |
| 653 |
"/** @constructor */ goog.A.FooA = function() {};\n" + |
| 654 |
"/** @return {void} */ goog.A.prototype.methodA = function() {};\n" + |
| 655 |
"/**\n" + |
| 656 |
" * @constructor\n" + |
| 657 |
" * @extends {goog.A}\n" + |
| 658 |
" */\n" + |
| 659 |
"goog.B = function() { this.fieldB = this.constructor; };\n" + |
| 660 |
"goog.inherits(goog.B, goog.A);\n" + |
| 661 |
"/** @return {void} */ goog.B.prototype.methodB = function() {};\n" + |
| 662 |
"/**\n" + |
| 663 |
" * @constructor\n" + |
| 664 |
" * @extends {goog.A}\n" + |
| 665 |
" */\n" + |
| 666 |
"goog.B2 = function() { this.fieldB = this.constructor; };\n" + |
| 667 |
"goog.inherits(goog.B2, goog.A);\n" + |
| 668 |
"/** @constructor */ goog.B2.FooB = function() {};\n" + |
| 669 |
"/** @return {void} */ goog.B2.prototype.methodB = function() {};\n" + |
| 670 |
"/**\n" + |
| 671 |
" * @constructor\n" + |
| 672 |
" * @extends {goog.B}\n" + |
| 673 |
" */\n" + |
| 674 |
"goog.C = function() { this.fieldC = this.constructor; };\n" + |
| 675 |
"goog.inherits(goog.C, goog.B);\n" + |
| 676 |
"/** @constructor */ goog.C.FooC = function() {};\n" + |
| 677 |
"/** @return {void} */ goog.C.prototype.methodC = function() {};\n"); |
| 678 |
|
| 679 |
Symbol bCtor = getGlobalVar(table, "goog.B.prototype.constructor"); |
| 680 |
assertNotNull(bCtor); |
| 681 |
|
| 682 |
List<Reference> bRefs = table.getReferenceList(bCtor); |
| 683 |
assertEquals(2, bRefs.size()); |
| 684 |
assertEquals(11, bCtor.getDeclaration().getNode().getLineno()); |
| 685 |
|
| 686 |
Symbol cCtor = getGlobalVar(table, "goog.C.prototype.constructor"); |
| 687 |
assertNotNull(cCtor); |
| 688 |
|
| 689 |
List<Reference> cRefs = table.getReferenceList(cCtor); |
| 690 |
assertEquals(2, cRefs.size()); |
| 691 |
assertEquals(26, cCtor.getDeclaration().getNode().getLineno()); |
| 692 |
} |
| 693 |
|
| 694 |
public void testJSDocAssociationWithBadNamespace() { |
| 695 |
SymbolTable table = createSymbolTable( |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
"/** @constructor */ goog.Foo = function(){};"); |
| 700 |
|
| 701 |
Symbol foo = getGlobalVar(table, "goog.Foo"); |
| 702 |
assertNotNull(foo); |
| 703 |
|
| 704 |
JSDocInfo info = foo.getJSDocInfo(); |
| 705 |
assertNotNull(info); |
| 706 |
assertTrue(info.isConstructor()); |
| 707 |
} |
| 708 |
|
| 709 |
public void testMissingConstructorTag() { |
| 710 |
SymbolTable table = createSymbolTable( |
| 711 |
"function F() {" + |
| 712 |
" this.field1 = 3;" + |
| 713 |
"}" + |
| 714 |
"F.prototype.method1 = function() {" + |
| 715 |
" this.field1 = 5;" + |
| 716 |
"};" + |
| 717 |
"(new F()).method1();"); |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
assertNull(getGlobalVar(table, "F.prototype.field1")); |
| 722 |
|
| 723 |
Symbol sym = getGlobalVar(table, "F.prototype.method1"); |
| 724 |
assertEquals(1, table.getReferenceList(sym).size()); |
| 725 |
} |
| 726 |
|
| 727 |
public void testTypeCheckingOff() { |
| 728 |
options = new CompilerOptions(); |
| 729 |
|
| 730 |
|
| 731 |
SymbolTable table = createSymbolTable( |
| 732 |
"/** @contstructor */" + |
| 733 |
"function F() {" + |
| 734 |
" this.field1 = 3;" + |
| 735 |
"}" + |
| 736 |
"F.prototype.method1 = function() {" + |
| 737 |
" this.field1 = 5;" + |
| 738 |
"};" + |
| 739 |
"(new F()).method1();"); |
| 740 |
assertNull(getGlobalVar(table, "F.prototype.field1")); |
| 741 |
assertNull(getGlobalVar(table, "F.prototype.method1")); |
| 742 |
|
| 743 |
Symbol sym = getGlobalVar(table, "F"); |
| 744 |
assertEquals(3, table.getReferenceList(sym).size()); |
| 745 |
} |
| 746 |
|
| 747 |
public void testSuperClassReference() throws Exception { |
| 748 |
SymbolTable table = createSymbolTable( |
| 749 |
" var a = {b: {}};\n" |
| 750 |
+ "/** @constructor */\n" |
| 751 |
+ "a.b.BaseClass = function() {};\n" |
| 752 |
+ "a.b.BaseClass.prototype.doSomething = function() {\n" |
| 753 |
+ " alert('hi');\n" |
| 754 |
+ "};\n" |
| 755 |
+ "/**\n" |
| 756 |
+ " * @constructor\n" |
| 757 |
+ " * @extends {a.b.BaseClass}\n" |
| 758 |
+ " */\n" |
| 759 |
+ "a.b.DerivedClass = function() {};\n" |
| 760 |
+ "goog.inherits(a.b.DerivedClass, a.b.BaseClass);\n" |
| 761 |
+ "/** @override */\n" |
| 762 |
+ "a.b.DerivedClass.prototype.doSomething = function() {\n" |
| 763 |
+ " a.b.DerivedClass.superClass_.doSomething();\n" |
| 764 |
+ "};\n"); |
| 765 |
|
| 766 |
Symbol bad = getGlobalVar( |
| 767 |
table, "a.b.DerivedClass.superClass_.doSomething"); |
| 768 |
assertNull(bad); |
| 769 |
|
| 770 |
Symbol good = getGlobalVar( |
| 771 |
table, "a.b.BaseClass.prototype.doSomething"); |
| 772 |
assertNotNull(good); |
| 773 |
|
| 774 |
List<Reference> refs = table.getReferenceList(good); |
| 775 |
assertEquals(2, refs.size()); |
| 776 |
assertEquals("a.b.DerivedClass.superClass_.doSomething", |
| 777 |
refs.get(1).getNode().getQualifiedName()); |
| 778 |
} |
| 779 |
|
| 780 |
public void testInnerEnum() throws Exception { |
| 781 |
SymbolTable table = createSymbolTable( |
| 782 |
"var goog = {}; goog.ui = {};" |
| 783 |
+ " /** @constructor */\n" |
| 784 |
+ "goog.ui.Zippy = function() {};\n" |
| 785 |
+ "/** @enum {string} */\n" |
| 786 |
+ "goog.ui.Zippy.EventType = { TOGGLE: 'toggle' };\n"); |
| 787 |
|
| 788 |
Symbol eventType = getGlobalVar(table, "goog.ui.Zippy.EventType"); |
| 789 |
assertNotNull(eventType); |
| 790 |
assertTrue(eventType.getType().isEnumType()); |
| 791 |
|
| 792 |
Symbol toggle = getGlobalVar(table, "goog.ui.Zippy.EventType.TOGGLE"); |
| 793 |
assertNotNull(toggle); |
| 794 |
} |
| 795 |
|
| 796 |
public void testMethodInAnonObject1() throws Exception { |
| 797 |
SymbolTable table = createSymbolTable( |
| 798 |
"var a = {}; a.b = {}; a.b.c = function() {};"); |
| 799 |
Symbol a = getGlobalVar(table, "a"); |
| 800 |
Symbol ab = getGlobalVar(table, "a.b"); |
| 801 |
Symbol abc = getGlobalVar(table, "a.b.c"); |
| 802 |
|
| 803 |
assertNotNull(abc); |
| 804 |
assertEquals(1, table.getReferenceList(abc).size()); |
| 805 |
|
| 806 |
assertEquals("{b: {c: function (): undefined}}", a.getType().toString()); |
| 807 |
assertEquals("{c: function (): undefined}", ab.getType().toString()); |
| 808 |
assertEquals("function (): undefined", abc.getType().toString()); |
| 809 |
} |
| 810 |
|
| 811 |
public void testMethodInAnonObject2() throws Exception { |
| 812 |
SymbolTable table = createSymbolTable( |
| 813 |
"var a = {b: {c: function() {}}};"); |
| 814 |
Symbol a = getGlobalVar(table, "a"); |
| 815 |
Symbol ab = getGlobalVar(table, "a.b"); |
| 816 |
Symbol abc = getGlobalVar(table, "a.b.c"); |
| 817 |
|
| 818 |
assertNotNull(abc); |
| 819 |
assertEquals(1, table.getReferenceList(abc).size()); |
| 820 |
|
| 821 |
assertEquals("{b: {c: function (): undefined}}", a.getType().toString()); |
| 822 |
assertEquals("{c: function (): undefined}", ab.getType().toString()); |
| 823 |
assertEquals("function (): undefined", abc.getType().toString()); |
| 824 |
} |
| 825 |
|
| 826 |
public void testJSDocOnlySymbol() throws Exception { |
| 827 |
SymbolTable table = createSymbolTable( |
| 828 |
"/**\n" |
| 829 |
+ " * @param {number} x\n" |
| 830 |
+ " * @param y\n" |
| 831 |
+ " */\n" |
| 832 |
+ "var a;"); |
| 833 |
Symbol x = getDocVar(table, "x"); |
| 834 |
assertNotNull(x); |
| 835 |
assertEquals("number", x.getType().toString()); |
| 836 |
assertEquals(1, table.getReferenceList(x).size()); |
| 837 |
|
| 838 |
Symbol y = getDocVar(table, "y"); |
| 839 |
assertNotNull(x); |
| 840 |
assertEquals(null, y.getType()); |
| 841 |
assertEquals(1, table.getReferenceList(y).size()); |
| 842 |
} |
| 843 |
|
| 844 |
public void testNamespaceDefinitionOrder() throws Exception { |
| 845 |
|
| 846 |
|
| 847 |
SymbolTable table = createSymbolTable( |
| 848 |
"/** @const */ var goog = {};\n" |
| 849 |
+ "/** @constructor */ goog.dom.Foo = function() {};\n" |
| 850 |
+ "/** @const */ goog.dom = {};\n"); |
| 851 |
|
| 852 |
Symbol goog = getGlobalVar(table, "goog"); |
| 853 |
Symbol dom = getGlobalVar(table, "goog.dom"); |
| 854 |
Symbol Foo = getGlobalVar(table, "goog.dom.Foo"); |
| 855 |
|
| 856 |
assertNotNull(goog); |
| 857 |
assertNotNull(dom); |
| 858 |
assertNotNull(Foo); |
| 859 |
|
| 860 |
assertEquals(dom, goog.getPropertyScope().getSlot("dom")); |
| 861 |
assertEquals(Foo, dom.getPropertyScope().getSlot("Foo")); |
| 862 |
} |
| 863 |
|
| 864 |
public void testConstructorAlias() throws Exception { |
| 865 |
SymbolTable table = createSymbolTable( |
| 866 |
"/** @constructor */ var Foo = function() {};\n" + |
| 867 |
"/** desc */ Foo.prototype.bar = function() {};\n" + |
| 868 |
"/** @constructor */ var FooAlias = Foo;\n" + |
| 869 |
"/** desc */ FooAlias.prototype.baz = function() {};\n"); |
| 870 |
|
| 871 |
Symbol foo = getGlobalVar(table, "Foo"); |
| 872 |
Symbol fooAlias = getGlobalVar(table, "FooAlias"); |
| 873 |
Symbol bar = getGlobalVar(table, "Foo.prototype.bar"); |
| 874 |
Symbol baz = getGlobalVar(table, "Foo.prototype.baz"); |
| 875 |
Symbol bazAlias = getGlobalVar(table, "FooAlias.prototype.baz"); |
| 876 |
|
| 877 |
assertNotNull(foo); |
| 878 |
assertNotNull(fooAlias); |
| 879 |
assertNotNull(bar); |
| 880 |
assertNotNull(baz); |
| 881 |
assertNull(bazAlias); |
| 882 |
|
| 883 |
Symbol barScope = table.getSymbolForScope(table.getScope(bar)); |
| 884 |
assertNotNull(barScope); |
| 885 |
|
| 886 |
Symbol bazScope = table.getSymbolForScope(table.getScope(baz)); |
| 887 |
assertNotNull(bazScope); |
| 888 |
|
| 889 |
Symbol fooPrototype = foo.getPropertyScope().getSlot("prototype"); |
| 890 |
assertNotNull(fooPrototype); |
| 891 |
|
| 892 |
assertEquals(fooPrototype, barScope); |
| 893 |
assertEquals(fooPrototype, bazScope); |
| 894 |
} |
| 895 |
|
| 896 |
public void testSymbolForScopeOfNatives() throws Exception { |
| 897 |
SymbolTable table = createSymbolTable(""); |
| 898 |
|
| 899 |
|
| 900 |
Symbol sliceArg = getLocalVar(table, "sliceArg"); |
| 901 |
assertNotNull(sliceArg); |
| 902 |
|
| 903 |
Symbol scope = table.getSymbolForScope(table.getScope(sliceArg)); |
| 904 |
assertNotNull(scope); |
| 905 |
assertEquals(scope, getGlobalVar(table, "String.prototype.slice")); |
| 906 |
|
| 907 |
Symbol proto = getGlobalVar(table, "String.prototype"); |
| 908 |
assertEquals( |
| 909 |
"externs1", proto.getDeclaration().getNode().getSourceFileName()); |
| 910 |
} |
| 911 |
|
| 912 |
private void assertSymmetricOrdering( |
| 913 |
Ordering<Symbol> ordering, Symbol first, Symbol second) { |
| 914 |
assertTrue(ordering.compare(first, first) == 0); |
| 915 |
assertTrue(ordering.compare(second, second) == 0); |
| 916 |
assertTrue(ordering.compare(first, second) < 0); |
| 917 |
assertTrue(ordering.compare(second, first) > 0); |
| 918 |
} |
| 919 |
|
| 920 |
private Symbol getGlobalVar(SymbolTable table, String name) { |
| 921 |
return table.getGlobalScope().getQualifiedSlot(name); |
| 922 |
} |
| 923 |
|
| 924 |
private Symbol getDocVar(SymbolTable table, String name) { |
| 925 |
for (Symbol sym : table.getAllSymbols()) { |
| 926 |
if (sym.isDocOnlyParameter() && sym.getName().equals(name)) { |
| 927 |
return sym; |
| 928 |
} |
| 929 |
} |
| 930 |
return null; |
| 931 |
} |
| 932 |
|
| 933 |
private Symbol getLocalVar(SymbolTable table, String name) { |
| 934 |
for (SymbolScope scope : table.getAllScopes()) { |
| 935 |
if (!scope.isGlobalScope() && scope.isLexicalScope() && |
| 936 |
scope.getQualifiedSlot(name) != null) { |
| 937 |
return scope.getQualifiedSlot(name); |
| 938 |
} |
| 939 |
} |
| 940 |
return null; |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
private List<Symbol> getVars(SymbolTable table) { |
| 945 |
List<Symbol> result = Lists.newArrayList(); |
| 946 |
for (Symbol symbol : table.getAllSymbols()) { |
| 947 |
if (symbol.getDeclaration() != null && |
| 948 |
!symbol.getDeclaration().getNode().isFromExterns()) { |
| 949 |
result.add(symbol); |
| 950 |
} |
| 951 |
} |
| 952 |
return result; |
| 953 |
} |
| 954 |
|
| 955 |
private SymbolTable createSymbolTable(String input) { |
| 956 |
List<SourceFile> inputs = Lists.newArrayList( |
| 957 |
SourceFile.fromCode("in1", input)); |
| 958 |
List<SourceFile> externs = Lists.newArrayList( |
| 959 |
SourceFile.fromCode("externs1", EXTERNS)); |
| 960 |
|
| 961 |
Compiler compiler = new Compiler(); |
| 962 |
compiler.compile(externs, inputs, options); |
| 963 |
return assertSymbolTableValid(compiler.buildKnownSymbolTable()); |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
private SymbolTable assertSymbolTableValid(SymbolTable table) { |
| 971 |
Set<Symbol> allSymbols = Sets.newHashSet(table.getAllSymbols()); |
| 972 |
for (Symbol sym : table.getAllSymbols()) { |
| 973 |
|
| 974 |
|
| 975 |
assertEquals(sym, table.getScope(sym).getQualifiedSlot(sym.getName())); |
| 976 |
|
| 977 |
for (Reference ref : table.getReferences(sym)) { |
| 978 |
|
| 979 |
assertEquals(sym, ref.getSymbol()); |
| 980 |
} |
| 981 |
|
| 982 |
Symbol scope = table.getSymbolForScope(table.getScope(sym)); |
| 983 |
assertTrue( |
| 984 |
"The symbol's scope is a zombie scope that shouldn't exist.\n" + |
| 985 |
"Symbol: " + sym + "\n" + |
| 986 |
"Scope: " + table.getScope(sym), |
| 987 |
scope == null || allSymbols.contains(scope)); |
| 988 |
} |
| 989 |
|
| 990 |
|
| 991 |
Symbol global = getGlobalVar(table, SymbolTable.GLOBAL_THIS); |
| 992 |
assertNotNull(global); |
| 993 |
assertNotNull(global.getDeclaration()); |
| 994 |
assertEquals(Token.SCRIPT, global.getDeclaration().getNode().getType()); |
| 995 |
|
| 996 |
List<Reference> globalRefs = table.getReferenceList(global); |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
assertFalse(globalRefs.contains(global.getDeclaration())); |
| 1001 |
|
| 1002 |
return table; |
| 1003 |
} |
| 1004 |
} |