Search in sources :

Example 1 with Inlinability

use of com.google.javascript.jscomp.GlobalNamespace.Inlinability 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)

Example 2 with Inlinability

use of com.google.javascript.jscomp.GlobalNamespace.Inlinability 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

Inlinability (com.google.javascript.jscomp.GlobalNamespace.Inlinability)2 Name (com.google.javascript.jscomp.GlobalNamespace.Name)2 Test (org.junit.Test)2