use of com.google.javascript.jscomp.SymbolTable.Reference in project closure-compiler by google.
the class SymbolTableTest method testPrototypeReferences3.
@Test
public void testPrototypeReferences3() {
SymbolTable table = createSymbolTable("/** @constructor */ function Foo() {}");
Symbol fooPrototype = getGlobalVar(table, "Foo.prototype");
assertThat(fooPrototype).isNotNull();
List<Reference> refs = table.getReferenceList(fooPrototype);
assertThat(refs).hasSize(1);
assertThat(refs.get(0).getNode().getToken()).isEqualTo(Token.NAME);
// Make sure that the ctor and its prototype are declared at the
// same node.
assertThat(table.getReferenceList(getGlobalVar(table, "Foo")).get(0).getNode()).isEqualTo(refs.get(0).getNode());
}
use of com.google.javascript.jscomp.SymbolTable.Reference in project closure-compiler by google.
the class SymbolTableTest method testGlobalVarReferences.
@Test
public void testGlobalVarReferences() {
SymbolTable table = createSymbolTable("/** @type {number} */ var x = 5; x = 6;");
Symbol x = getGlobalVar(table, "x");
List<Reference> refs = table.getReferenceList(x);
assertThat(refs).hasSize(2);
assertThat(refs.get(0)).isEqualTo(x.getDeclaration());
assertThat(refs.get(0).getNode().getParent().getToken()).isEqualTo(Token.VAR);
assertThat(refs.get(1).getNode().getParent().getToken()).isEqualTo(Token.ASSIGN);
}
use of com.google.javascript.jscomp.SymbolTable.Reference in project closure-compiler by google.
the class SymbolTableTest method testGlobalThisPropertyReferences.
@Test
public void testGlobalThisPropertyReferences() {
SymbolTable table = createSymbolTable("/** @constructor */ function Foo() {} this.Foo;");
Symbol foo = getGlobalVar(table, "Foo");
assertThat(foo).isNotNull();
List<Reference> refs = table.getReferenceList(foo);
assertThat(refs).hasSize(2);
}
use of com.google.javascript.jscomp.SymbolTable.Reference in project closure-compiler by google.
the class SymbolTableTest method testDuplicatedWindowExternsMergedWithWindowPrototype.
@Test
public void testDuplicatedWindowExternsMergedWithWindowPrototype() {
// Add window so that it triggers logic that defines all global externs on window.
// See DeclaredGlobalExternsOnWindow.java pass.
// Also add Window class as it affects symbols (e.g. window.foo is set on Window.prototype.foo
// instead).
String externs = lines("/** @externs */", "/** @constructor */ function Window() {}", "/** @type {!Window} */ var window;", "/** @type {number} */ var foo;");
String mainCode = lines("foo = 2;", "window.foo = 1;");
SymbolTable table = createSymbolTable(mainCode, externs);
Map<String, Integer> refsPerFile = new HashMap<>();
for (Reference reference : table.getReferenceList(getGlobalVar(table, "foo"))) {
String file = reference.getSourceFile().getName();
refsPerFile.put(file, refsPerFile.getOrDefault(file, 0) + 1);
}
assertThat(refsPerFile).containsExactly("in1", 2, "externs1", 1);
}
use of com.google.javascript.jscomp.SymbolTable.Reference in project closure-compiler by google.
the class SymbolTableTest method testLocalVarReferences.
@Test
public void testLocalVarReferences() {
SymbolTable table = createSymbolTable("function f(x) { return x; }");
Symbol x = getLocalVar(table, "x");
List<Reference> refs = table.getReferenceList(x);
assertThat(refs).hasSize(2);
assertThat(refs.get(0)).isEqualTo(x.getDeclaration());
assertThat(refs.get(0).getNode().getParent().getToken()).isEqualTo(Token.PARAM_LIST);
assertThat(refs.get(1).getNode().getParent().getToken()).isEqualTo(Token.RETURN);
}
Aggregations