use of com.google.javascript.jscomp.SymbolTable.Symbol in project closure-compiler by google.
the class SymbolTableTest method testTypeCheckingOff.
@Test
public void testTypeCheckingOff() {
options = new CompilerOptions();
// Turning type-checking off is even worse than not annotating anything.
SymbolTable table = createSymbolTable(lines("/** @contstructor */", "function F() {", " this.field1 = 3;", "}", "F.prototype.method1 = function() {", " this.field1 = 5;", "};", "(new F()).method1();"));
assertThat(getGlobalVar(table, "F.prototype.field1")).isNull();
assertThat(getGlobalVar(table, "F.prototype.method1")).isNull();
Symbol sym = getGlobalVar(table, "F");
assertThat(table.getReferenceList(sym)).hasSize(3);
}
use of com.google.javascript.jscomp.SymbolTable.Symbol in project closure-compiler by google.
the class SymbolTableTest method testMethodInAnonObject1.
@Test
public void testMethodInAnonObject1() {
SymbolTable table = createSymbolTable("var a = {}; a.b = {}; a.b.c = function() {};");
Symbol a = getGlobalVar(table, "a");
Symbol ab = getGlobalVar(table, "a.b");
Symbol abc = getGlobalVar(table, "a.b.c");
assertThat(abc).isNotNull();
assertThat(table.getReferenceList(abc)).hasSize(1);
assertThat(a.getType().toString()).isEqualTo("{b: {c: function(): undefined}}");
assertThat(ab.getType().toString()).isEqualTo("{c: function(): undefined}");
assertThat(abc.getType().toString()).isEqualTo("function(): undefined");
}
use of com.google.javascript.jscomp.SymbolTable.Symbol in project closure-compiler by google.
the class SymbolTableTest method testGetEnclosingScope_FunctionNested.
@Test
public void testGetEnclosingScope_FunctionNested() {
SymbolTable table = createSymbolTable("function foo() { { const baz = 1; } }");
Symbol baz = getLocalVar(table, "baz");
SymbolScope bazScope = table.getEnclosingScope(baz.getDeclarationNode());
assertThat(bazScope).isNotNull();
assertThat(bazScope.isBlockScope()).isTrue();
assertThat(bazScope.getParentScope().isBlockScope()).isTrue();
Node foo = getGlobalVar(table, "foo").getDeclarationNode().getParent();
assertThat(bazScope.getParentScope().getParentScope().getRootNode()).isEqualTo(foo);
}
use of com.google.javascript.jscomp.SymbolTable.Symbol in project closure-compiler by google.
the class SymbolTableTest method testMultipleExtends.
@Test
public void testMultipleExtends() {
SymbolTable table = createSymbolTable(lines("/** @const */ var goog = goog || {};", "goog.inherits = function(x, y) {};", "/** @constructor */", "goog.A = function() { this.fieldA = this.constructor; };", "/** @constructor */ goog.A.FooA = function() {};", "/** @return {void} */ goog.A.prototype.methodA = function() {};", "/**", " * @constructor", " * @extends {goog.A}", " */", "goog.B = function() { this.fieldB = this.constructor; };", "goog.inherits(goog.B, goog.A);", "/** @return {void} */ goog.B.prototype.methodB = function() {};", "/**", " * @constructor", " * @extends {goog.A}", " */", "goog.B2 = function() { this.fieldB = this.constructor; };", "goog.inherits(goog.B2, goog.A);", "/** @constructor */ goog.B2.FooB = function() {};", "/** @return {void} */ goog.B2.prototype.methodB = function() {};", "/**", " * @constructor", " * @extends {goog.B}", " */", "goog.C = function() { this.fieldC = this.constructor; };", "goog.inherits(goog.C, goog.B);", "/** @constructor */ goog.C.FooC = function() {};", "/** @return {void} */ goog.C.prototype.methodC = function() {};"));
Symbol bCtor = getGlobalVar(table, "goog.B.prototype.constructor");
assertThat(bCtor).isNotNull();
List<Reference> bRefs = table.getReferenceList(bCtor);
assertThat(bRefs).hasSize(2);
assertThat(bCtor.getDeclaration().getNode().getLineno()).isEqualTo(11);
Symbol cCtor = getGlobalVar(table, "goog.C.prototype.constructor");
assertThat(cCtor).isNotNull();
List<Reference> cRefs = table.getReferenceList(cCtor);
assertThat(cRefs).hasSize(2);
assertThat(cCtor.getDeclaration().getNode().getLineno()).isEqualTo(26);
}
use of com.google.javascript.jscomp.SymbolTable.Symbol in project closure-compiler by google.
the class SymbolTableTest method testLocalVarInExterns.
@Test
public void testLocalVarInExterns() {
SymbolTable table = createSymbolTable("", "function customExternFn(customExternArg) {}");
Symbol arg = getLocalVar(table, "customExternArg");
List<Reference> refs = table.getReferenceList(arg);
assertThat(refs).hasSize(1);
Symbol fn = getGlobalVar(table, "customExternFn");
SymbolScope scope = table.getEnclosingScope(refs.get(0).getNode());
assertThat(scope.isGlobalScope()).isFalse();
assertThat(table.getSymbolForScope(scope)).isEqualTo(fn);
}
Aggregations