use of com.google.javascript.jscomp.GlobalNamespace.Ref in project closure-compiler by google.
the class GlobalNamespaceTest method testRemoveDeclaration1.
public void testRemoveDeclaration1() {
Name n = new Name("a", null, false);
Ref set1 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);
Ref set2 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);
n.addRef(set1);
n.addRef(set2);
assertEquals(set1, n.getDeclaration());
assertEquals(2, n.globalSets);
assertThat(n.getRefs()).hasSize(2);
n.removeRef(set1);
assertEquals(set2, n.getDeclaration());
assertEquals(1, n.globalSets);
assertThat(n.getRefs()).hasSize(1);
}
use of com.google.javascript.jscomp.GlobalNamespace.Ref in project closure-compiler by google.
the class GlobalNamespaceTest method testRemoveDeclaration2.
public void testRemoveDeclaration2() {
Name n = new Name("a", null, false);
Ref set1 = createNodelessRef(Ref.Type.SET_FROM_GLOBAL);
Ref set2 = createNodelessRef(Ref.Type.SET_FROM_LOCAL);
n.addRef(set1);
n.addRef(set2);
assertEquals(set1, n.getDeclaration());
assertEquals(1, n.globalSets);
assertEquals(1, n.localSets);
assertThat(n.getRefs()).hasSize(2);
n.removeRef(set1);
assertNull(n.getDeclaration());
assertEquals(0, n.globalSets);
}
use of com.google.javascript.jscomp.GlobalNamespace.Ref in project closure-compiler by google.
the class ProcessDefines method collectDefines.
/**
* Finds all defines, and creates a {@link DefineInfo} data structure for
* each one.
* @return A map of {@link DefineInfo} structures, keyed by name.
*/
Map<String, DefineInfo> collectDefines(Node externs, Node root) {
if (namespace == null) {
namespace = new GlobalNamespace(compiler, externs, root);
}
// Find all the global names with a @define annotation
List<Name> allDefines = new ArrayList<>();
for (Name name : namespace.getNameIndex().values()) {
Ref decl = name.getDeclaration();
if (name.docInfo != null && name.docInfo.isDefine()) {
// so we look for the JSDoc instead of the inferred type.
if (isValidDefineType(name.docInfo.getType())) {
allDefines.add(name);
} else {
JSError error = JSError.make(decl.node, INVALID_DEFINE_TYPE_ERROR);
compiler.report(error);
}
} else {
for (Ref ref : name.getRefs()) {
if (ref == decl) {
// Declarations were handled above.
continue;
}
Node n = ref.node;
Node parent = ref.node.getParent();
JSDocInfo info = n.getJSDocInfo();
if (info == null && parent.isVar() && parent.hasOneChild()) {
info = parent.getJSDocInfo();
}
if (info != null && info.isDefine()) {
allDefines.add(name);
break;
}
}
}
}
CollectDefines pass = new CollectDefines(compiler, allDefines);
NodeTraversal.traverseRootsEs6(compiler, pass, externs, root);
return pass.getAllDefines();
}
use of com.google.javascript.jscomp.GlobalNamespace.Ref in project closure-compiler by google.
the class CheckGlobalNames method validateName.
private void validateName(Name name, boolean isDefined) {
// If the name is not defined, emit warnings for each reference. While
// we're looking through each reference, check all the module dependencies.
Ref declaration = name.getDeclaration();
Name parent = name.parent;
JSModuleGraph moduleGraph = compiler.getModuleGraph();
for (Ref ref : name.getRefs()) {
// Don't worry about global exprs.
boolean isGlobalExpr = ref.getNode().getParent().isExprResult();
if (!isDefined && !isTypedef(ref)) {
if (!isGlobalExpr) {
reportRefToUndefinedName(name, ref);
}
} else if (declaration != null && ref.getModule() != declaration.getModule() && !moduleGraph.dependsOn(ref.getModule(), declaration.getModule())) {
reportBadModuleReference(name, ref);
} else {
// Check for late references.
if (ref.scope.getClosestHoistScope().isGlobal()) {
// Prototype references are special, because in our reference graph,
// A.prototype counts as a reference to A.
boolean isPrototypeGet = (ref.type == Ref.Type.PROTOTYPE_GET);
Name owner = isPrototypeGet ? name : parent;
boolean singleGlobalParentDecl = owner != null && owner.getDeclaration() != null && owner.localSets == 0;
if (singleGlobalParentDecl && owner.getDeclaration().preOrderIndex > ref.preOrderIndex) {
String refName = isPrototypeGet ? name.getFullName() + ".prototype" : name.getFullName();
compiler.report(JSError.make(ref.node, NAME_DEFINED_LATE_WARNING, refName, owner.getFullName(), owner.getDeclaration().getSourceFile().getName(), String.valueOf(owner.getDeclaration().node.getLineno())));
}
}
}
}
}
use of com.google.javascript.jscomp.GlobalNamespace.Ref in project closure-compiler by google.
the class CollapseProperties method updateGlobalNameDeclarationAtVariableNode.
/**
* Updates the first initialization (a.k.a "declaration") of a global name that occurs at a VAR
* node. See comment for {@link #updateGlobalNameDeclaration}.
*
* @param n An object representing a global name (e.g. "a")
*/
private void updateGlobalNameDeclarationAtVariableNode(Name n, boolean canCollapseChildNames) {
if (!canCollapseChildNames) {
return;
}
Ref ref = n.getDeclaration();
String name = ref.node.getString();
Node rvalue = ref.node.getFirstChild();
Node variableNode = ref.node.getParent();
Node grandparent = variableNode.getParent();
boolean isObjLit = rvalue.isObjectLit();
if (isObjLit) {
declareVariablesForObjLitValues(n, name, rvalue, variableNode, variableNode.getPrevious(), grandparent);
}
addStubsForUndeclaredProperties(n, name, grandparent, variableNode);
if (isObjLit && canEliminate(n)) {
variableNode.removeChild(ref.node);
compiler.reportChangeToEnclosingScope(variableNode);
if (!variableNode.hasChildren()) {
grandparent.removeChild(variableNode);
}
// Clear out the object reference, since we've eliminated it from the
// parse tree.
ref.node = null;
}
}
Aggregations