class InferJSDocInfo extends NodeTraversal.AbstractPostOrderCallback implements HotSwapCompilerPass
var x = new Foo();
x.bar;
then the JSType attached to x.bar may get associated JSDoc, but the
Node and Var will not.
JSDoc is initially attached to AST Nodes at parse time.
There are 3 ways that JSDoc get propagated across the type system.
1) Nominal types (e.g., constructors) may contain JSDocInfo for their
declaration.
2) Object types have a JSDocInfo slot for each property on that type.
3) Shape types (like structural functions) may have JSDocInfo.
#1 and #2 should be self-explanatory, and non-controversial. #3 is
a bit trickier. It means that if you have:
/** @param {number} x /
Foo.prototype.bar = goog.abstractMethod;
the JSDocInfo will appear in two places in the type system: in the 'bar'
slot of Foo.prototype, and on the function expression type created by
this expression.Modifier and Type | Field and Description |
---|---|
private AbstractCompiler |
compiler |
Constructor and Description |
---|
InferJSDocInfo(AbstractCompiler compiler) |
Modifier and Type | Method and Description |
---|---|
private static void |
attachJSDocInfoToNominalTypeOrShape(ObjectType objType,
JSDocInfo docInfo,
java.lang.String qName)
Handle cases #1 and #3 in the class doc.
|
private static ObjectType |
dereferenceToObject(JSType type)
Dereferences the given type to an object, or returns null.
|
void |
hotSwapScript(Node root,
Node originalRoot)
Process the JS with root node root.
|
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
void |
visit(NodeTraversal t,
Node n,
Node parent)
Visits a node in postorder (after its children have been visited).
|
shouldTraverse
private final AbstractCompiler compiler
InferJSDocInfo(AbstractCompiler compiler)
public void process(Node externs, Node root)
CompilerPass
process
in interface CompilerPass
externs
- Top of external JS treeroot
- Top of JS treepublic void hotSwapScript(Node root, Node originalRoot)
HotSwapCompilerPass
hotSwapScript
in interface HotSwapCompilerPass
root
- Root node corresponding to the file that is modified,
should be of type Token.SCRIPT
.originalRoot
- Root node corresponding to the original version of the
file that is modified. Should be of type token.SCRIPT
.public void visit(NodeTraversal t, Node n, Node parent)
NodeTraversal.Callback
Visits a node in postorder (after its children have been visited).
A node is visited only if all its parents should be traversed
(NodeTraversal.Callback.shouldTraverse(NodeTraversal, Node, Node)
).
Implementations can have side effects (e.g. modifying the parse tree).
visit
in interface NodeTraversal.Callback
private static ObjectType dereferenceToObject(JSType type)
private static void attachJSDocInfoToNominalTypeOrShape(ObjectType objType, JSDocInfo docInfo, @Nullable java.lang.String qName)