use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method testCannotCollapseAliasedConstructorProperty_consideringStaticInheritance.
@Test
public void testCannotCollapseAliasedConstructorProperty_consideringStaticInheritance() {
this.assumeStaticInheritanceIsNotUsed = false;
GlobalNamespace namespace = parse(lines("/** @constructor */", "var Foo = function() {}", "", "Foo.prop = prop;", "use(Foo);"));
Name fooProp = namespace.getSlot("Foo.prop");
assertThat(fooProp.canCollapse()).isFalse();
}
use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method testScanFromNodeNoticesHasOwnProperty.
@Test
public void testScanFromNodeNoticesHasOwnProperty() {
GlobalNamespace namespace = parse("const x = {bar: 0}; const y = x; y.hasOwnProperty('bar');");
Name xName = namespace.getOwnSlot("x");
Name yName = namespace.getOwnSlot("y");
assertThat(xName.usesHasOwnProperty()).isFalse();
assertThat(yName.usesHasOwnProperty()).isTrue();
// Replace "const baz = y.bar" with "const baz = x.bar"
Node root = lastCompiler.getJsRoot();
Node yDotHasOwnProperty = // SCRIPT
root.getFirstChild().getLastChild().getFirstFirstChild();
// `y`
Node yNode = yDotHasOwnProperty.getFirstChild();
assertNode(yDotHasOwnProperty).matchesQualifiedName("y.hasOwnProperty");
Node xNode = IR.name("x");
yNode.replaceWith(xNode);
// Rescan the new nodes
// In this case the new node is `x.hasOwnProperty`, since that's the full, new qualified name.
namespace.scanNewNodes(ImmutableSet.of(createGlobalAstChangeForNode(root, yDotHasOwnProperty)));
assertThat(xName.usesHasOwnProperty()).isTrue();
}
use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method localAssignmentWillNotBeConsideredADeclaration.
@Test
public void localAssignmentWillNotBeConsideredADeclaration() {
GlobalNamespace namespace = parse("");
Name n = namespace.createNameForTesting("a");
Ref set1 = n.addSingleRefForTesting(Ref.Type.SET_FROM_GLOBAL);
Ref localSet = n.addSingleRefForTesting(Ref.Type.SET_FROM_LOCAL);
assertThat(n.getRefs()).containsExactly(set1, localSet).inOrder();
assertThat(n.getDeclaration()).isEqualTo(set1);
assertThat(n.getGlobalSets()).isEqualTo(1);
assertThat(n.getLocalSets()).isEqualTo(1);
n.removeRef(set1);
// local set will not be used as the declaration
assertThat(n.getDeclaration()).isNull();
assertThat(n.getGlobalSets()).isEqualTo(0);
}
use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method hook.
@Test
public void hook() {
GlobalNamespace namespace = parse("var a = a ? a : {}");
Name a = namespace.getSlot("a");
assertThat(a).isNotNull();
assertThat(a.getRefs()).hasSize(3);
assertThat(a.getLocalSets()).isEqualTo(0);
assertThat(a.getGlobalSets()).isEqualTo(1);
}
use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method testObjectLitSpreadAliasInAssign.
@Test
public void testObjectLitSpreadAliasInAssign() {
GlobalNamespace namespace = parse("const ns = {a: 3}; const x = {}; ({a: x.y} = {...ns});");
Name ns = namespace.getSlot("ns");
assertThat(ns.getGlobalSets()).isEqualTo(1);
assertThat(ns.getAliasingGets()).isEqualTo(1);
Name nsA = namespace.getSlot("ns.a");
assertThat(nsA.getGlobalSets()).isEqualTo(1);
assertThat(nsA.getAliasingGets()).isEqualTo(0);
Name xY = namespace.getSlot("x.y");
// TODO(b/117673791): this should be 1
assertThat(xY.getGlobalSets()).isEqualTo(0);
}
Aggregations