Search in sources :

Example 56 with Symbol

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

the class SymbolTableTest method testIncompleteNamespacedReferences.

@Test
public void testIncompleteNamespacedReferences() {
    SymbolTable table = createSymbolTable(lines("/** @constructor */", "my.dom.DomHelper = function(){};", "var y = my.dom.DomHelper;"));
    Symbol my = getGlobalVar(table, "my");
    assertThat(my).isNotNull();
    assertThat(table.getReferenceList(my)).hasSize(2);
    Symbol myDom = getGlobalVar(table, "my.dom");
    assertThat(myDom).isNotNull();
    assertThat(table.getReferenceList(myDom)).hasSize(2);
    Symbol myDomHelper = getGlobalVar(table, "my.dom.DomHelper");
    assertThat(myDomHelper).isNotNull();
    assertThat(table.getReferences(myDomHelper)).hasSize(2);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 57 with Symbol

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

the class SymbolTableTest method testGetEnclosingScope_Global.

@Test
public void testGetEnclosingScope_Global() {
    SymbolTable table = createSymbolTable("const baz = 1;");
    Symbol baz = getGlobalVar(table, "baz");
    SymbolScope bazScope = table.getEnclosingScope(baz.getDeclarationNode());
    assertThat(bazScope).isNotNull();
    assertThat(bazScope.isGlobalScope()).isTrue();
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) SymbolScope(com.google.javascript.jscomp.SymbolTable.SymbolScope) Test(org.junit.Test)

Example 58 with Symbol

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

the class SymbolTableTest method testSymbolForScopeOfNatives.

@Test
public void testSymbolForScopeOfNatives() {
    SymbolTable table = createSymbolTableWithDefaultExterns("");
    // From the externs.
    Symbol sliceArg = getLocalVar(table, "sliceArg");
    assertThat(sliceArg).isNotNull();
    Symbol scope = table.getSymbolForScope(table.getScope(sliceArg));
    assertThat(scope).isNotNull();
    assertThat(getGlobalVar(table, "String.prototype.slice")).isEqualTo(scope);
    Symbol proto = getGlobalVar(table, "String.prototype");
    assertThat(proto.getDeclaration().getNode().getSourceFileName()).isEqualTo("externs1");
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 59 with Symbol

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

the class SymbolTableTest method assertSymbolTableValid.

/**
 * Asserts that the symbol table meets some invariants. Returns the same table for easy chaining.
 */
private SymbolTable assertSymbolTableValid(SymbolTable table) {
    Set<Symbol> allSymbols = new HashSet<>();
    allSymbols.addAll(table.getAllSymbols());
    for (Symbol sym : table.getAllSymbols()) {
        // Make sure that grabbing the symbol's scope and looking it up
        // again produces the same symbol.
        assertThat(table.getScope(sym).getQualifiedSlot(sym.getName())).isEqualTo(sym);
        for (Reference ref : table.getReferences(sym)) {
            // Make sure that the symbol and reference are mutually linked.
            assertThat(ref.getSymbol()).isEqualTo(sym);
        }
        Symbol symbolForScope = table.getSymbolForScope(table.getScope(sym));
        if (symbolForScope != null) {
            assertWithMessage("The %s has a scope that shouldn't exist; a zombie scope.", sym).that(allSymbols).contains(symbolForScope);
        }
    }
    // Make sure that the global "this" is declared at the first input root.
    Symbol global = getGlobalVar(table, SymbolTable.GLOBAL_THIS);
    assertThat(global).isNotNull();
    assertThat(global.getDeclaration()).isNotNull();
    assertThat(global.getDeclaration().getNode().getToken()).isEqualTo(Token.SCRIPT);
    List<Reference> globalRefs = table.getReferenceList(global);
    // The main reference list should never contain the synthetic declaration
    // for the global root.
    assertThat(globalRefs).doesNotContain(global.getDeclaration());
    return table;
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) HashSet(java.util.HashSet)

Example 60 with Symbol

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

the class SymbolTableTest method testInnerEnum.

@Test
public void testInnerEnum() {
    SymbolTable table = createSymbolTable("var goog = {}; goog.ui = {};" + "  /** @constructor */" + "goog.ui.Zippy = function() {};" + "/** @enum {string} */" + "goog.ui.Zippy.EventType = { TOGGLE: 'toggle' };");
    Symbol eventType = getGlobalVar(table, "goog.ui.Zippy.EventType");
    assertThat(eventType).isNotNull();
    assertThat(eventType.getType().isEnumType()).isTrue();
    Symbol toggle = getGlobalVar(table, "goog.ui.Zippy.EventType.TOGGLE");
    assertThat(toggle).isNotNull();
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) 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