use of com.google.javascript.jscomp.SymbolTable.Symbol 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.Symbol in project closure-compiler by google.
the class SymbolTableTest method testFieldReferences.
@Test
public void testFieldReferences() {
SymbolTable table = createSymbolTable(lines("/** @constructor */ var DomHelper = function(){", " /** @type {number} */ this.field = 3;", "};", "function f() { ", " return (new DomHelper()).field + (new DomHelper()).field; };"));
Symbol field = getGlobalVar(table, "DomHelper.prototype.field");
assertThat(table.getReferences(field)).hasSize(3);
}
use of com.google.javascript.jscomp.SymbolTable.Symbol in project closure-compiler by google.
the class SymbolTableTest method testConstructorAlias.
@Test
public void testConstructorAlias() {
SymbolTable table = createSymbolTable(lines("/** @constructor */ var Foo = function() {};", "/** desc */ Foo.prototype.bar = function() {};", "/** @constructor */ var FooAlias = Foo;", "/** desc */ FooAlias.prototype.baz = function() {};"));
Symbol foo = getGlobalVar(table, "Foo");
Symbol fooAlias = getGlobalVar(table, "FooAlias");
Symbol bar = getGlobalVar(table, "Foo.prototype.bar");
Symbol baz = getGlobalVar(table, "Foo.prototype.baz");
Symbol bazAlias = getGlobalVar(table, "FooAlias.prototype.baz");
assertThat(foo).isNotNull();
assertThat(fooAlias).isNotNull();
assertThat(bar).isNotNull();
assertThat(baz).isNotNull();
assertThat(baz).isEqualTo(bazAlias);
Symbol barScope = table.getSymbolForScope(table.getScope(bar));
assertThat(barScope).isNotNull();
Symbol bazScope = table.getSymbolForScope(table.getScope(baz));
assertThat(bazScope).isNotNull();
Symbol fooPrototype = foo.getPropertyScope().getSlot("prototype");
assertThat(fooPrototype).isNotNull();
assertThat(barScope).isEqualTo(fooPrototype);
assertThat(bazScope).isEqualTo(fooPrototype);
}
use of com.google.javascript.jscomp.SymbolTable.Symbol in project closure-compiler by google.
the class SymbolTableTest method testObjectLiteralWithMemberFunction.
@Test
public void testObjectLiteralWithMemberFunction() {
String input = lines("var obj = { fn() {} }; obj.fn();");
SymbolTable table = createSymbolTable(input);
Symbol objFn = getGlobalVar(table, "obj.fn");
assertThat(objFn).isNotNull();
List<Reference> references = table.getReferenceList(objFn);
assertThat(references).hasSize(2);
// The declaration node corresponds to "fn", not "fn() {}", in the source info.
Node declaration = objFn.getDeclarationNode();
assertThat(declaration.getCharno()).isEqualTo(12);
assertThat(declaration.getLength()).isEqualTo(2);
}
use of com.google.javascript.jscomp.SymbolTable.Symbol in project closure-compiler by google.
the class SymbolTableTest method testGetEnclosingFunctionScope_FunctionNested.
@Test
public void testGetEnclosingFunctionScope_FunctionNested() {
SymbolTable table = createSymbolTable("function foo() { { { const baz = 1; } } }");
Symbol baz = getLocalVar(table, "baz");
SymbolScope bazFunctionScope = table.getEnclosingFunctionScope(baz.getDeclarationNode());
assertThat(bazFunctionScope).isNotNull();
Node foo = getGlobalVar(table, "foo").getDeclarationNode().getParent();
assertThat(bazFunctionScope.getRootNode()).isEqualTo(foo);
}
Aggregations