use of com.google.javascript.rhino.jstype.JSTypeNative in project closure-compiler by google.
the class TypeInference method traverseNewTarget.
private void traverseNewTarget(Node newTargetNode) {
// new.target is (undefined|!Function) within a vanilla function and !Function within an ES6
// constructor.
// Find the closest non-arrow function (TODO(sdh): this could be an AbstractScope method).
TypedScope scope = containerScope;
while (scope != null && !NodeUtil.isNonArrowFunction(scope.getRootNode())) {
scope = scope.getParent();
}
if (scope == null) {
// NOTE: we already have a parse error for new.target outside a function. The only other case
// where this might happen is a top-level arrow function, which is a parse error in the VM,
// but allowed by our parser.
newTargetNode.setJSType(unknownType);
return;
}
Node root = scope.getRootNode();
Node parent = root.getParent();
if (parent.getGrandparent().isClass()) {
// In an ES6 constuctor, new.target may not be undefined. In any other method, it must be
// undefined, since methods are not constructable.
JSTypeNative type = NodeUtil.isEs6ConstructorMemberFunctionDef(parent) ? JSTypeNative.FUNCTION_TYPE : VOID_TYPE;
newTargetNode.setJSType(registry.getNativeType(type));
} else {
// Other functions also include undefined, in case they are not called with 'new'.
newTargetNode.setJSType(registry.createUnionType(registry.getNativeType(JSTypeNative.FUNCTION_TYPE), registry.getNativeType(VOID_TYPE)));
}
}
Aggregations