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