Search in sources :

Example 41 with Name

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

the class GlobalNamespaceTest method googLoadModule_containsExports.

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

Example 42 with Name

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

the class GlobalNamespaceTest method testObjectAssignOntoAGetProp.

@Test
public void testObjectAssignOntoAGetProp() {
    GlobalNamespace namespace = parse("const obj = {a:3}; const ns = {}; ns.a = Object.assign({}, obj); ");
    Name obj = namespace.getSlot("obj");
    assertThat(obj.getGlobalSets()).isEqualTo(1);
    assertThat(obj.getAliasingGets()).isEqualTo(1);
    Name objA = namespace.getSlot("obj.a");
    assertThat(objA.getGlobalSets()).isEqualTo(1);
    assertThat(objA.getAliasingGets()).isEqualTo(0);
    Name ns = namespace.getSlot("ns");
    assertThat(ns.getGlobalSets()).isEqualTo(1);
    assertThat(ns.getAliasingGets()).isEqualTo(0);
    Name nsA = namespace.getSlot("ns.a");
    assertThat(nsA.getGlobalSets()).isEqualTo(1);
    assertThat(nsA.getAliasingGets()).isEqualTo(0);
    // `ns.a` is considered an "OTHER" type
    assertThat(nsA.isObjectLiteral()).isFalse();
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 43 with Name

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

the class GlobalNamespaceTest method updateRefNodeRejectsNodeWithExistingRefs.

@Test
public void updateRefNodeRejectsNodeWithExistingRefs() {
    GlobalNamespace namespace = parse(lines(// declaration ref
    "const A = 3;", // use ref
    "A;"));
    Name nameA = namespace.getOwnSlot("A");
    Ref declarationRef = nameA.getDeclaration();
    // use ref is 2nd
    Ref useRef = Iterables.get(nameA.getRefs(), 1);
    Node useNode = useRef.getNode();
    assertThrows(IllegalArgumentException.class, () -> nameA.updateRefNode(declarationRef, useNode));
}
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 44 with Name

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

the class GlobalNamespaceTest method testConditionalDestructuringDoesNotHideAliasingGet.

@Test
public void testConditionalDestructuringDoesNotHideAliasingGet() {
    GlobalNamespace namespace = parse(lines(// 
    "", "const ns1 = {a: 3};", "const ns2 = {b: 3};", // Creates an aliasing get for both ns1 and ns2
    "const {a, b} = Math.random() ? ns1 : ns2;", ""));
    Name ns1 = namespace.getSlot("ns1");
    assertThat(ns1.getAliasingGets()).isEqualTo(1);
    assertThat(ns1.getTotalGets()).isEqualTo(1);
    Name ns2 = namespace.getSlot("ns2");
    assertThat(ns2.getAliasingGets()).isEqualTo(1);
    assertThat(ns2.getTotalGets()).isEqualTo(1);
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 45 with Name

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

the class GlobalNamespaceTest method testInlinability_forAliasingPropertyOnEscapedConstructor_consideringStaticInheritance.

@Test
public void testInlinability_forAliasingPropertyOnEscapedConstructor_consideringStaticInheritance() {
    this.assumeStaticInheritanceIsNotUsed = false;
    GlobalNamespace namespace = parse(lines("var prop = 1;", "/** @constructor */", "var Foo = function() {}", "", "Foo.prop = prop;", "", "/** @constructor */", "function Bar() {}", // alias Foo
    "Bar.aliasOfFoo = Foo;", // uninlinable alias of Bar
    "use(Bar);", // inlinable alias of Bar
    "const BarAlias = Bar;", "alert(Bar.aliasOfFoo.prop);", "alert(BarAlias.aliasOfFoo.prop);"));
    Name barAliasOfFoo = namespace.getSlot("Bar.aliasOfFoo");
    Inlinability barAliasInlinability = barAliasOfFoo.calculateInlinability();
    assertThat(barAliasInlinability.shouldInlineUsages()).isFalse();
    assertThat(barAliasInlinability.shouldRemoveDeclaration()).isFalse();
}
Also used : Inlinability(com.google.javascript.jscomp.GlobalNamespace.Inlinability) 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