use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method googLoadModule_containsExports.
@Test
public void googLoadModule_containsExports() {
GlobalNamespace namespace = parseAndGatherModuleData(lines("goog.loadModule(function(exports) {", " goog.module('m');", " const x = 0;", " return exports;", "});"));
ModuleMetadata metadata = lastCompiler.getModuleMetadataMap().getModulesByGoogNamespace().get("m");
Name exports = namespace.getNameFromModule(metadata, "exports");
assertThat(exports.getGlobalSets()).isEqualTo(0);
}
use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method testObjectAssignOntoAGetProp.
@Test
public void testObjectAssignOntoAGetProp() {
GlobalNamespace namespace = parse("const obj = {a:3}; const ns = {}; ns.a = Object.assign({}, 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);
// `ns.a` is considered an "OTHER" type
assertThat(nsA.isObjectLiteral()).isFalse();
}
use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method updateRefNodeRejectsNodeWithExistingRefs.
@Test
public void updateRefNodeRejectsNodeWithExistingRefs() {
GlobalNamespace namespace = parse(lines(// declaration ref
"const A = 3;", // use ref
"A;"));
Name nameA = namespace.getOwnSlot("A");
Ref declarationRef = nameA.getDeclaration();
// use ref is 2nd
Ref useRef = Iterables.get(nameA.getRefs(), 1);
Node useNode = useRef.getNode();
assertThrows(IllegalArgumentException.class, () -> nameA.updateRefNode(declarationRef, useNode));
}
use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method testConditionalDestructuringDoesNotHideAliasingGet.
@Test
public void testConditionalDestructuringDoesNotHideAliasingGet() {
GlobalNamespace namespace = parse(lines(//
"", "const ns1 = {a: 3};", "const ns2 = {b: 3};", // Creates an aliasing get for both ns1 and ns2
"const {a, b} = Math.random() ? ns1 : ns2;", ""));
Name ns1 = namespace.getSlot("ns1");
assertThat(ns1.getAliasingGets()).isEqualTo(1);
assertThat(ns1.getTotalGets()).isEqualTo(1);
Name ns2 = namespace.getSlot("ns2");
assertThat(ns2.getAliasingGets()).isEqualTo(1);
assertThat(ns2.getTotalGets()).isEqualTo(1);
}
use of com.google.javascript.jscomp.GlobalNamespace.Name 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();
}
Aggregations