Search in sources :

Example 1 with SymbolScope

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

the class SymbolTableTest method testGetEnclosingFunctionScope_FunctionNoNestedScopes.

@Test
public void testGetEnclosingFunctionScope_FunctionNoNestedScopes() {
    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)

Example 2 with SymbolScope

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

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

the class SymbolTableTest method testPrototypeSymbolEqualityForTwoPathsToSamePrototype.

@Test
public void testPrototypeSymbolEqualityForTwoPathsToSamePrototype() {
    String input = lines("/**", "* An employer.", "*", "* @param {String} name name of employer.", "* @param {String} address address of employer.", "* @constructor", "*/", "function Employer(name, address) {", "this.name = name;", "this.address = address;", "}", "", "/**", "* @return {String} information about an employer.", "*/", "Employer.prototype.getInfo = function() {", "return this.name + '.' + this.address;", "};");
    SymbolTable table = createSymbolTable(input);
    Symbol employer = getGlobalVar(table, "Employer");
    assertThat(employer).isNotNull();
    SymbolScope propertyScope = employer.getPropertyScope();
    assertThat(propertyScope).isNotNull();
    Symbol prototypeOfEmployer = propertyScope.getQualifiedSlot("prototype");
    assertThat(prototypeOfEmployer).isNotNull();
    Symbol employerPrototype = getGlobalVar(table, "Employer.prototype");
    assertThat(employerPrototype).isNotNull();
    new EqualsTester().addEqualityGroup(employerPrototype, prototypeOfEmployer).testEquals();
}
Also used : EqualsTester(com.google.common.testing.EqualsTester) Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) SymbolScope(com.google.javascript.jscomp.SymbolTable.SymbolScope) Test(org.junit.Test)

Example 4 with SymbolScope

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

the class SymbolTableTest method testGetEnclosingFunctionScope_GlobalScopeNested.

@Test
public void testGetEnclosingFunctionScope_GlobalScopeNested() {
    SymbolTable table = createSymbolTable("{ const baz = 1; }");
    Symbol baz = getLocalVar(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 5 with SymbolScope

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