Search in sources :

Example 61 with Symbol

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

the class SymbolTableTest method testGetEnclosingFunctionScope_FunctionInsideFunction.

@Test
public void testGetEnclosingFunctionScope_FunctionInsideFunction() {
    SymbolTable table = createSymbolTable("function foo() { function baz() {} }");
    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)

Example 62 with Symbol

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

the class SymbolTableTest method testClassPropertiesDisableModuleRewriting.

@Test
public void testClassPropertiesDisableModuleRewriting() {
    SymbolTable table = createSymbolTableFromManySources(lines("goog.module('foo');", "class Person {", "  constructor() {", "    /** @type {string} */", "    this.lastName;", "  }", "}", "exports = {Person};"), lines("goog.module('bar');", "const {Person} = goog.require('foo');", "let /** !Person */ p;", "p.lastName;"));
    Symbol lastName = table.getAllSymbols().stream().filter((s) -> s.getName().equals("lastName")).findFirst().get();
    assertThat(table.getReferences(lastName)).hasSize(2);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 63 with Symbol

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

the class SymbolTableTest method testRestParameter.

@Test
public void testRestParameter() {
    String input = "function f(...x) {} f(1, 2, 3);";
    SymbolTable table = createSymbolTable(input);
    Symbol f = getGlobalVar(table, "f");
    assertThat(f).isNotNull();
    Symbol x = table.getParameterInFunction(f, "x");
    assertThat(x).isNotNull();
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 64 with Symbol

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

the class SymbolTableTest method testEnums.

@Test
public void testEnums() {
    SymbolTable table = createSymbolTableFromManySources(lines("goog.module('foo');", "/** @enum {number} */", "const Color = {RED: 1};", "exports.Color = Color;", "Color.RED;"), lines("goog.module('bar');", "const foo = goog.require('foo');", "foo.Color.RED;"), lines("goog.module('baz');", "const {Color} = goog.require('foo');", "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(4);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 65 with Symbol

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

the class SymbolTableTest method testLocalVarReferences.

@Test
public void testLocalVarReferences() {
    SymbolTable table = createSymbolTable("function f(x) { return x; }");
    Symbol x = getLocalVar(table, "x");
    List<Reference> refs = table.getReferenceList(x);
    assertThat(refs).hasSize(2);
    assertThat(refs.get(0)).isEqualTo(x.getDeclaration());
    assertThat(refs.get(0).getNode().getParent().getToken()).isEqualTo(Token.PARAM_LIST);
    assertThat(refs.get(1).getNode().getParent().getToken()).isEqualTo(Token.RETURN);
}
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