class MethodCompilerPass.GetExternMethods {
public void visit(NodeTraversal t, Node n, Node parent) {
switch (n.getType()) {
case Token.GETPROP:
case Token.GETELEM: {
Node dest = n.getFirstChild().getNext();
if (dest.getType() != Token.STRING) {
return;
}
String name = dest.getString();
// We have a signature. Parse tree of the form:
// assign <- parent
// getprop <- n
// name methods
// string setTimeout
// function
if (parent.getType() == Token.ASSIGN &&
parent.getFirstChild() == n &&
n.getNext().getType() == Token.FUNCTION) {
addSignature(name, n.getNext(), t.getSourceName());
} else {
getSignatureStore().removeSignature(name);
externMethodsWithoutSignatures.add(name);
}
externMethods.add(name);
} break;
case Token.OBJECTLIT: {
for (Node key = n.getFirstChild(); key != null; key = key.getNext()) {
if (key.getType() != Token.NUMBER) {
Node value = key.getFirstChild();
String name = key.getString();
if (key.getType() == Token.STRING
&& value.getType() == Token.FUNCTION) {
addSignature(name, value, t.getSourceName());
} else {
getSignatureStore().removeSignature(name);
externMethodsWithoutSignatures.add(name);
}
externMethods.add(name);
}
}
} break;
}
}
}
class MethodCompilerPass.GatherSignatures {
public void visit(NodeTraversal t, Node n, Node parent) {
switch (n.getType()) {
case Token.GETPROP:
case Token.GETELEM:
Node dest = n.getFirstChild().getNext();
if (dest.getType() == Token.STRING) {
if (dest.getString().equals("prototype")) {
processPrototypeParent(t, parent);
} else {
// Static methods of the form Foo.bar = function() {} or
// Static methods of the form Foo.bar = baz (where baz is a
// function name). Parse tree looks like:
// assign <- parent
// getprop <- n
// name Foo
// string bar
// function or name <- n.getNext()
if (parent.getType() == Token.ASSIGN &&
parent.getFirstChild() == n) {
addPossibleSignature(dest.getString(), n.getNext(), t);
}
}
}
break;
case Token.OBJECTLIT:
for (Node key = n.getFirstChild(); key != null; key = key.getNext()) {
switch(key.getType()) {
case Token.STRING:
addPossibleSignature(key.getString(), key.getFirstChild(), t);
break;
case Token.SET:
case Token.GET:
nonMethodProperties.add(key.getString());
break;
case Token.NUMBER:
// Ignore numberic keys.
break;
default:
throw new IllegalStateException(
"unexpect OBJECTLIT key: " + key);
}
}
break;
}
}
}