Search in sources :

Example 31 with Symbol

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

the class SymbolTableTest method exerciseGoogRequireReferenceCorrectly.

private void exerciseGoogRequireReferenceCorrectly(String inputProvidingNamespace) {
    SymbolTable table = createSymbolTableFromManySources(inputProvidingNamespace, lines("goog.provide('module.two');", "goog.require('module.one');", "goog.requireType('module.one');", "goog.forwardDeclare('module.one');"), lines("goog.module('module.three');", "const one = goog.require('module.one');", "const oneType = goog.requireType('module.one');", "const oneForward = goog.forwardDeclare('module.one');"), lines("goog.module('module.four');", "goog.module.declareLegacyNamespace();", "const one = goog.require('module.one');", "const oneType = goog.requireType('module.one');", "const oneForward = goog.forwardDeclare('module.one');"));
    Symbol namespace = getGlobalVar(table, "ns$module.one");
    assertThat(namespace).isNotNull();
    // 1 ref from declaration and then 2 refs in each of 3 files.
    assertThat(table.getReferences(namespace)).hasSize(10);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol)

Example 32 with Symbol

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

the class SymbolTableTest method testObjectLiteral.

@Test
public void testObjectLiteral() {
    SymbolTable table = createSymbolTable("var obj = {foo: 0};");
    Symbol foo = getGlobalVar(table, "obj.foo");
    assertThat(foo.getName()).isEqualTo("foo");
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 33 with Symbol

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

the class SymbolTableTest method testNamespaceReferencesInGoogRequire.

@Test
public void testNamespaceReferencesInGoogRequire() {
    SymbolTable table = createSymbolTable(lines("goog.provide('my.dom');", "goog.require('my.dom');"));
    Symbol googRequire = getGlobalVar(table, "ns$my.dom");
    assertThat(googRequire).isNotNull();
    assertThat(table.getReferences(googRequire)).hasSize(2);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 34 with Symbol

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

the class SymbolTableTest method testEnumsWithDirectExport.

@Test
public void testEnumsWithDirectExport() {
    SymbolTable table = createSymbolTableFromManySources(lines("goog.module('foo');", "/** @enum {number} */", "exports.Color = {RED: 1}; // exports.Color = Color;"), 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();
    assertThat(table.getScope(red).getSymbolForScope().getSourceFileName()).isEqualTo("file1.js");
    assertThat(table.getReferences(red)).hasSize(3);
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Test(org.junit.Test)

Example 35 with Symbol

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

the class SymbolTableTest method testSymbolSuperclassStaticInheritance.

@Test
public void testSymbolSuperclassStaticInheritance() {
    // set this option so that typechecking sees untranspiled classes.
    // TODO(b/76025401): remove this option after class transpilation is always post-typechecking
    options.setSkipUnsupportedPasses(false);
    SymbolTable table = createSymbolTable(lines("class Bar { static staticMethod() {} }", "class Foo extends Bar {", "  act() { Foo.staticMethod(); }", "}"));
    Symbol foo = getGlobalVar(table, "Foo");
    Symbol bar = getGlobalVar(table, "Bar");
    FunctionType barType = checkNotNull(bar.getType().toMaybeFunctionType());
    FunctionType fooType = checkNotNull(foo.getType().toMaybeFunctionType());
    FunctionType superclassCtorType = fooType.getSuperClassConstructor();
    assertType(superclassCtorType).isEqualTo(barType);
    Symbol superSymbol = table.getSymbolDeclaredBy(superclassCtorType);
    assertThat(superSymbol).isEqualTo(bar);
    Symbol barStaticMethod = getGlobalVar(table, "Bar.staticMethod");
    ImmutableList<Reference> barStaticMethodRefs = table.getReferenceList(barStaticMethod);
    assertThat(barStaticMethodRefs).hasSize(2);
    assertThat(barStaticMethodRefs.get(0)).isEqualTo(barStaticMethod.getDeclaration());
    // We recognize that the call to Foo.staticMethod() is actually a call to Bar.staticMethod().
    assertNode(barStaticMethodRefs.get(1).getNode()).matchesQualifiedName("Foo.staticMethod");
}
Also used : Symbol(com.google.javascript.jscomp.SymbolTable.Symbol) Reference(com.google.javascript.jscomp.SymbolTable.Reference) FunctionType(com.google.javascript.rhino.jstype.FunctionType) 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