Search in sources :

Example 21 with Symbol

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

Example 22 with Symbol

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

Example 23 with Symbol

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

Example 24 with Symbol

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

Example 25 with Symbol

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

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