Search in sources :

Example 66 with Symbol

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

the class SymbolTableTest method testPrototypeReferences2.

@Test
public void testPrototypeReferences2() {
    SymbolTable table = createSymbolTable("/** @constructor */" + "function Snork() {}" + "Snork.prototype.baz = 3;");
    Symbol prototype = getGlobalVar(table, "Snork.prototype");
    assertThat(prototype).isNotNull();
    List<Reference> refs = table.getReferenceList(prototype);
    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 67 with Symbol

use of com.google.javascript.jscomp.SymbolTable.Symbol 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 68 with Symbol

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

the class SymbolTableTest method testPrototypeReferences_es6Class.

@Test
public void testPrototypeReferences_es6Class() {
    SymbolTable table = createSymbolTable(lines("class DomHelper { method() {} }"));
    Symbol prototype = getGlobalVar(table, "DomHelper.prototype");
    assertThat(prototype).isNotNull();
    List<Reference> refs = table.getReferenceList(prototype);
    // The class declaration creates an implicit .prototype reference.
    assertWithMessage(refs.toString()).that(refs).hasSize(1);
    assertNode(refs.get(0).getNode().getParent()).hasToken(Token.CLASS);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) Test(org.junit.Test)

Example 69 with Symbol

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

the class SymbolTableTest method testMethodReferencesMissingTypeInfo.

@Test
public void testMethodReferencesMissingTypeInfo() {
    SymbolTable table = createSymbolTable(lines("/**", " * @constructor", " * @extends {Missing}", " */ var DomHelper = function(){};", "/** method */ DomHelper.prototype.method = function() {", "  this.method();", "};", "function f() { ", "  (new DomHelper()).method();", "};"));
    Symbol method = getGlobalVar(table, "DomHelper.prototype.method");
    assertThat(table.getReferences(method)).hasSize(3);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 70 with Symbol

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

the class SymbolTableTest method testReferencesInJSDocType2.

@Test
public void testReferencesInJSDocType2() {
    SymbolTable table = createSymbolTableWithDefaultExterns("/** @param {string} x */ function f(x) {}");
    Symbol str = getGlobalVar(table, "String");
    assertThat(str).isNotNull();
    List<Reference> refs = table.getReferenceList(str);
    // We're going to pick up a lot of references from the externs,
    // so it's not meaningful to check the number of references.
    // We really want to make sure that all the references are in the externs,
    // except the last one.
    assertThat(refs.size()).isGreaterThan(1);
    int last = refs.size() - 1;
    for (int i = 0; i < refs.size(); i++) {
        Reference ref = refs.get(i);
        assertThat(ref.getNode().isFromExterns()).isEqualTo(i != last);
        if (!ref.getNode().isFromExterns()) {
            assertThat(ref.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

Symbol (com.google.javascript.jscomp.SymbolTable.Symbol)78 Test (org.junit.Test)71 Reference (com.google.javascript.jscomp.SymbolTable.Reference)29 SymbolScope (com.google.javascript.jscomp.SymbolTable.SymbolScope)11 Node (com.google.javascript.rhino.Node)5 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)5 EqualsTester (com.google.common.testing.EqualsTester)1 JSDocInfo (com.google.javascript.rhino.JSDocInfo)1 FunctionType (com.google.javascript.rhino.jstype.FunctionType)1 HashSet (java.util.HashSet)1