use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method testObjectPatternAliasInDeclaration.
@Test
public void testObjectPatternAliasInDeclaration() {
GlobalNamespace namespace = parse("const ns = {a: 3}; const {a: b} = ns;");
Name bar = namespace.getSlot("ns");
assertThat(bar.getGlobalSets()).isEqualTo(1);
assertThat(bar.getAliasingGets()).isEqualTo(1);
assertThat(bar.getTotalGets()).isEqualTo(1);
Name nsA = namespace.getSlot("ns.a");
assertThat(nsA.getGlobalSets()).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 removeTwinRefsTogether.
@Test
public void removeTwinRefsTogether() {
GlobalNamespace namespace = parse(lines(//
"let A;", // A will have twin refs here
"const B = A = 3;"));
Name nameA = namespace.getOwnSlot("A");
// first ref is declaration of A
// second is the SET twin
Ref setTwinRef = Iterables.get(nameA.getRefs(), 1);
// third is the GET twin
Ref getTwinRef = Iterables.get(nameA.getRefs(), 2);
// see confirmTwinsAreCreated() for verification of the original twin relationship
Node oldNode = getTwinRef.getNode();
nameA.removeRef(setTwinRef);
assertThat(nameA.getRefs()).doesNotContain(setTwinRef);
// twin is still there
assertThat(nameA.getRefs()).contains(getTwinRef);
// and not a twin anymore
assertThat(getTwinRef.getTwin()).isNull();
assertThat(nameA.getRefsForNode(oldNode)).containsExactly(getTwinRef);
}
use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method testCannotCollapseAliasedInterfaceProperty_consideringStaticInheritance.
@Test
public void testCannotCollapseAliasedInterfaceProperty_consideringStaticInheritance() {
this.assumeStaticInheritanceIsNotUsed = false;
GlobalNamespace namespace = parse(lines("/** @interface */", "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 moduleLevelNamesAreCaptured_esExportDecl.
@Test
public void moduleLevelNamesAreCaptured_esExportDecl() {
GlobalNamespace namespace = parseAndGatherModuleData("export const x = 0;");
ModuleMetadata metadata = lastCompiler.getModuleMetadataMap().getModulesByPath().get("test.js");
Name x = namespace.getNameFromModule(metadata, "x");
assertThat(x).isNotNull();
assertNode(x.getDeclaration().getNode().getParent()).hasToken(Token.CONST);
}
use of com.google.javascript.jscomp.GlobalNamespace.Name in project closure-compiler by google.
the class GlobalNamespaceTest method testScanFromNodeDoesntDuplicateVarDeclarationSets.
@Test
public void testScanFromNodeDoesntDuplicateVarDeclarationSets() {
GlobalNamespace namespace = parse("class Foo {} const Bar = Foo; const Baz = Bar;");
Name foo = namespace.getOwnSlot("Foo");
assertThat(foo.getAliasingGets()).isEqualTo(1);
Name baz = namespace.getOwnSlot("Baz");
assertThat(baz.getGlobalSets()).isEqualTo(1);
// Replace "const Baz = Bar" with "const Baz = Foo"
Node root = lastCompiler.getJsRoot();
Node barRef = root.getFirstChild().getLastChild().getFirstFirstChild();
checkState(barRef.getString().equals("Bar"), barRef);
Node fooName = IR.name("Foo");
barRef.replaceWith(fooName);
// Rescan the new nodes
namespace.scanNewNodes(ImmutableSet.of(createGlobalAstChangeForNode(root, fooName)));
assertThat(foo.getAliasingGets()).isEqualTo(2);
// A bug in scanFromNode used to make this `2`
assertThat(baz.getGlobalSets()).isEqualTo(1);
}
Aggregations