Search in sources :

Example 21 with Reference

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());
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) Test(org.junit.Test)

Example 22 with Reference

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);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) Test(org.junit.Test)

Example 23 with Reference

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);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) Test(org.junit.Test)

Example 24 with Reference

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);
}
Also used : HashMap(java.util.HashMap) Reference(com.google.javascript.jscomp.SymbolTable.Reference) Test(org.junit.Test)

Example 25 with Reference

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);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) Test(org.junit.Test)

Aggregations

Reference (com.google.javascript.jscomp.SymbolTable.Reference)29 Symbol (com.google.javascript.jscomp.SymbolTable.Symbol)27 Test (org.junit.Test)27 SymbolScope (com.google.javascript.jscomp.SymbolTable.SymbolScope)2 HashMap (java.util.HashMap)2 Node (com.google.javascript.rhino.Node)1 FunctionType (com.google.javascript.rhino.jstype.FunctionType)1 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)1 HashSet (java.util.HashSet)1