Search in sources :

Example 76 with Symbol

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

the class SymbolTableTest method testSymbolsForType.

@Test
public void testSymbolsForType() {
    SymbolTable table = createSymbolTableWithDefaultExterns(lines("function random() { return 1; }", "/** @constructor */ function Foo() {}", "/** @constructor */ function Bar() {}", "var x = random() ? new Foo() : new Bar();"));
    Symbol x = getGlobalVar(table, "x");
    Symbol foo = getGlobalVar(table, "Foo");
    Symbol bar = getGlobalVar(table, "Bar");
    Symbol fooPrototype = getGlobalVar(table, "Foo.prototype");
    Symbol fn = getGlobalVar(table, "Function");
    assertThat(table.getAllSymbolsForTypeOf(x)).containsExactly(foo, bar);
    assertThat(table.getAllSymbolsForTypeOf(foo)).containsExactly(fn);
    assertThat(table.getAllSymbolsForTypeOf(fooPrototype)).containsExactly(foo);
    assertThat(table.getSymbolDeclaredBy(foo.getType().toMaybeFunctionType())).isEqualTo(foo);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 77 with Symbol

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

the class SymbolTableTest method testNamespaceDefinitionOrder.

@Test
public void testNamespaceDefinitionOrder() {
    // Sometimes, weird things can happen where the files appear in
    // a strange order. We need to make sure we're robust against this.
    SymbolTable table = createSymbolTable(lines("/** @constructor */ goog.dom.Foo = function() {};", "/** @const */ goog.dom = {};", "/** @const */ var goog = {};"));
    Symbol goog = getGlobalVar(table, "goog");
    Symbol dom = getGlobalVar(table, "goog.dom");
    Symbol foo = getGlobalVar(table, "goog.dom.Foo");
    assertThat(goog).isNotNull();
    assertThat(dom).isNotNull();
    assertThat(foo).isNotNull();
    assertThat(goog.getPropertyScope().getSlot("dom")).isEqualTo(dom);
    assertThat(dom.getPropertyScope().getSlot("Foo")).isEqualTo(foo);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 78 with Symbol

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

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