Search in sources :

Example 6 with SymbolScope

use of com.google.javascript.jscomp.SymbolTable.SymbolScope 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 7 with SymbolScope

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

the class SymbolTableTest method testGetEnclosingFunctionScope_GlobalScopeNoNesting.

@Test
public void testGetEnclosingFunctionScope_GlobalScopeNoNesting() {
    SymbolTable table = createSymbolTable("const baz = 1;");
    Symbol baz = getGlobalVar(table, "baz");
    SymbolScope bazFunctionScope = table.getEnclosingFunctionScope(baz.getDeclarationNode());
    assertThat(bazFunctionScope).isNotNull();
    assertThat(bazFunctionScope.isGlobalScope()).isTrue();
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) SymbolScope(com.google.javascript.jscomp.SymbolTable.SymbolScope) Test(org.junit.Test)

Example 8 with SymbolScope

use of com.google.javascript.jscomp.SymbolTable.SymbolScope 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 9 with SymbolScope

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

the class SymbolTableTest method testGetEnclosingScope_Global.

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

Example 10 with SymbolScope

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

the class SymbolTableTest method testGlobalVarInExterns.

@Test
public void testGlobalVarInExterns() {
    SymbolTable table = createSymbolTable("customExternFn(1);", "function customExternFn(customExternArg) {}");
    Symbol fn = getGlobalVar(table, "customExternFn");
    List<Reference> refs = table.getReferenceList(fn);
    assertThat(refs).hasSize(3);
    SymbolScope scope = table.getEnclosingScope(refs.get(0).getNode());
    assertThat(scope.isGlobalScope()).isTrue();
    assertThat(table.getSymbolForScope(scope).getName()).isEqualTo(SymbolTable.GLOBAL_THIS);
}
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)11 SymbolScope (com.google.javascript.jscomp.SymbolTable.SymbolScope)11 Test (org.junit.Test)11 Node (com.google.javascript.rhino.Node)4 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)4 Reference (com.google.javascript.jscomp.SymbolTable.Reference)2 EqualsTester (com.google.common.testing.EqualsTester)1