Search in sources :

Example 61 with Name

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

the class GlobalNamespaceTest method testLhsCastInAssignment.

@Test
public void testLhsCastInAssignment() {
    // The type of the cast doesn't matter.
    // Casting is only legal JS syntax in simple assignments, not with destructuring or declaration.
    GlobalNamespace namespace = parse("const ns = {}; const b = 5; /** @type {*} */ (ns.a) = b;");
    Name ns = namespace.getSlot("ns");
    assertThat(ns.getGlobalSets()).isEqualTo(1);
    assertThat(ns.getTotalGets()).isEqualTo(0);
    Name nsA = namespace.getSlot("ns.a");
    // TODO(b/127505242): Should be 1.
    assertThat(nsA.getGlobalSets()).isEqualTo(0);
    // TODO(b/127505242): Should be 0.
    assertThat(nsA.getTotalGets()).isEqualTo(1);
    Name b = namespace.getSlot("b");
    assertThat(b.getGlobalSets()).isEqualTo(1);
    assertThat(b.getAliasingGets()).isEqualTo(1);
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 62 with Name

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

the class GlobalNamespaceTest method moduleLevelNamesAreCaptured_esExportDefaultFunctionDecl.

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

Example 63 with Name

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

the class GlobalNamespaceTest method testCannotCollapseObjectLitPropertyEscapedWithOptChainCall.

@Test
public void testCannotCollapseObjectLitPropertyEscapedWithOptChainCall() {
    GlobalNamespace namespace = parse("var foo = {prop: 0}; use?.(foo);");
    Name fooProp = namespace.getSlot("foo.prop");
    // We should not convert foo.prop -> foo$prop because use(foo) might read foo.prop
    assertThat(fooProp.canCollapse()).isFalse();
}
Also used : Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 64 with Name

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

the class GlobalNamespaceTest method testCanCollapseAliasedClassProperty_ignoringStaticInheritance.

@Test
public void testCanCollapseAliasedClassProperty_ignoringStaticInheritance() {
    GlobalNamespace namespace = parse(lines("class Foo {} 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 65 with Name

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

the class GlobalNamespaceTest method testInlinability_forAliasingPropertyOnEscapedConstructor_ignoringStaticInheritance.

@Test
public void testInlinability_forAliasingPropertyOnEscapedConstructor_ignoringStaticInheritance() {
    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();
    // We should convert references to `Bar.aliasOfFoo.prop` to become `Foo.prop`
    // because...
    assertThat(barAliasInlinability.shouldInlineUsages()).isTrue();
    // However, we should not remove the assignment (`Bar.aliasOfFoo = Foo`) that creates the alias,
    // because "BarAlias" still needs to be inlined to "Bar", which will create another usage of
    // "Bar.aliasOfFoo" in the last line, We will locate the value to inline Bar.aliasOfFoo
    // again from `Bar.aliasOfFoo = Foo`.
    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