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);
}
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);
}
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();
}
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();
}
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();
}
Aggregations