use of com.google.javascript.jscomp.SymbolTable.Reference in project closure-compiler by google.
the class SymbolTableTest method testSymbolSuperclassStaticInheritance.
@Test
public void testSymbolSuperclassStaticInheritance() {
// set this option so that typechecking sees untranspiled classes.
// TODO(b/76025401): remove this option after class transpilation is always post-typechecking
options.setSkipUnsupportedPasses(false);
SymbolTable table = createSymbolTable(lines("class Bar { static staticMethod() {} }", "class Foo extends Bar {", " act() { Foo.staticMethod(); }", "}"));
Symbol foo = getGlobalVar(table, "Foo");
Symbol bar = getGlobalVar(table, "Bar");
FunctionType barType = checkNotNull(bar.getType().toMaybeFunctionType());
FunctionType fooType = checkNotNull(foo.getType().toMaybeFunctionType());
FunctionType superclassCtorType = fooType.getSuperClassConstructor();
assertType(superclassCtorType).isEqualTo(barType);
Symbol superSymbol = table.getSymbolDeclaredBy(superclassCtorType);
assertThat(superSymbol).isEqualTo(bar);
Symbol barStaticMethod = getGlobalVar(table, "Bar.staticMethod");
ImmutableList<Reference> barStaticMethodRefs = table.getReferenceList(barStaticMethod);
assertThat(barStaticMethodRefs).hasSize(2);
assertThat(barStaticMethodRefs.get(0)).isEqualTo(barStaticMethod.getDeclaration());
// We recognize that the call to Foo.staticMethod() is actually a call to Bar.staticMethod().
assertNode(barStaticMethodRefs.get(1).getNode()).matchesQualifiedName("Foo.staticMethod");
}
use of com.google.javascript.jscomp.SymbolTable.Reference in project closure-compiler by google.
the class SymbolTableTest method testDuplicatedWindowExternsMerged.
@Test
public void testDuplicatedWindowExternsMerged() {
// Add window so that it triggers logic that defines all global externs on window.
// See DeclaredGlobalExternsOnWindow.java pass.
String externs = lines("/** @externs */", "var window", "/** @type {number} */ var foo;");
String mainCode = lines("foo = 2;", "window.foo = 1;");
SymbolTable table = createSymbolTable(mainCode, externs);
Map<String, Integer> refsPerFile = new HashMap<>();
for (Reference reference : table.getReferenceList(getGlobalVar(table, "foo"))) {
if (!reference.getNode().isIndexable()) {
continue;
}
String file = reference.getSourceFile().getName();
refsPerFile.put(file, refsPerFile.getOrDefault(file, 0) + 1);
}
assertThat(refsPerFile).containsExactly("in1", 2, "externs1", 1);
}
use of com.google.javascript.jscomp.SymbolTable.Reference in project closure-compiler by google.
the class SymbolTableTest method testLocalThisReferences.
@Test
public void testLocalThisReferences() {
SymbolTable table = createSymbolTable("/** @constructor */ function F() { this.foo = 3; this.bar = 5; }");
Symbol f = getGlobalVar(table, "F");
assertThat(f).isNotNull();
Symbol t = table.getParameterInFunction(f, "this");
assertThat(t).isNotNull();
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 testDeclarationDisagreement.
@Test
public void testDeclarationDisagreement() {
SymbolTable table = createSymbolTable(lines("/** @const */ var goog = goog || {};", "/** @param {!Function} x */", "goog.addSingletonGetter2 = function(x) {};", "/** Wakka wakka wakka */", "goog.addSingletonGetter = goog.addSingletonGetter2;", "/** @param {!Function} x */", "goog.addSingletonGetter = function(x) {};"));
Symbol method = getGlobalVar(table, "goog.addSingletonGetter");
List<Reference> refs = table.getReferenceList(method);
assertThat(refs).hasSize(2);
// Note that the declaration should show up second.
assertThat(method.getDeclaration().getNode().getLineno()).isEqualTo(7);
assertThat(refs.get(1).getNode().getLineno()).isEqualTo(5);
}
use of com.google.javascript.jscomp.SymbolTable.Reference in project closure-compiler by google.
the class SymbolTableTest method testReferencesInJSDocName.
@Test
public void testReferencesInJSDocName() {
String code = "/** @param {Object} x */ function f(x) {}";
SymbolTable table = createSymbolTable(code);
Symbol x = getLocalVar(table, "x");
assertThat(x).isNotNull();
List<Reference> refs = table.getReferenceList(x);
assertThat(refs).hasSize(2);
assertThat(refs.get(0).getNode().getCharno()).isEqualTo(code.indexOf("x) {"));
assertThat(refs.get(1).getNode().getCharno()).isEqualTo(code.indexOf("x */"));
assertThat(refs.get(0).getNode().getSourceFileName()).isEqualTo("in1");
}
Aggregations