Search in sources :

Example 16 with Reference

use of com.google.javascript.jscomp.SymbolTable.Reference 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)

Example 17 with Reference

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

the class SymbolTableTest method testMultipleExtends.

@Test
public void testMultipleExtends() {
    SymbolTable table = createSymbolTable(lines("/** @const */ var goog = goog || {};", "goog.inherits = function(x, y) {};", "/** @constructor */", "goog.A = function() { this.fieldA = this.constructor; };", "/** @constructor */ goog.A.FooA = function() {};", "/** @return {void} */ goog.A.prototype.methodA = function() {};", "/**", " * @constructor", " * @extends {goog.A}", " */", "goog.B = function() { this.fieldB = this.constructor; };", "goog.inherits(goog.B, goog.A);", "/** @return {void} */ goog.B.prototype.methodB = function() {};", "/**", " * @constructor", " * @extends {goog.A}", " */", "goog.B2 = function() { this.fieldB = this.constructor; };", "goog.inherits(goog.B2, goog.A);", "/** @constructor */ goog.B2.FooB = function() {};", "/** @return {void} */ goog.B2.prototype.methodB = function() {};", "/**", " * @constructor", " * @extends {goog.B}", " */", "goog.C = function() { this.fieldC = this.constructor; };", "goog.inherits(goog.C, goog.B);", "/** @constructor */ goog.C.FooC = function() {};", "/** @return {void} */ goog.C.prototype.methodC = function() {};"));
    Symbol bCtor = getGlobalVar(table, "goog.B.prototype.constructor");
    assertThat(bCtor).isNotNull();
    List<Reference> bRefs = table.getReferenceList(bCtor);
    assertThat(bRefs).hasSize(2);
    assertThat(bCtor.getDeclaration().getNode().getLineno()).isEqualTo(11);
    Symbol cCtor = getGlobalVar(table, "goog.C.prototype.constructor");
    assertThat(cCtor).isNotNull();
    List<Reference> cRefs = table.getReferenceList(cCtor);
    assertThat(cRefs).hasSize(2);
    assertThat(cCtor.getDeclaration().getNode().getLineno()).isEqualTo(26);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) Test(org.junit.Test)

Example 18 with Reference

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

the class SymbolTableTest method testPrototypeReferences5.

@Test
public void testPrototypeReferences5() {
    SymbolTable table = createSymbolTable("var goog = {}; /** @constructor */ goog.Foo = function() {};");
    Symbol fooPrototype = getGlobalVar(table, "goog.Foo.prototype");
    assertThat(fooPrototype).isNotNull();
    List<Reference> refs = table.getReferenceList(fooPrototype);
    assertThat(refs).hasSize(1);
    assertThat(refs.get(0).getNode().getToken()).isEqualTo(Token.GETPROP);
    // Make sure that the ctor and its prototype are declared at the
    // same node.
    assertThat(table.getReferenceList(getGlobalVar(table, "goog.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 19 with Reference

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

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

the class SymbolTableTest method testSuperKeywords.

@Test
public void testSuperKeywords() {
    SymbolTable table = createSymbolTable(lines("class Parent { doFoo() {} }", "class Child extends Parent {", "  constructor() { super(); }", "  doFoo() { super.doFoo(); }", "}"));
    Symbol parentClass = getGlobalVar(table, "Parent");
    long numberOfSuperReferences = table.getReferenceList(parentClass).stream().filter((Reference ref) -> ref.getNode().isSuper()).count();
    // TODO(b/112359882): change number of refs to 2 once Es6ConvertSuper pass is moved after type
    // checks.
    assertThat(numberOfSuperReferences).isEqualTo(1);
}
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