Search in sources :

Example 16 with Symbol

use of com.google.javascript.jscomp.SymbolTable.Symbol in project closure-compiler by google.

the class SymbolTableTest method testEnumsProvidedAsMemberOfNamespace.

@Test
public void testEnumsProvidedAsMemberOfNamespace() {
    SymbolTable table = createSymbolTableFromManySources(lines("goog.provide('foo.bar');", "/** @enum {number} */", "foo.bar.Color = {RED: 1};", "const /** !foo.bar.Color */ color = ", "  foo.bar.Color.RED;"));
    Symbol red = table.getAllSymbols().stream().filter((s) -> s.getName().equals("RED")).findFirst().get();
    // Make sure that RED belongs to the scope of Color that is defined in the first file and not
    // third. Because the third file also has "Color" enum that has the same type.
    assertThat(table.getScope(red).getSymbolForScope().getSourceFileName()).isEqualTo("file1.js");
    assertThat(table.getReferences(red)).hasSize(2);
    Symbol colorEnum = table.getAllSymbols().stream().filter((s) -> s.getName().equals("Color")).findFirst().get();
    assertThat(table.getReferences(colorEnum)).hasSize(3);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 17 with Symbol

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

Example 18 with Symbol

use of com.google.javascript.jscomp.SymbolTable.Symbol in project closure-compiler by google.

the class SymbolTableTest method testGetEnclosingScope_GlobalNested.

@Test
public void testGetEnclosingScope_GlobalNested() {
    SymbolTable table = createSymbolTable("{ const baz = 1; }");
    Symbol baz = getLocalVar(table, "baz");
    SymbolScope bazScope = table.getEnclosingScope(baz.getDeclarationNode());
    assertThat(bazScope).isNotNull();
    assertThat(bazScope.isBlockScope()).isTrue();
    assertThat(bazScope.getParentScope().isGlobalScope()).isTrue();
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) SymbolScope(com.google.javascript.jscomp.SymbolTable.SymbolScope) Test(org.junit.Test)

Example 19 with Symbol

use of com.google.javascript.jscomp.SymbolTable.Symbol in project closure-compiler by google.

the class SymbolTableTest method testMethodReferences.

@Test
public void testMethodReferences() {
    SymbolTable table = createSymbolTable(lines("/** @constructor */ var DomHelper = function(){};", "/** method */ DomHelper.prototype.method = function() {};", "function f() { ", "  (new DomHelper()).method(); (new DomHelper()).method(); };"));
    Symbol method = getGlobalVar(table, "DomHelper.prototype.method");
    assertThat(table.getReferences(method)).hasSize(3);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 20 with Symbol

use of com.google.javascript.jscomp.SymbolTable.Symbol in project closure-compiler by google.

the class SymbolTableTest method testGlobalThisReferences3.

@Test
public void testGlobalThisReferences3() {
    SymbolTable table = createSymbolTable("this.foo = {}; this.foo.bar = {};");
    Symbol global = getGlobalVar(table, "*global*");
    assertThat(global).isNotNull();
    List<Reference> refs = table.getReferenceList(global);
    assertThat(refs).hasSize(2);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) 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