Search in sources :

Example 71 with Symbol

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);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 72 with Symbol

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");
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 73 with Symbol

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);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) SymbolScope(com.google.javascript.jscomp.SymbolTable.SymbolScope) Test(org.junit.Test)

Example 74 with Symbol

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);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) Test(org.junit.Test)

Example 75 with Symbol

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);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) SymbolScope(com.google.javascript.jscomp.SymbolTable.SymbolScope) Test(org.junit.Test)

Aggregations

Symbol (com.google.javascript.jscomp.SymbolTable.Symbol)78 Test (org.junit.Test)71 Reference (com.google.javascript.jscomp.SymbolTable.Reference)29 SymbolScope (com.google.javascript.jscomp.SymbolTable.SymbolScope)11 Node (com.google.javascript.rhino.Node)5 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)5 EqualsTester (com.google.common.testing.EqualsTester)1 JSDocInfo (com.google.javascript.rhino.JSDocInfo)1 FunctionType (com.google.javascript.rhino.jstype.FunctionType)1 HashSet (java.util.HashSet)1