Search in sources :

Example 31 with Ref

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();
}
Also used : Ref(com.google.javascript.jscomp.GlobalNamespace.Ref) ImmutableSet(com.google.common.collect.ImmutableSet) AstChange(com.google.javascript.jscomp.GlobalNamespace.AstChange) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 32 with Ref

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()));
}
Also used : Ref(com.google.javascript.jscomp.GlobalNamespace.Ref) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 33 with Ref

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);
}
Also used : Ref(com.google.javascript.jscomp.GlobalNamespace.Ref) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 34 with Ref

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();
}
Also used : Ref(com.google.javascript.jscomp.GlobalNamespace.Ref) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Aggregations

Ref (com.google.javascript.jscomp.GlobalNamespace.Ref)34 Name (com.google.javascript.jscomp.GlobalNamespace.Name)25 Node (com.google.javascript.rhino.Node)24 Test (org.junit.Test)13 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)9 JSDocInfo (com.google.javascript.rhino.JSDocInfo)7 AstChange (com.google.javascript.jscomp.GlobalNamespace.AstChange)4 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)2 JSType (com.google.javascript.rhino.jstype.JSType)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Module (com.google.javascript.jscomp.modules.Module)1 FeatureSet (com.google.javascript.jscomp.parsing.parser.FeatureSet)1 QualifiedName (com.google.javascript.rhino.QualifiedName)1 Nullable (javax.annotation.Nullable)1