Search in sources :

Example 11 with Reference

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

the class SymbolTableTest method testDottedReferencesInJSDocType.

@Test
public void testDottedReferencesInJSDocType() {
    SymbolTable table = createSymbolTable(lines("var goog = {};", "/** @constructor */ goog.Foo = function() {}", "/** @type {goog.Foo} */ var x;", "/** @param {goog.Foo} x */ function f(x) {}", "/** @return {function(): goog.Foo} */ function g() {}", "/**", " * @constructor", " * @extends {goog.Foo}", " */ function Sub() {}"));
    Symbol foo = getGlobalVar(table, "goog.Foo");
    assertThat(foo).isNotNull();
    List<Reference> refs = table.getReferenceList(foo);
    assertThat(refs).hasSize(5);
    assertThat(refs.get(0).getNode().getLineno()).isEqualTo(2);
    assertThat(refs.get(0).getNode().getCharno()).isEqualTo(25);
    assertThat(refs.get(0).getNode().getLength()).isEqualTo(3);
    assertThat(refs.get(1).getNode().getLineno()).isEqualTo(3);
    assertThat(refs.get(1).getNode().getCharno()).isEqualTo(16);
    assertThat(refs.get(2).getNode().getLineno()).isEqualTo(4);
    assertThat(refs.get(2).getNode().getCharno()).isEqualTo(17);
    assertThat(refs.get(3).getNode().getLineno()).isEqualTo(5);
    assertThat(refs.get(3).getNode().getCharno()).isEqualTo(30);
    assertThat(refs.get(4).getNode().getLineno()).isEqualTo(8);
    assertThat(refs.get(4).getNode().getCharno()).isEqualTo(18);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) Test(org.junit.Test)

Example 12 with Reference

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

the class SymbolTableTest method testReferencesInJSDocType.

@Test
public void testReferencesInJSDocType() {
    SymbolTable table = createSymbolTable(lines("/** @constructor */ function Foo() {}", "/** @type {Foo} */ var x;", "/** @param {Foo} x */ function f(x) {}", "/** @return {function(): Foo} */ function g() {}", "/**", " * @constructor", " * @extends {Foo}", " */ function Sub() {}"));
    Symbol foo = getGlobalVar(table, "Foo");
    assertThat(foo).isNotNull();
    List<Reference> refs = table.getReferenceList(foo);
    assertThat(refs).hasSize(5);
    assertThat(refs.get(0).getNode().getLineno()).isEqualTo(1);
    assertThat(refs.get(0).getNode().getCharno()).isEqualTo(29);
    assertThat(refs.get(0).getNode().getLength()).isEqualTo(3);
    assertThat(refs.get(1).getNode().getLineno()).isEqualTo(2);
    assertThat(refs.get(1).getNode().getCharno()).isEqualTo(11);
    assertThat(refs.get(2).getNode().getLineno()).isEqualTo(3);
    assertThat(refs.get(2).getNode().getCharno()).isEqualTo(12);
    assertThat(refs.get(3).getNode().getLineno()).isEqualTo(4);
    assertThat(refs.get(3).getNode().getCharno()).isEqualTo(25);
    assertThat(refs.get(4).getNode().getLineno()).isEqualTo(7);
    assertThat(refs.get(4).getNode().getCharno()).isEqualTo(13);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) Test(org.junit.Test)

Example 13 with Reference

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

the class SymbolTableTest method testSuperClassReference.

@Test
public void testSuperClassReference() {
    SymbolTable table = createSymbolTable("  var a = {b: {}};" + "/** @constructor */" + "a.b.BaseClass = function() {};" + "a.b.BaseClass.prototype.doSomething = function() {" + "  alert('hi');" + "};" + "/**" + " * @constructor" + " * @extends {a.b.BaseClass}" + " */" + "a.b.DerivedClass = function() {};" + "goog.inherits(a.b.DerivedClass, a.b.BaseClass);" + "/** @override */" + "a.b.DerivedClass.prototype.doSomething = function() {" + "  a.b.DerivedClass.superClass_.doSomething();" + "};");
    Symbol bad = getGlobalVar(table, "a.b.DerivedClass.superClass_.doSomething");
    assertThat(bad).isNull();
    Symbol good = getGlobalVar(table, "a.b.BaseClass.prototype.doSomething");
    assertThat(good).isNotNull();
    List<Reference> refs = table.getReferenceList(good);
    assertThat(refs).hasSize(2);
    assertThat(refs.get(1).getNode().getQualifiedName()).isEqualTo("a.b.DerivedClass.superClass_.doSomething");
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) Test(org.junit.Test)

Example 14 with Reference

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

the class SymbolTableTest method testLocalThisReferences2.

public void testLocalThisReferences2() throws Exception {
    SymbolTable table = createSymbolTable("/** @constructor */ function F() {}\n" + "F.prototype.baz = function() { this.foo = 3; this.bar = 5; };");
    Symbol baz = getGlobalVar(table, "F.prototype.baz");
    assertNotNull(baz);
    Symbol t = table.getParameterInFunction(baz, "this");
    assertNotNull(t);
    List<Reference> refs = table.getReferenceList(t);
    assertThat(refs).hasSize(2);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference)

Example 15 with Reference

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

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