Search in sources :

Example 6 with Ref

use of com.google.javascript.jscomp.GlobalNamespace.Ref in project closure-compiler by google.

the class GlobalNamespaceTest method updateRefNodeRemovesTwinRelationship.

@Test
public void updateRefNodeRemovesTwinRelationship() {
    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);
    Node oldNode = getTwinRef.getNode();
    // move the getTwinRef
    Node newNode = IR.name("A");
    nameA.updateRefNode(getTwinRef, newNode);
    // see confirmTwinsAreCreated() for verification of the original twin relationship
    // confirm that getTwinRef has been updated
    assertThat(getTwinRef.getNode()).isEqualTo(newNode);
    assertThat(nameA.getRefsForNode(newNode)).containsExactly(getTwinRef);
    // confirm that the getTwinRef and setTwinRef are no longer twins
    assertThat(getTwinRef.getTwin()).isNull();
    assertThat(setTwinRef.getTwin()).isNull();
    // confirm that setTwinRef remains otherwise unchanged
    assertThat(setTwinRef.getNode()).isEqualTo(oldNode);
    assertThat(nameA.getRefsForNode(oldNode)).containsExactly(setTwinRef);
}
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 7 with Ref

use of com.google.javascript.jscomp.GlobalNamespace.Ref in project closure-compiler by google.

the class GlobalNamespaceTest method withoutAssignmentFirstQnameDeclarationStatementJSDocWins.

@Test
public void withoutAssignmentFirstQnameDeclarationStatementJSDocWins() {
    GlobalNamespace namespace = parse(lines(// 
    "const X = {};", "/** @type {string} */", "X.number;", "/** @type {Object} */", "X.number;", ""));
    Name nameX = namespace.getOwnSlot("X.number");
    Ref declarationRef = nameX.getDeclaration();
    assertThat(declarationRef).isNull();
    // Make sure JSDoc on the first assignment is the JSDoc for the name
    JSDocInfo jsDocInfo = nameX.getJSDocInfo();
    assertThat(jsDocInfo).isNotNull();
    JSTypeExpression jsTypeExpression = jsDocInfo.getType();
    assertThat(jsTypeExpression).isNotNull();
    JSType jsType = jsTypeExpression.evaluate(/* scope= */
    null, lastCompiler.getTypeRegistry());
    assertType(jsType).isString();
}
Also used : Ref(com.google.javascript.jscomp.GlobalNamespace.Ref) JSType(com.google.javascript.rhino.jstype.JSType) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 8 with Ref

use of com.google.javascript.jscomp.GlobalNamespace.Ref in project closure-compiler by google.

the class GlobalNamespaceTest method firstGlobalAssignmentIsConsideredDeclaration.

@Test
public void firstGlobalAssignmentIsConsideredDeclaration() {
    GlobalNamespace namespace = parse("");
    Name n = namespace.createNameForTesting("a");
    Ref set1 = n.addSingleRefForTesting(Ref.Type.SET_FROM_GLOBAL);
    Ref set2 = n.addSingleRefForTesting(Ref.Type.SET_FROM_GLOBAL);
    assertThat(n.getRefs()).containsExactly(set1, set2).inOrder();
    assertThat(n.getDeclaration()).isEqualTo(set1);
    assertThat(n.getGlobalSets()).isEqualTo(2);
    n.removeRef(set1);
    // declaration moves to next global assignment when first is removed
    assertThat(n.getDeclaration()).isEqualTo(set2);
    assertThat(n.getGlobalSets()).isEqualTo(1);
    assertThat(n.getRefs()).containsExactly(set2);
}
Also used : Ref(com.google.javascript.jscomp.GlobalNamespace.Ref) Name(com.google.javascript.jscomp.GlobalNamespace.Name) Test(org.junit.Test)

Example 9 with Ref

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

Example 10 with Ref

use of com.google.javascript.jscomp.GlobalNamespace.Ref in project closure-compiler by google.

the class GlobalNamespaceTest method firstDeclarationJSDocAlwaysWins.

@Test
public void firstDeclarationJSDocAlwaysWins() {
    GlobalNamespace namespace = parse(lines(// 
    "const X = {};", // later assignment should win
    "/** @type {symbol} */", "X.number;", // this is the JSDoc we should use
    "/** @type {number} */", "X.number = 3;", "/** @type {string} */", "X.number = 'hi';", "/** @type {Object} */", "X.number;", ""));
    Name nameX = namespace.getOwnSlot("X.number");
    Ref declarationRef = nameX.getDeclaration();
    assertThat(declarationRef).isNotNull();
    // make sure first assignment is considered to be the declaration
    Node declarationNode = declarationRef.getNode();
    assertNode(declarationNode).matchesQualifiedName("X.number");
    Node assignNode = declarationNode.getParent();
    assertNode(assignNode).isAssign();
    Node valueNode = declarationNode.getNext();
    assertNode(valueNode).isNumber(3);
    // Make sure JSDoc on the first assignment is the JSDoc for the name
    JSDocInfo jsDocInfo = nameX.getJSDocInfo();
    assertThat(jsDocInfo).isNotNull();
    JSTypeExpression jsTypeExpression = jsDocInfo.getType();
    assertThat(jsTypeExpression).isNotNull();
    JSType jsType = jsTypeExpression.evaluate(/* scope= */
    null, lastCompiler.getTypeRegistry());
    assertType(jsType).isNumber();
}
Also used : Ref(com.google.javascript.jscomp.GlobalNamespace.Ref) JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) JSDocInfo(com.google.javascript.rhino.JSDocInfo) 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