Search in sources :

Example 6 with Reference

use of com.google.javascript.jscomp.SymbolTable.Reference in project closure-compiler by google.

the class SymbolTableTest method testSymbolSuperclassStaticInheritance.

@Test
public void testSymbolSuperclassStaticInheritance() {
    // set this option so that typechecking sees untranspiled classes.
    // TODO(b/76025401): remove this option after class transpilation is always post-typechecking
    options.setSkipUnsupportedPasses(false);
    SymbolTable table = createSymbolTable(lines("class Bar { static staticMethod() {} }", "class Foo extends Bar {", "  act() { Foo.staticMethod(); }", "}"));
    Symbol foo = getGlobalVar(table, "Foo");
    Symbol bar = getGlobalVar(table, "Bar");
    FunctionType barType = checkNotNull(bar.getType().toMaybeFunctionType());
    FunctionType fooType = checkNotNull(foo.getType().toMaybeFunctionType());
    FunctionType superclassCtorType = fooType.getSuperClassConstructor();
    assertType(superclassCtorType).isEqualTo(barType);
    Symbol superSymbol = table.getSymbolDeclaredBy(superclassCtorType);
    assertThat(superSymbol).isEqualTo(bar);
    Symbol barStaticMethod = getGlobalVar(table, "Bar.staticMethod");
    ImmutableList<Reference> barStaticMethodRefs = table.getReferenceList(barStaticMethod);
    assertThat(barStaticMethodRefs).hasSize(2);
    assertThat(barStaticMethodRefs.get(0)).isEqualTo(barStaticMethod.getDeclaration());
    // We recognize that the call to Foo.staticMethod() is actually a call to Bar.staticMethod().
    assertNode(barStaticMethodRefs.get(1).getNode()).matchesQualifiedName("Foo.staticMethod");
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) FunctionType(com.google.javascript.rhino.jstype.FunctionType) Test(org.junit.Test)

Example 7 with Reference

use of com.google.javascript.jscomp.SymbolTable.Reference in project closure-compiler by google.

the class SymbolTableTest method testDuplicatedWindowExternsMerged.

@Test
public void testDuplicatedWindowExternsMerged() {
    // Add window so that it triggers logic that defines all global externs on window.
    // See DeclaredGlobalExternsOnWindow.java pass.
    String externs = lines("/** @externs */", "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"))) {
        if (!reference.getNode().isIndexable()) {
            continue;
        }
        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 8 with Reference

use of com.google.javascript.jscomp.SymbolTable.Reference in project closure-compiler by google.

the class SymbolTableTest method testLocalThisReferences.

@Test
public void testLocalThisReferences() {
    SymbolTable table = createSymbolTable("/** @constructor */ function F() { this.foo = 3; this.bar = 5; }");
    Symbol f = getGlobalVar(table, "F");
    assertThat(f).isNotNull();
    Symbol t = table.getParameterInFunction(f, "this");
    assertThat(t).isNotNull();
    List<Reference> refs = table.getReferenceList(t);
    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 9 with Reference

use of com.google.javascript.jscomp.SymbolTable.Reference in project closure-compiler by google.

the class SymbolTableTest method testDeclarationDisagreement.

@Test
public void testDeclarationDisagreement() {
    SymbolTable table = createSymbolTable(lines("/** @const */ var goog = goog || {};", "/** @param {!Function} x */", "goog.addSingletonGetter2 = function(x) {};", "/** Wakka wakka wakka */", "goog.addSingletonGetter = goog.addSingletonGetter2;", "/** @param {!Function} x */", "goog.addSingletonGetter = function(x) {};"));
    Symbol method = getGlobalVar(table, "goog.addSingletonGetter");
    List<Reference> refs = table.getReferenceList(method);
    assertThat(refs).hasSize(2);
    // Note that the declaration should show up second.
    assertThat(method.getDeclaration().getNode().getLineno()).isEqualTo(7);
    assertThat(refs.get(1).getNode().getLineno()).isEqualTo(5);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) Test(org.junit.Test)

Example 10 with Reference

use of com.google.javascript.jscomp.SymbolTable.Reference in project closure-compiler by google.

the class SymbolTableTest method testReferencesInJSDocName.

@Test
public void testReferencesInJSDocName() {
    String code = "/** @param {Object} x */ function f(x) {}";
    SymbolTable table = createSymbolTable(code);
    Symbol x = getLocalVar(table, "x");
    assertThat(x).isNotNull();
    List<Reference> refs = table.getReferenceList(x);
    assertThat(refs).hasSize(2);
    assertThat(refs.get(0).getNode().getCharno()).isEqualTo(code.indexOf("x) {"));
    assertThat(refs.get(1).getNode().getCharno()).isEqualTo(code.indexOf("x */"));
    assertThat(refs.get(0).getNode().getSourceFileName()).isEqualTo("in1");
}
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