/** Adds the type t to Node n, and returns n. Does nothing if t is null. */ static Node withType(Node n, JSType t) { if (t != null) { n.setJSType(t); } return n; }
/** * Evaluates the type expression into a {@code JSType} object. */ public JSType evaluate(StaticTypedScope<JSType> scope, TypeIRegistry registry) { JSType type = (JSType) registry.createTypeFromCommentNode(root, sourceName, scope); root.setJSType(type); return type; }
/** * Creates a reference to "arguments" with the type specified in externs, or unknown if the * externs for it weren't included. */ Node createArgumentsReference() { Node result = IR.name("arguments"); if (isAddingTypes()) { result.setJSType(argumentsTypeSupplier.get()); } return result; }
/** Creates an empty function `function() {}` */ Node createEmptyFunction(JSType type) { Node result = NodeUtil.emptyFunction(); if (isAddingTypes()) { checkNotNull(type); checkArgument(type.isFunctionType(), type); result.setJSType(checkNotNull(type)); } return result; }
/** Traverse each element of the array. */ private FlowScope traverseArrayLiteral(Node n, FlowScope scope) { scope = traverseChildren(n, scope); n.setJSType(getNativeType(ARRAY_TYPE)); return scope; }
/** Traverse each element of the array. */ private FlowScope traverseArrayLiteral(Node n, FlowScope scope) { scope = traverseChildren(n, scope); n.setJSType(getNativeType(ARRAY_TYPE)); return scope; }
private Node createUndefinedNode() { Node undefined = IR.name("undefined"); if (shouldAddTypesOnNewAstNodes) { undefined.setJSType(getNativeType(VOID_TYPE)); } return undefined; }
/** * Returns a call to $jscomp.arrayFromIterable with {@code iterable} as its argument. */ static Node arrayFromIterable(AbstractCompiler compiler, Node iterable) { JSTypeRegistry registry = compiler.getTypeRegistry(); JSType arrayType = registry.getNativeType(JSTypeNative.ARRAY_TYPE); Node call = callEs6RuntimeFunction(compiler, iterable, "arrayFromIterable").setJSType(arrayType); call.getFirstChild().setJSType(registry.createFunctionTypeWithVarArgs(arrayType)); return call; }
Node createString(String value) { Node result = IR.string(value); if (isAddingTypes()) { result.setJSType(getNativeType(JSTypeNative.STRING_TYPE)); } return result; }
Node createNumber(double value) { Node result = IR.number(value); if (isAddingTypes()) { result.setJSType(getNativeType(JSTypeNative.NUMBER_TYPE)); } return result; }
Node createComputedProperty(Node key, Node value) { Node result = IR.computedProp(key, value); if (isAddingTypes()) { result.setJSType(value.getJSType()); } return result; }
Node createComma(Node left, Node right) { Node result = IR.comma(left, right); if (isAddingTypes()) { result.setJSType(right.getJSType()); } return result; }
private Node callInferringJSType(Node callee, Node... args) { Node call = IR.call(callee, args); JSType calleeType = callee.getJSType(); if (calleeType == null || !(calleeType instanceof FunctionType)) { return call; } JSType returnType = ((FunctionType) calleeType).getReturnType(); return call.setJSType(returnType); } }
/** Creates a THIS node with the correct type for the given function node. */ Node createThisForFunction(Node functionNode) { final Node result = IR.thisNode(); if (isAddingTypes()) { result.setJSType(getTypeOfThisForFunctionNode(functionNode)); } return result; }
Node createName(Scope scope, String name) { Node result = IR.name(name); if (isAddingTypes()) { result.setJSType(getVarNameType(scope, name)); } return result; }
Node createDelProp(Node target) { Node result = IR.delprop(target); if (isAddingTypes()) { result.setJSType(getNativeType(JSTypeNative.BOOLEAN_TYPE)); } return result; }
/** Creates an assignment expression `lhs = rhs` */ Node createAssign(Node lhs, Node rhs) { Node result = IR.assign(lhs, rhs); if (isAddingTypes()) { result.setJSType(rhs.getJSType()); } return result; }
Node createMemberFunctionDef(String name, Node function) { // A function used for a member function definition must have an empty name, // because the name string goes on the MEMBER_FUNCTION_DEF node. checkArgument(function.getFirstChild().getString().isEmpty(), function); Node result = IR.memberFunctionDef(name, function); if (isAddingTypes()) { // member function definition must share the type of the function that implements it result.setJSType(function.getJSType()); } return result; }
private Node getpropInferringJSType(Node receiver, String propName) { Node getprop = IR.getprop(receiver, propName); JSType receiverType = receiver.getJSType(); if (receiverType == null) { return getprop; } JSType getpropType = receiverType.findPropertyType(propName); if (getpropType == null && receiverType instanceof FunctionType) { getpropType = ((FunctionType) receiverType).getPropertyType(propName); } return getprop.setJSType(getpropType); }
@Override JSType resolveInternal(ErrorReporter reporter) { returnType = safeResolve(returnType, reporter); if (parameters != null) { for (Node paramNode = parameters.getFirstChild(); paramNode != null; paramNode = paramNode.getNext()) { paramNode.setJSType(paramNode.getJSType().resolve(reporter)); } } return this; }