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);
}
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();
}
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);
}
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);
}
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();
}
Aggregations