Search in sources :

Example 86 with Name

use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.

the class GlobalNamespaceTest method moduleLevelNamesAreCaptured_esExportClassDecl.

@Test
public void moduleLevelNamesAreCaptured_esExportClassDecl() {
    GlobalNamespace namespace = parseAndGatherModuleData("export class Foo {}");
    ModuleMetadata metadata = lastCompiler.getModuleMetadataMap().getModulesByPath().get("test.js");
    Name x = namespace.getNameFromModule(metadata, "Foo");
    assertThat(x).isNotNull();
    assertNode(x.getDeclaration().getNode().getParent()).hasToken(Token.CLASS);
}
Also used : ModuleMetadata(com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 87 with Name

use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.

the class GlobalNamespaceTest method testThrowPreventsCollapsingChildNames.

@Test
public void testThrowPreventsCollapsingChildNames() {
    GlobalNamespace namespace = parse(lines("const X = {Y: 1};", "", "function fn(a) {", // inside an if statement or the thrown value isn't a conditional expression.
    "  if (a) {", "    throw a ? X : {};", "  }", "}", "", "console.log(fn(true).Y);"));
    Name nameX = namespace.getSlot("X");
    assertThat(nameX.canCollapseUnannotatedChildNames()).isFalse();
    Name propY = namespace.getSlot("X.Y");
    assertThat(propY.canCollapse()).isFalse();
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 88 with Name

use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.

the class GlobalNamespaceTest method testCanCollapseAliasedInterfaceProperty_ignoringStaticInheritance.

@Test
public void testCanCollapseAliasedInterfaceProperty_ignoringStaticInheritance() {
    GlobalNamespace namespace = parse(lines("/** @interface */", "var Foo = function() {}", "", "Foo.prop = prop;", "use(Foo);"));
    Name fooProp = namespace.getSlot("Foo.prop");
    // We should still convert Foo.prop -> Foo$prop, even though use(Foo) might read Foo.prop,
    // because Foo is a constructor
    assertThat(fooProp.canCollapse()).isTrue();
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 89 with Name

use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.

the class GlobalNamespaceTest method removeOneRefOfAPairOfTwins.

@Test
public void removeOneRefOfAPairOfTwins() {
    GlobalNamespace namespace = parse(lines(// 
    "let A;", // A will have twin refs here
    "const B = A = 3;"));
    Name nameA = namespace.getOwnSlot("A");
    // first ref is declaration of A
    // second is the SET twin
    Ref setTwinRef = Iterables.get(nameA.getRefs(), 1);
    // third is the GET twin
    Ref getTwinRef = Iterables.get(nameA.getRefs(), 2);
    // see confirmTwinsAreCreated() for verification of the original twin relationship
    Node oldNode = getTwinRef.getNode();
    // confirm that they are both associated with oldNode
    assertThat(nameA.getRefsForNode(oldNode)).containsExactly(setTwinRef, getTwinRef);
    nameA.removeTwinRefs(setTwinRef);
    assertThat(nameA.getRefs()).doesNotContain(setTwinRef);
    assertThat(nameA.getRefs()).doesNotContain(getTwinRef);
    assertThat(nameA.getRefsForNode(oldNode)).isEmpty();
}
Also used : Ref(com.google.javascript.jscomp.GlobalNamespace.Ref) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 90 with Name

use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.

the class GlobalNamespaceTest method googModule_containsExports_explicitAssign.

@Test
public void googModule_containsExports_explicitAssign() {
    GlobalNamespace namespace = parseAndGatherModuleData("goog.module('m'); const x = 0; exports = {x};");
    ModuleMetadata metadata = lastCompiler.getModuleMetadataMap().getModulesByGoogNamespace().get("m");
    Name exports = namespace.getNameFromModule(metadata, "exports");
    assertThat(exports.getGlobalSets()).isEqualTo(1);
    assertThat(namespace.getNameFromModule(metadata, "x").getGlobalSets()).isEqualTo(1);
}
Also used : ModuleMetadata(com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Aggregations

Name (com.google.javascript.jscomp.GlobalNamespace.Name)95 Test (org.junit.Test)72 Ref (com.google.javascript.jscomp.GlobalNamespace.Ref)25 Node (com.google.javascript.rhino.Node)22 ModuleMetadata (com.google.javascript.jscomp.modules.ModuleMetadataMap.ModuleMetadata)12 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)12 JSDocInfo (com.google.javascript.rhino.JSDocInfo)5 AstChange (com.google.javascript.jscomp.GlobalNamespace.AstChange)2 Inlinability (com.google.javascript.jscomp.GlobalNamespace.Inlinability)2 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)2 JSType (com.google.javascript.rhino.jstype.JSType)2 LinkedHashSet (java.util.LinkedHashSet)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 PropagateConstantAnnotationsOverVars (com.google.javascript.jscomp.Normalize.PropagateConstantAnnotationsOverVars)1 Module (com.google.javascript.jscomp.modules.Module)1 FeatureSet (com.google.javascript.jscomp.parsing.parser.FeatureSet)1 QualifiedName (com.google.javascript.rhino.QualifiedName)1 ArrayList (java.util.ArrayList)1