use of com.google.javascript.jscomp.GlobalNamespace.Ref in project closure-compiler by google.
the class GlobalNamespaceTest method rescanningExistingNodesDoesNotCreateDuplicateRefs.
@Test
public void rescanningExistingNodesDoesNotCreateDuplicateRefs() {
GlobalNamespace namespace = parse("class Foo {} const Bar = Foo; const Baz = Bar;");
Name foo = namespace.getOwnSlot("Foo");
Name bar = namespace.getOwnSlot("Bar");
Name baz = namespace.getOwnSlot("Baz");
Collection<Ref> originalFooRefs = ImmutableList.copyOf(foo.getRefs());
Collection<Ref> originalBarRefs = ImmutableList.copyOf(bar.getRefs());
Collection<Ref> originalBazRefs = ImmutableList.copyOf(baz.getRefs());
// Rescan all of the nodes for which we got refs as if they were newly added
Node root = lastCompiler.getJsRoot();
ImmutableSet.Builder<AstChange> astChangeSetBuilder = ImmutableSet.builder();
for (Name name : ImmutableList.of(foo, bar, baz)) {
for (Ref ref : name.getRefs()) {
astChangeSetBuilder.add(createGlobalAstChangeForNode(root, ref.getNode()));
}
}
namespace.scanNewNodes(astChangeSetBuilder.build());
// We should get the same Name objects
assertThat(namespace.getOwnSlot("Foo")).isEqualTo(foo);
assertThat(namespace.getOwnSlot("Bar")).isEqualTo(bar);
assertThat(namespace.getOwnSlot("Baz")).isEqualTo(baz);
// ...and they should contain the same refs with no duplicates added
assertThat(foo.getRefs()).containsExactlyElementsIn(originalFooRefs).inOrder();
assertThat(bar.getRefs()).containsExactlyElementsIn(originalBarRefs).inOrder();
assertThat(baz.getRefs()).containsExactlyElementsIn(originalBazRefs).inOrder();
}
use of com.google.javascript.jscomp.GlobalNamespace.Ref in project closure-compiler by google.
the class GlobalNamespaceTest method updateRefNodeRejectsRedundantUpdate.
@Test
public void updateRefNodeRejectsRedundantUpdate() {
GlobalNamespace namespace = parse("const A = 3;");
Name nameA = namespace.getOwnSlot("A");
Ref refA = nameA.getFirstRef();
assertThrows(IllegalArgumentException.class, () -> nameA.updateRefNode(refA, refA.getNode()));
}
use of com.google.javascript.jscomp.GlobalNamespace.Ref 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.Ref in project closure-compiler by google.
the class GlobalNamespaceTest method removeOneRefOfAPairOfTwins.
@Test
public void removeOneRefOfAPairOfTwins() {
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();
// confirm that they are both associated with oldNode
assertThat(nameA.getRefsForNode(oldNode)).containsExactly(setTwinRef, getTwinRef);
nameA.removeTwinRefs(setTwinRef);
assertThat(nameA.getRefs()).doesNotContain(setTwinRef);
assertThat(nameA.getRefs()).doesNotContain(getTwinRef);
assertThat(nameA.getRefsForNode(oldNode)).isEmpty();
}
Aggregations