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);
}
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);
}
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");
}
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);
}
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;
}
Aggregations