Search in sources :

Example 81 with Name

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

the class GlobalNamespaceTest method testObjectLitSpreadOntoAGetProp.

@Test
public void testObjectLitSpreadOntoAGetProp() {
    GlobalNamespace namespace = parse("const obj = {a:3}; const ns = {}; ns.a = {...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);
    assertThat(nsA.isObjectLiteral()).isTrue();
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 82 with Name

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

the class AggressiveInlineAliasesTest method validateGlobalNamespace.

/**
 * To ensure that as we modify the AST, the GlobalNamespace stays up-to-date, we do a consistency
 * check after every unit test.
 *
 * <p>This check compares the names in the global namespace in the pass with a freshly-created
 * global namespace.
 */
public void validateGlobalNamespace(GlobalNamespace passGlobalNamespace) {
    GlobalNamespace expectedGlobalNamespace = new GlobalNamespace(getLastCompiler(), getLastCompiler().getJsRoot());
    // 3. each name has the same computed `Inlinability`
    for (Name expectedName : expectedGlobalNamespace.getNameForest()) {
        if (expectedName.inExterns()) {
            continue;
        }
        String fullName = expectedName.getFullName();
        Name actualName = passGlobalNamespace.getSlot(expectedName.getFullName());
        assertWithMessage(fullName).that(actualName).isNotNull();
        assertWithMessage(fullName).that(actualName.getAliasingGets()).isEqualTo(expectedName.getAliasingGets());
        assertWithMessage(fullName).that(actualName.getSubclassingGets()).isEqualTo(expectedName.getSubclassingGets());
        assertWithMessage(fullName).that(actualName.getLocalSets()).isEqualTo(expectedName.getLocalSets());
        assertWithMessage(fullName).that(actualName.getGlobalSets()).isEqualTo(expectedName.getGlobalSets());
        assertWithMessage(fullName).that(actualName.getDeleteProps()).isEqualTo(expectedName.getDeleteProps());
        assertWithMessage(fullName).that(actualName.getCallGets()).isEqualTo(expectedName.getCallGets());
        assertWithMessage(fullName + ": canCollapseOrInline()").that(actualName.canCollapseOrInline()).isEqualTo(expectedName.canCollapseOrInline());
        assertWithMessage(fullName + ": canCollapseOrInlineChildNames()").that(actualName.canCollapseOrInlineChildNames()).isEqualTo(expectedName.canCollapseOrInlineChildNames());
    }
    // Verify that no names in the actual name forest are not present in the expected name forest
    for (Name actualName : passGlobalNamespace.getNameForest()) {
        String actualFullName = actualName.getFullName();
        assertWithMessage(actualFullName).that(expectedGlobalNamespace.getSlot(actualFullName)).isNotNull();
    }
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name)

Example 83 with Name

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

the class GlobalNamespaceTest method testCanCollapseAliasedConstructorProperty_ignoringStaticInheritance.

@Test
public void testCanCollapseAliasedConstructorProperty_ignoringStaticInheritance() {
    GlobalNamespace namespace = parse(lines("/** @constructor */", "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 84 with Name

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

the class GlobalNamespaceTest method testObjectPatternRestInDeclaration.

@Test
public void testObjectPatternRestInDeclaration() {
    GlobalNamespace namespace = parse("const ns = {a: 3}; const {a, ...b} = ns;");
    Name ns = namespace.getSlot("ns");
    assertThat(ns.getGlobalSets()).isEqualTo(1);
    assertThat(ns.getTotalGets()).isEqualTo(1);
    assertThat(ns.getAliasingGets()).isEqualTo(1);
    Name nsA = namespace.getSlot("ns.a");
    assertThat(nsA.getGlobalSets()).isEqualTo(1);
    assertThat(nsA.getTotalGets()).isEqualTo(1);
    assertThat(nsA.getAliasingGets()).isEqualTo(1);
    Name b = namespace.getSlot("b");
    assertThat(b.getGlobalSets()).isEqualTo(1);
    assertThat(b.getTotalGets()).isEqualTo(0);
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 85 with Name

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

the class GlobalNamespaceTest method testCollapsing_forEscapedConstructor_consideringStaticInheritance.

@Test
public void testCollapsing_forEscapedConstructor_consideringStaticInheritance() {
    this.assumeStaticInheritanceIsNotUsed = false;
    GlobalNamespace namespace = parse(lines("/** @constructor */", "function Bar() {}", "use(Bar);"));
    Name bar = namespace.getSlot("Bar");
    // trivially true, already collapsed
    assertThat(bar.canCollapse()).isTrue();
    assertThat(bar.canCollapseUnannotatedChildNames()).isFalse();
}
Also used : 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