Code with Finding: |
class GlobalNamespace.BuildGlobalNamespace {
@Override
public void visit(NodeTraversal t, Node n, Node parent) {
if (nodeFilter != null && !nodeFilter.apply(n)) {
return;
}
// If we are traversing the externs, then we save a pointer to the scope
// generated by them, so that we can do lookups in it later.
if (externsRoot != null && n == externsRoot) {
externsScope = t.getScope();
}
String name;
boolean isSet = false;
Name.Type type = Name.Type.OTHER;
boolean isPropAssign = false;
switch (n.getType()) {
case Token.GET:
case Token.SET:
case Token.STRING:
// This may be a key in an object literal declaration.
name = null;
if (parent != null && parent.getType() == Token.OBJECTLIT) {
name = getNameForObjLitKey(n);
}
if (name == null) return;
isSet = true;
switch (n.getType()) {
case Token.STRING:
type = getValueType(n.getFirstChild());
break;
case Token.GET:
type = Name.Type.GET;
break;
case Token.SET:
type = Name.Type.SET;
break;
default:
throw new IllegalStateException("unexpected:" + n);
}
break;
case Token.NAME:
// This may be a variable get or set.
if (parent != null) {
switch (parent.getType()) {
case Token.VAR:
isSet = true;
Node rvalue = n.getFirstChild();
type = rvalue == null ? Name.Type.OTHER : getValueType(rvalue);
break;
case Token.ASSIGN:
if (parent.getFirstChild() == n) {
isSet = true;
type = getValueType(n.getNext());
}
break;
case Token.GETPROP:
return;
case Token.FUNCTION:
Node gramps = parent.getParent();
if (gramps == null ||
NodeUtil.isFunctionExpression(parent)) return;
isSet = true;
type = Name.Type.FUNCTION;
break;
}
}
name = n.getString();
break;
case Token.GETPROP:
// This may be a namespaced name get or set.
if (parent != null) {
switch (parent.getType()) {
case Token.ASSIGN:
if (parent.getFirstChild() == n) {
isSet = true;
type = getValueType(n.getNext());
isPropAssign = true;
}
break;
case Token.GETPROP:
return;
}
}
name = n.getQualifiedName();
if (name == null) return;
break;
default:
return;
}
// We are only interested in global names.
Scope scope = t.getScope();
if (!isGlobalNameReference(name, scope)) {
return;
}
if (isSet) {
if (isGlobalScope(scope)) {
handleSetFromGlobal(t, n, parent, name, isPropAssign, type);
} else {
handleSetFromLocal(t, n, parent, name);
}
} else {
handleGet(t, n, parent, name);
}
}
}
|