Search in sources :

Example 51 with Symbol

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

the class SymbolTableTest method testGlobalVarReferences.

@Test
public void testGlobalVarReferences() {
    SymbolTable table = createSymbolTable("/** @type {number} */ var x = 5; x = 6;");
    Symbol x = getGlobalVar(table, "x");
    List<Reference> refs = table.getReferenceList(x);
    assertThat(refs).hasSize(2);
    assertThat(refs.get(0)).isEqualTo(x.getDeclaration());
    assertThat(refs.get(0).getNode().getParent().getToken()).isEqualTo(Token.VAR);
    assertThat(refs.get(1).getNode().getParent().getToken()).isEqualTo(Token.ASSIGN);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) Test(org.junit.Test)

Example 52 with Symbol

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

the class SymbolTableTest method testGoogRequireReferences.

@Test
public void testGoogRequireReferences() {
    SymbolTable table = createSymbolTableWithDefaultExterns(lines(// goog.require is defined in the default externs, among other Closure methods
    "goog.provide('goog.dom');", "goog.require('goog.dom');"));
    Symbol googRequire = getGlobalVar(table, "goog.require");
    assertThat(googRequire).isNotNull();
    assertThat(table.getReferences(googRequire)).hasSize(2);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 53 with Symbol

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

the class SymbolTableTest method verifySymbolReferencedInSecondFile.

private void verifySymbolReferencedInSecondFile(String firstFile, String secondFile, String symbolName) {
    SymbolTable table = createSymbolTableFromManySources(firstFile, secondFile);
    Symbol symbol = table.getAllSymbols().stream().filter((s) -> s.getSourceFileName().equals("file1.js") && s.getName().equals(symbolName)).findFirst().get();
    for (SymbolTable.Reference ref : table.getReferences(symbol)) {
        if (ref.getNode().getSourceFileName().equals("file2.js")) {
            return;
        }
    }
    Assert.fail("Did not find references in file2.js of symbol " + symbol);
}
Also used : Reference(com.google.javascript.jscomp.SymbolTable.Reference) Symbol(com.google.javascript.jscomp.SymbolTable.Symbol)

Example 54 with Symbol

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

the class SymbolTableTest method testMissingConstructorTag.

@Test
public void testMissingConstructorTag() {
    SymbolTable table = createSymbolTable(lines("function F() {", "  this.field1 = 3;", "}", "F.prototype.method1 = function() {", "  this.field1 = 5;", "};", "(new F()).method1();"));
    // Because the constructor tag is missing, this is going
    // to be missing a lot of inference.
    assertThat(getGlobalVar(table, "F.prototype.field1")).isNull();
    Symbol sym = getGlobalVar(table, "F.prototype.method1");
    assertThat(table.getReferenceList(sym)).hasSize(1);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 55 with Symbol

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

the class SymbolTableTest method testGlobalRichObjectReference.

@Test
public void testGlobalRichObjectReference() {
    SymbolTable table = createSymbolTable(lines("/** @constructor */", "function A(){};", "/** @type {?A} */ A.prototype.b;", "/** @type {A} */ var a = new A();", "function g() {", "  return a.b ? 'x' : 'y';", "}", "(function() {", "  var x; if (x) { x = a.b.b; } else { x = a.b.c; }", "  return x;", "})();"));
    Symbol ab = getGlobalVar(table, "a.b");
    assertThat(ab).isNull();
    Symbol propB = getGlobalVar(table, "A.prototype.b");
    assertThat(propB).isNotNull();
    assertThat(table.getReferenceList(propB)).hasSize(5);
}
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