From b0ade675cd54136d3cae4223625be59e52271ee7 Mon Sep 17 00:00:00 2001 From: nickreid Date: Mon, 11 Feb 2019 11:26:22 -0800 Subject: [PATCH] Delete unused `NodeTraversal` params in `TypeValidator`. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=233444628 --- .../google/javascript/jscomp/TypeCheck.java | 120 +++++----- .../javascript/jscomp/TypeValidator.java | 225 +++++++++--------- 2 files changed, 165 insertions(+), 180 deletions(-) diff --git a/src/com/google/javascript/jscomp/TypeCheck.java b/src/com/google/javascript/jscomp/TypeCheck.java index 833eb9a9a6e..328a2e31882 100644 --- a/src/com/google/javascript/jscomp/TypeCheck.java +++ b/src/com/google/javascript/jscomp/TypeCheck.java @@ -534,7 +534,7 @@ public void visit(NodeTraversal t, Node n, Node parent) { // TODO(johnlenz): determine if we can limit object literals in some // way. if (!expr.isObjectLit()) { - validator.expectCanCast(t, n, castType, exprType); + validator.expectCanCast(n, castType, exprType); } ensureTyped(n, castType); @@ -588,7 +588,7 @@ public void visit(NodeTraversal t, Node n, Node parent) { break; case GETELEM: - visitGetElem(t, n); + visitGetElem(n); // The type of GETELEM is always unknown, so no point counting that. // If that unknown leaks elsewhere (say by an assignment to another // variable), then it will be counted. @@ -632,7 +632,7 @@ public void visit(NodeTraversal t, Node n, Node parent) { case INC: left = n.getFirstChild(); checkPropCreation(t, left); - validator.expectNumber(t, left, getJSType(left), "increment/decrement"); + validator.expectNumber(left, getJSType(left), "increment/decrement"); ensureTyped(n, NUMBER_TYPE); break; @@ -670,7 +670,7 @@ public void visit(NodeTraversal t, Node n, Node parent) { left = n.getFirstChild(); if (n.getToken() == Token.NEG) { // We are more permissive with +, because it is used to coerce to number - validator.expectNumber(t, left, getJSType(left), "sign operator"); + validator.expectNumber(left, getJSType(left), "sign operator"); } ensureTyped(n, NUMBER_TYPE); break; @@ -685,10 +685,10 @@ public void visit(NodeTraversal t, Node n, Node parent) { if (left.isTypeOf()) { if (right.isString()) { - checkTypeofString(t, right, right.getString()); + checkTypeofString(right, right.getString()); } } else if (right.isTypeOf() && left.isString()) { - checkTypeofString(t, left, left.getString()); + checkTypeofString(left, left.getString()); } leftType = getJSType(left); @@ -742,14 +742,14 @@ public void visit(NodeTraversal t, Node n, Node parent) { rightType = getJSType(rightSide); if (rightType.isUnknownType()) { // validate comparable left - validator.expectStringOrNumber(t, leftSide, leftType, "left side of comparison"); + validator.expectStringOrNumber(leftSide, leftType, "left side of comparison"); } else if (leftType.isUnknownType()) { // validate comparable right - validator.expectStringOrNumber(t, rightSide, rightType, "right side of comparison"); + validator.expectStringOrNumber(rightSide, rightType, "right side of comparison"); } else if (rightType.isNumber()) { - validator.expectNumber(t, leftSide, leftType, "left side of numeric comparison"); + validator.expectNumber(leftSide, leftType, "left side of numeric comparison"); } else if (leftType.isNumber()) { - validator.expectNumber(t, rightSide, rightType, "right side of numeric comparison"); + validator.expectNumber(rightSide, rightType, "right side of numeric comparison"); } else { String errorMsg = "expected matching types in comparison"; this.validator.expectMatchingTypesStrict(n, leftType, rightType, errorMsg); @@ -758,11 +758,11 @@ public void visit(NodeTraversal t, Node n, Node parent) { // each time the expression is evaluated. Regardless, both operands // should match a string context. String message = "left side of comparison"; - validator.expectString(t, leftSide, leftType, message); + validator.expectString(leftSide, leftType, message); validator.expectNotNullOrUndefined( t, leftSide, leftType, message, getNativeType(STRING_TYPE)); message = "right side of comparison"; - validator.expectString(t, rightSide, rightType, message); + validator.expectString(rightSide, rightType, message); validator.expectNotNullOrUndefined( t, rightSide, rightType, message, getNativeType(STRING_TYPE)); } @@ -774,8 +774,8 @@ public void visit(NodeTraversal t, Node n, Node parent) { left = n.getFirstChild(); right = n.getLastChild(); rightType = getJSType(right); - validator.expectStringOrSymbol(t, left, getJSType(left), "left side of 'in'"); - validator.expectObject(t, n, rightType, "'in' requires an object"); + validator.expectStringOrSymbol(left, getJSType(left), "left side of 'in'"); + validator.expectObject(n, rightType, "'in' requires an object"); if (rightType.isStruct()) { report(t, right, IN_USED_WITH_STRUCT); } @@ -786,9 +786,8 @@ public void visit(NodeTraversal t, Node n, Node parent) { left = n.getFirstChild(); right = n.getLastChild(); rightType = getJSType(right).restrictByNotNullOrUndefined(); - validator.expectAnyObject( - t, left, getJSType(left), "deterministic instanceof yields false"); - validator.expectActualObject(t, right, rightType, "instanceof requires an object"); + validator.expectAnyObject(left, getJSType(left), "deterministic instanceof yields false"); + validator.expectActualObject(right, rightType, "instanceof requires an object"); ensureTyped(n, BOOLEAN_TYPE); break; @@ -837,14 +836,14 @@ public void visit(NodeTraversal t, Node n, Node parent) { case CASE: JSType switchType = getJSType(parent.getFirstChild()); JSType caseType = getJSType(n.getFirstChild()); - validator.expectSwitchMatchesCase(t, n, switchType, caseType); + validator.expectSwitchMatchesCase(n, switchType, caseType); typeable = false; break; case WITH: { Node child = n.getFirstChild(); childType = getJSType(child); - validator.expectObject(t, child, childType, "with requires an object"); + validator.expectObject(child, childType, "with requires an object"); typeable = false; break; } @@ -891,7 +890,7 @@ public void visit(NodeTraversal t, Node n, Node parent) { case ARRAY_PATTERN: ensureTyped(n); validator.expectAutoboxesToIterable( - t, n, getJSType(n), "array pattern destructuring requires an Iterable"); + n, getJSType(n), "array pattern destructuring requires an Iterable"); break; case OBJECT_PATTERN: @@ -914,11 +913,11 @@ public void visit(NodeTraversal t, Node n, Node parent) { Node rhs = n.getSecondChild(); if (lhs.isArrayPattern()) { validator.expectAutoboxesToIterable( - t, rhs, getJSType(rhs), "array pattern destructuring requires an Iterable"); + rhs, getJSType(rhs), "array pattern destructuring requires an Iterable"); } else if (lhs.isObjectPattern()) { // Verify that the value is not null/undefined, since those can't be destructured. validator.expectObject( - t, rhs, getJSType(rhs), "cannot destructure a 'null' or 'undefined' default value"); + rhs, getJSType(rhs), "cannot destructure a 'null' or 'undefined' default value"); } typeable = false; @@ -971,7 +970,7 @@ public void visit(NodeTraversal t, Node n, Node parent) { break; case SPREAD: - checkSpread(t, n); + checkSpread(n); typeable = false; break; @@ -1004,7 +1003,7 @@ public void visit(NodeTraversal t, Node n, Node parent) { checkJsdocInfoContainsObjectWithBadKey(t, n); } - private void checkSpread(NodeTraversal t, Node spreadNode) { + private void checkSpread(Node spreadNode) { if (spreadNode.getParent().isObjectLit()) { // Nothing to check for object spread, anything can be spread. return; @@ -1014,10 +1013,10 @@ private void checkSpread(NodeTraversal t, Node spreadNode) { ensureTyped(iterableNode); JSType iterableType = getJSType(iterableNode); validator.expectAutoboxesToIterable( - t, iterableNode, iterableType, "Spread operator only applies to Iterable types"); + iterableNode, iterableType, "Spread operator only applies to Iterable types"); } - private void checkTypeofString(NodeTraversal t, Node n, String s) { + private void checkTypeofString(Node n, String s) { if (!(s.equals("number") || s.equals("string") || s.equals("boolean") @@ -1026,7 +1025,7 @@ private void checkTypeofString(NodeTraversal t, Node n, String s) { || s.equals("object") || s.equals("symbol") || s.equals("unknown"))) { - validator.expectValidTypeofName(t, n, s); + validator.expectValidTypeofName(n, s); } } @@ -1141,7 +1140,7 @@ private void checkCanAssignToNameGetpropOrGetelem( // during TypedScopeCreator, and we only look for the "dumb" cases here. // object.prototype = ...; if (pname.equals("prototype")) { - validator.expectCanAssignToPrototype(t, objectJsType, nodeToWarn, rightType); + validator.expectCanAssignToPrototype(objectJsType, nodeToWarn, rightType); return; } @@ -1161,7 +1160,7 @@ private void checkCanAssignToNameGetpropOrGetelem( // assignments to it. if (!propertyIsImplicitCast(objectCastType, pname)) { validator.expectCanAssignToPropertyOf( - t, nodeToWarn, rightType, expectedPropertyType, object, pname); + nodeToWarn, rightType, expectedPropertyType, object, pname); } return; } @@ -1194,7 +1193,7 @@ private void checkCanAssignToNameGetpropOrGetelem( } } // Fall through case for arbitrary LHS and arbitrary RHS. - validator.expectCanAssignTo(t, nodeToWarn, rightType, leftType, msg); + validator.expectCanAssignTo(nodeToWarn, rightType, leftType, msg); } private void checkPropCreation(NodeTraversal t, Node lvalue) { @@ -1300,14 +1299,14 @@ private void checkPropertyInheritance( */ private void visitObjectPattern(NodeTraversal t, Node pattern) { JSType patternType = getJSType(pattern); - validator.expectObject(t, pattern, patternType, "cannot destructure 'null' or 'undefined'"); + validator.expectObject(pattern, patternType, "cannot destructure 'null' or 'undefined'"); for (Node child : pattern.children()) { DestructuredTarget target = DestructuredTarget.createTarget(typeRegistry, patternType, child); if (target.hasComputedProperty()) { Node computedProperty = target.getComputedProperty(); validator.expectIndexMatch( - t, computedProperty, patternType, getJSType(computedProperty.getFirstChild())); + computedProperty, patternType, getJSType(computedProperty.getFirstChild())); } else if (target.hasStringKey()) { Node stringKey = target.getStringKey(); if (!stringKey.isQuotedString()) { @@ -1352,7 +1351,7 @@ private void visitObjectOrClassLiteralKey( // Validate computed properties similarly to how we validate GETELEMs. if (key.isComputedProp()) { - validator.expectIndexMatch(t, key, ownerType, getJSType(key.getFirstChild())); + validator.expectIndexMatch(key, ownerType, getJSType(key.getFirstChild())); return; } @@ -1399,9 +1398,9 @@ private void visitObjectOrClassLiteralKey( allowedValueType.toMaybeEnumElementType().getPrimitiveType(); } - boolean valid = validator.expectCanAssignToPropertyOf(t, key, - rightType, allowedValueType, - owner, NodeUtil.getObjectLitKeyName(key)); + boolean valid = + validator.expectCanAssignToPropertyOf( + key, rightType, allowedValueType, owner, NodeUtil.getObjectLitKeyName(key)); if (valid) { ensureTyped(key, rightType); } else { @@ -1431,8 +1430,7 @@ private void visitObjectOrClassLiteralKey( && !type.isPropertyTypeInferred(property) && !propertyIsImplicitCast(type, property)) { validator.expectCanAssignToPropertyOf( - t, key, keyType, - type.getPropertyType(property), owner, property); + key, keyType, type.getPropertyType(property), owner, property); } } } @@ -1758,7 +1756,6 @@ private void checkForOfTypes(NodeTraversal t, Node forOf) { if (forOf.isForAwaitOf()) { Optional maybeType = validator.expectAutoboxesToIterableOrAsyncIterable( - t, iterable, iterableType, "Can only async iterate over a (non-null) Iterable or AsyncIterable type"); @@ -1772,7 +1769,7 @@ private void checkForOfTypes(NodeTraversal t, Node forOf) { actualType = maybeType.get(); } else { validator.expectAutoboxesToIterable( - t, iterable, iterableType, "Can only iterate over a (non-null) Iterable type"); + iterable, iterableType, "Can only iterate over a (non-null) Iterable type"); actualType = // Convert primitives to their wrapper type and remove null/undefined @@ -2085,13 +2082,10 @@ private static SuggestionPair getClosestPropertySuggestion( /** * Visits a GETELEM node. * - * @param t The node traversal object that supplies context, such as the - * scope chain to use in name lookups as well as error reporting. * @param n The node being visited. */ - private void visitGetElem(NodeTraversal t, Node n) { - validator.expectIndexMatch( - t, n, getJSType(n.getFirstChild()), getJSType(n.getLastChild())); + private void visitGetElem(Node n) { + validator.expectIndexMatch(n, getJSType(n.getFirstChild()), getJSType(n.getLastChild())); ensureTyped(n); } @@ -2236,7 +2230,7 @@ private void visitFunction(NodeTraversal t, Node n) { // An async generator function must return a AsyncGenerator or supertype of AsyncGenerator JSType returnType = functionType.getReturnType(); validator.expectAsyncGeneratorSupertype( - t, + n, returnType, "An async generator function must return a (supertype of) AsyncGenerator"); @@ -2244,12 +2238,12 @@ private void visitFunction(NodeTraversal t, Node n) { // A generator function must return a Generator or supertype of Generator JSType returnType = functionType.getReturnType(); validator.expectGeneratorSupertype( - t, n, returnType, "A generator function must return a (supertype of) Generator"); + n, returnType, "A generator function must return a (supertype of) Generator"); } else if (n.isAsyncFunction()) { // An async function must return a Promise or supertype of Promise JSType returnType = functionType.getReturnType(); - validator.expectValidAsyncReturnType(t, n, returnType); + validator.expectValidAsyncReturnType(n, returnType); } } @@ -2325,7 +2319,7 @@ private void checkConstructor(NodeTraversal t, Node n, FunctionType functionType } } // check properties - validator.expectAllInterfaceProperties(t, n, functionType); + validator.expectAllInterfaceProperties(n, functionType); if (!functionType.isAbstract()) { validator.expectAbstractMethodsImplemented(n, functionType); } @@ -2392,7 +2386,7 @@ private void checkCallConventions(NodeTraversal t, Node n) { && !superClass.isEmptyType() && subClass != null && !subClass.isEmptyType()) { - validator.expectSuperType(t, n, superClass, subClass); + validator.expectSuperType(n, superClass, subClass); } } } @@ -2511,7 +2505,7 @@ private void checkArgumentsMatchParameters( if (checkArgumentTypeAgainstParameter) { validator.expectArgumentMatchesParameter( - t, argument, getJSType(argument), getJSType(parameter), call, normalArgumentCount); + argument, getJSType(argument), getJSType(parameter), call, normalArgumentCount); } } @@ -2566,8 +2560,8 @@ private void visitImplicitReturnExpression(NodeTraversal t, Node exprNode) { // Fetch the returned value's type JSType actualReturnType = getJSType(exprNode); - validator.expectCanAssignTo(t, exprNode, actualReturnType, expectedReturnType, - "inconsistent return type"); + validator.expectCanAssignTo( + exprNode, actualReturnType, expectedReturnType, "inconsistent return type"); } } @@ -2637,7 +2631,7 @@ private void visitReturn(NodeTraversal t, Node n) { // verifying validator.expectCanAssignTo( - t, valueNode, actualReturnType, returnType, "inconsistent return type"); + valueNode, actualReturnType, returnType, "inconsistent return type"); } } @@ -2675,7 +2669,7 @@ private void visitYield(NodeTraversal t, Node n) { if (t.getEnclosingFunction().isAsyncGeneratorFunction()) { Optional maybeActualYieldType = validator.expectAutoboxesToIterableOrAsyncIterable( - t, n, actualYieldType, "Expression yield* expects an iterable or async iterable"); + n, actualYieldType, "Expression yield* expects an iterable or async iterable"); if (!maybeActualYieldType.isPresent()) { // don't do any further typechecking of the yield* type. return; @@ -2683,7 +2677,7 @@ private void visitYield(NodeTraversal t, Node n) { actualYieldType = maybeActualYieldType.get(); } else { if (!validator.expectAutoboxesToIterable( - t, n, actualYieldType, "Expression yield* expects an iterable")) { + n, actualYieldType, "Expression yield* expects an iterable")) { // don't do any further typechecking of the yield* type. return; } @@ -2694,7 +2688,7 @@ private void visitYield(NodeTraversal t, Node n) { // verifying validator.expectCanAssignTo( - t, + valueNode, actualYieldType, declaredYieldType, @@ -2739,7 +2733,7 @@ private void visitTaggedTemplateLit(NodeTraversal t, Node n) { JSType parameterType = firstParameter.getJSType().restrictByNotNullOrUndefined(); if (parameterType != null) { validator.expectITemplateArraySupertype( - t, firstParameter, parameterType, "Invalid type for the first parameter of tag function"); + firstParameter, parameterType, "Invalid type for the first parameter of tag function"); } // Validate the remaining parameters (the template literal substitutions) @@ -2792,8 +2786,8 @@ private void visitBinaryOperator(Token op, NodeTraversal t, Node n) { case MUL: case SUB: case EXPONENT: - validator.expectNumber(t, left, leftType, "left operand"); - validator.expectNumber(t, right, rightType, "right operand"); + validator.expectNumber(left, leftType, "left operand"); + validator.expectNumber(right, rightType, "right operand"); break; case ASSIGN_BITAND: @@ -2802,8 +2796,8 @@ private void visitBinaryOperator(Token op, NodeTraversal t, Node n) { case BITAND: case BITXOR: case BITOR: - validator.expectBitwiseable(t, left, leftType, "bad left operand to bitwise operator"); - validator.expectBitwiseable(t, right, rightType, "bad right operand to bitwise operator"); + validator.expectBitwiseable(left, leftType, "bad left operand to bitwise operator"); + validator.expectBitwiseable(right, rightType, "bad right operand to bitwise operator"); break; case ASSIGN_ADD: @@ -2845,7 +2839,7 @@ private void checkEnumAlias( JSType valueEnumPrimitiveType = valueEnumType.getElementsType().getPrimitiveType(); validator.expectCanAssignTo( - t, + nodeToWarn, valueEnumPrimitiveType, declInfo.getEnumParameterType().evaluate(t.getTypedScope(), typeRegistry), diff --git a/src/com/google/javascript/jscomp/TypeValidator.java b/src/com/google/javascript/jscomp/TypeValidator.java index afca3930c28..2857e739b13 100644 --- a/src/com/google/javascript/jscomp/TypeValidator.java +++ b/src/com/google/javascript/jscomp/TypeValidator.java @@ -241,46 +241,46 @@ public Iterable getImplicitInterfaceUses() { } // All non-private methods should have the form: - // expectCondition(NodeTraversal t, Node n, ...); + // expectCondition(Node n, ...); // If there is a mismatch, the {@code expect} method should issue // a warning and attempt to correct the mismatch, when possible. - void expectValidTypeofName(NodeTraversal t, Node n, String found) { + void expectValidTypeofName(Node n, String found) { report(JSError.make(n, UNKNOWN_TYPEOF_VALUE, found)); } /** - * Expect the type to be an object, or a type convertible to object. If the - * expectation is not met, issue a warning at the provided node's source code - * position. + * Expect the type to be an object, or a type convertible to object. If the expectation is not + * met, issue a warning at the provided node's source code position. + * * @return True if there was no warning, false if there was a mismatch. */ - boolean expectObject(NodeTraversal t, Node n, JSType type, String msg) { + boolean expectObject(Node n, JSType type, String msg) { if (!type.matchesObjectContext()) { - mismatch(t, n, msg, type, OBJECT_TYPE); + mismatch(n, msg, type, OBJECT_TYPE); return false; } return true; } /** - * Expect the type to be an object. Unlike expectObject, a type convertible - * to object is not acceptable. + * Expect the type to be an object. Unlike expectObject, a type convertible to object is not + * acceptable. */ - void expectActualObject(NodeTraversal t, Node n, JSType type, String msg) { + void expectActualObject(Node n, JSType type, String msg) { if (!type.isObject()) { - mismatch(t, n, msg, type, OBJECT_TYPE); + mismatch(n, msg, type, OBJECT_TYPE); } } /** - * Expect the type to contain an object sometimes. If the expectation is - * not met, issue a warning at the provided node's source code position. + * Expect the type to contain an object sometimes. If the expectation is not met, issue a warning + * at the provided node's source code position. */ - void expectAnyObject(NodeTraversal t, Node n, JSType type, String msg) { + void expectAnyObject(Node n, JSType type, String msg) { JSType anyObjectType = getNativeType(NO_OBJECT_TYPE); if (!anyObjectType.isSubtypeOf(type) && !type.isEmptyType()) { - mismatch(t, n, msg, type, anyObjectType); + mismatch(n, msg, type, anyObjectType); } } /** @@ -288,14 +288,14 @@ void expectAnyObject(NodeTraversal t, Node n, JSType type, String msg) { * * @return True if there was no warning, false if there was a mismatch. */ - boolean expectAutoboxesToIterable(NodeTraversal t, Node n, JSType type, String msg) { + boolean expectAutoboxesToIterable(Node n, JSType type, String msg) { // Note: we don't just use JSType.autobox() here because that removes null and undefined. // We want to keep null and undefined around. if (type.isUnionType()) { for (JSType alt : type.toMaybeUnionType().getAlternatesWithoutStructuralTyping()) { alt = alt.isBoxableScalar() ? alt.autoboxesTo() : alt; if (!alt.isSubtypeOf(getNativeType(ITERABLE_TYPE))) { - mismatch(t, n, msg, type, ITERABLE_TYPE); + mismatch(n, msg, type, ITERABLE_TYPE); return false; } } @@ -303,7 +303,7 @@ boolean expectAutoboxesToIterable(NodeTraversal t, Node n, JSType type, String m } else { JSType autoboxedType = type.isBoxableScalar() ? type.autoboxesTo() : type; if (!autoboxedType.isSubtypeOf(getNativeType(ITERABLE_TYPE))) { - mismatch(t, n, msg, type, ITERABLE_TYPE); + mismatch(n, msg, type, ITERABLE_TYPE); return false; } } @@ -315,8 +315,7 @@ boolean expectAutoboxesToIterable(NodeTraversal t, Node n, JSType type, String m * * @return The unwrapped variants of the iterable(s), or empty if not iterable. */ - Optional expectAutoboxesToIterableOrAsyncIterable( - NodeTraversal t, Node n, JSType type, String msg) { + Optional expectAutoboxesToIterableOrAsyncIterable(Node n, JSType type, String msg) { MaybeBoxedIterableOrAsyncIterable maybeBoxed = JsIterables.maybeBoxIterableOrAsyncIterable(type, typeRegistry); @@ -324,22 +323,22 @@ Optional expectAutoboxesToIterableOrAsyncIterable( return Optional.of(maybeBoxed.getTemplatedType()); } - mismatch(t, n, msg, type, iterableOrAsyncIterable); + mismatch(n, msg, type, iterableOrAsyncIterable); return Optional.empty(); } /** Expect the type to be a Generator or supertype of Generator. */ - void expectGeneratorSupertype(NodeTraversal t, Node n, JSType type, String msg) { + void expectGeneratorSupertype(Node n, JSType type, String msg) { if (!getNativeType(GENERATOR_TYPE).isSubtypeOf(type)) { - mismatch(t, n, msg, type, GENERATOR_TYPE); + mismatch(n, msg, type, GENERATOR_TYPE); } } /** Expect the type to be a AsyncGenerator or supertype of AsyncGenerator. */ - void expectAsyncGeneratorSupertype(NodeTraversal t, Node n, JSType type, String msg) { + void expectAsyncGeneratorSupertype(Node n, JSType type, String msg) { if (!getNativeType(ASYNC_GENERATOR_TYPE).isSubtypeOf(type)) { - mismatch(t, n, msg, type, ASYNC_GENERATOR_TYPE); + mismatch(n, msg, type, ASYNC_GENERATOR_TYPE); } } @@ -349,7 +348,7 @@ void expectAsyncGeneratorSupertype(NodeTraversal t, Node n, JSType type, String *

`Promise` is the lower bound of the declared return type, since that's what async * functions always return; the user can't return an instance of a more specific type. */ - void expectValidAsyncReturnType(NodeTraversal t, Node n, JSType type) { + void expectValidAsyncReturnType(Node n, JSType type) { if (promiseOfUnknownType.isSubtypeOf(type)) { return; } @@ -360,31 +359,29 @@ void expectValidAsyncReturnType(NodeTraversal t, Node n, JSType type) { } /** Expect the type to be an ITemplateArray or supertype of ITemplateArray. */ - void expectITemplateArraySupertype(NodeTraversal t, Node n, JSType type, String msg) { + void expectITemplateArraySupertype(Node n, JSType type, String msg) { if (!getNativeType(I_TEMPLATE_ARRAY_TYPE).isSubtypeOf(type)) { - mismatch(t, n, msg, type, I_TEMPLATE_ARRAY_TYPE); + mismatch(n, msg, type, I_TEMPLATE_ARRAY_TYPE); } } /** - * Expect the type to be a string, or a type convertible to string. If the - * expectation is not met, issue a warning at the provided node's source code - * position. + * Expect the type to be a string, or a type convertible to string. If the expectation is not met, + * issue a warning at the provided node's source code position. */ - void expectString(NodeTraversal t, Node n, JSType type, String msg) { + void expectString(Node n, JSType type, String msg) { if (!type.matchesStringContext()) { - mismatch(t, n, msg, type, STRING_TYPE); + mismatch(n, msg, type, STRING_TYPE); } } /** - * Expect the type to be a number, or a type convertible to number. If the - * expectation is not met, issue a warning at the provided node's source code - * position. + * Expect the type to be a number, or a type convertible to number. If the expectation is not met, + * issue a warning at the provided node's source code position. */ - void expectNumber(NodeTraversal t, Node n, JSType type, String msg) { + void expectNumber(Node n, JSType type, String msg) { if (!type.matchesNumberContext()) { - mismatch(t, n, msg, type, NUMBER_TYPE); + mismatch(n, msg, type, NUMBER_TYPE); } else { expectNumberStrict(n, type, msg); } @@ -407,13 +404,12 @@ void expectMatchingTypesStrict(Node n, JSType left, JSType right, String msg) { } /** - * Expect the type to be a valid operand to a bitwise operator. This includes - * numbers, any type convertible to a number, or any other primitive type - * (undefined|null|boolean|string). + * Expect the type to be a valid operand to a bitwise operator. This includes numbers, any type + * convertible to a number, or any other primitive type (undefined|null|boolean|string). */ - void expectBitwiseable(NodeTraversal t, Node n, JSType type, String msg) { + void expectBitwiseable(Node n, JSType type, String msg) { if (!type.matchesNumberContext() && !type.isSubtypeOf(allBitwisableValueTypes)) { - mismatch(t, n, msg, type, allBitwisableValueTypes); + mismatch(n, msg, type, allBitwisableValueTypes); } else { expectNumberStrict(n, type, msg); } @@ -423,9 +419,9 @@ void expectBitwiseable(NodeTraversal t, Node n, JSType type, String msg) { * Expect the type to be a number or string, or a type convertible to a number or symbol. If the * expectation is not met, issue a warning at the provided node's source code position. */ - void expectNumberOrSymbol(NodeTraversal t, Node n, JSType type, String msg) { + void expectNumberOrSymbol(Node n, JSType type, String msg) { if (!type.matchesNumberContext() && !type.matchesSymbolContext()) { - mismatch(t, n, msg, type, NUMBER_SYMBOL); + mismatch(n, msg, type, NUMBER_SYMBOL); } } @@ -433,9 +429,9 @@ void expectNumberOrSymbol(NodeTraversal t, Node n, JSType type, String msg) { * Expect the type to be a string or symbol, or a type convertible to a string. If the expectation * is not met, issue a warning at the provided node's source code position. */ - void expectStringOrSymbol(NodeTraversal t, Node n, JSType type, String msg) { + void expectStringOrSymbol(Node n, JSType type, String msg) { if (!type.matchesStringContext() && !type.matchesSymbolContext()) { - mismatch(t, n, msg, type, STRING_SYMBOL); + mismatch(n, msg, type, STRING_SYMBOL); } } @@ -444,11 +440,11 @@ void expectStringOrSymbol(NodeTraversal t, Node n, JSType type, String msg) { * string. If the expectation is not met, issue a warning at the provided node's source code * position. */ - void expectStringOrNumber(NodeTraversal t, Node n, JSType type, String msg) { + void expectStringOrNumber(Node n, JSType type, String msg) { if (!type.matchesNumberContext() && !type.matchesStringContext() && !type.matchesStringContext()) { - mismatch(t, n, msg, type, NUMBER_STRING); + mismatch(n, msg, type, NUMBER_STRING); } else { expectStringOrNumberOrSymbolStrict(n, type, msg); } @@ -466,11 +462,11 @@ void expectStringOrNumberStrict(Node n, JSType type, String msg) { * string. If the expectation is not met, issue a warning at the provided node's source code * position. */ - void expectStringOrNumberOrSymbol(NodeTraversal t, Node n, JSType type, String msg) { + void expectStringOrNumberOrSymbol(Node n, JSType type, String msg) { if (!type.matchesNumberContext() && !type.matchesStringContext() && !type.matchesSymbolContext()) { - mismatch(t, n, msg, type, NUMBER_STRING_SYMBOL); + mismatch(n, msg, type, NUMBER_STRING_SYMBOL); } else { expectStringOrNumberOrSymbolStrict(n, type, msg); } @@ -484,10 +480,10 @@ void expectStringOrNumberOrSymbolStrict(Node n, JSType type, String msg) { } /** - * Expect the type to be anything but the null or void type. If the - * expectation is not met, issue a warning at the provided node's - * source code position. Note that a union type that includes the + * Expect the type to be anything but the null or void type. If the expectation is not met, issue + * a warning at the provided node's source code position. Note that a union type that includes the * void type and at least one other type meets the expectation. + * * @return Whether the expectation was met. */ boolean expectNotNullOrUndefined( @@ -511,7 +507,7 @@ boolean expectNotNullOrUndefined( return true; } - mismatch(t, n, msg, type, expectedType); + mismatch(n, msg, type, expectedType); return false; } return true; @@ -528,20 +524,15 @@ private static boolean containsForwardDeclaredUnresolvedName(JSType type) { return type.isNoResolvedType(); } - /** - * Expect that the type of a switch condition matches the type of its - * case condition. - */ - void expectSwitchMatchesCase(NodeTraversal t, Node n, JSType switchType, JSType caseType) { + /** Expect that the type of a switch condition matches the type of its case condition. */ + void expectSwitchMatchesCase(Node n, JSType switchType, JSType caseType) { // ECMA-262, page 68, step 3 of evaluation of CaseBlock, // but allowing extra autoboxing. // TODO(user): remove extra conditions when type annotations // in the code base have adapted to the change in the compiler. if (!switchType.canTestForShallowEqualityWith(caseType) && (caseType.autoboxesTo() == null || !caseType.autoboxesTo().isSubtypeOf(switchType))) { - mismatch(t, n.getFirstChild(), - "case expression doesn't match switch", - caseType, switchType); + mismatch(n.getFirstChild(), "case expression doesn't match switch", caseType, switchType); } else if (!switchType.canTestForShallowEqualityWith(caseType) && (caseType.autoboxesTo() == null || !caseType.autoboxesTo().isSubtypeWithoutStructuralTyping(switchType))) { @@ -559,7 +550,7 @@ void expectSwitchMatchesCase(NodeTraversal t, Node n, JSType switchType, JSType * @param objType The type we're indexing into (the left side of the GETELEM). * @param indexType The type inside the brackets of the GETELEM/COMPUTED_PROP. */ - void expectIndexMatch(NodeTraversal t, Node n, JSType objType, JSType indexType) { + void expectIndexMatch(Node n, JSType objType, JSType indexType) { checkState(n.isGetElem() || n.isComputedProp(), n); Node indexNode = n.isGetElem() ? n.getLastChild() : n.getFirstChild(); if (indexType.isSymbolValueType()) { @@ -572,21 +563,27 @@ void expectIndexMatch(NodeTraversal t, Node n, JSType objType, JSType indexType) ILLEGAL_PROPERTY_ACCESS, "'[]'", "struct")); } if (objType.isUnknownType()) { - expectStringOrNumberOrSymbol(t, indexNode, indexType, "property access"); + expectStringOrNumberOrSymbol(indexNode, indexType, "property access"); } else { ObjectType dereferenced = objType.dereference(); if (dereferenced != null && dereferenced .getTemplateTypeMap() .hasTemplateKey(typeRegistry.getObjectIndexKey())) { - expectCanAssignTo(t, indexNode, indexType, dereferenced - .getTemplateTypeMap().getResolvedTemplateType(typeRegistry.getObjectIndexKey()), + expectCanAssignTo( + indexNode, + indexType, + dereferenced + .getTemplateTypeMap() + .getResolvedTemplateType(typeRegistry.getObjectIndexKey()), "restricted index type"); } else if (dereferenced != null && dereferenced.isArrayType()) { - expectNumberOrSymbol(t, indexNode, indexType, "array access"); + expectNumberOrSymbol(indexNode, indexType, "array access"); } else if (objType.matchesObjectContext()) { - expectStringOrSymbol(t, indexNode, indexType, "property access"); + expectStringOrSymbol(indexNode, indexType, "property access"); } else { - mismatch(t, n, "only arrays or objects can be accessed", + mismatch( + n, + "only arrays or objects can be accessed", objType, typeRegistry.createUnionType(ARRAY_TYPE, OBJECT_TYPE)); } @@ -594,8 +591,7 @@ void expectIndexMatch(NodeTraversal t, Node n, JSType objType, JSType indexType) } /** - * Expect that the first type can be assigned to a symbol of the second - * type. + * Expect that the first type can be assigned to a symbol of the second type. * * @param t The node traversal. * @param n The node to issue warnings on. @@ -605,8 +601,8 @@ void expectIndexMatch(NodeTraversal t, Node n, JSType objType, JSType indexType) * @param propName The name of the property being assigned to. * @return True if the types matched, false otherwise. */ - boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType, - JSType leftType, Node owner, String propName) { + boolean expectCanAssignToPropertyOf( + Node n, JSType rightType, JSType leftType, Node owner, String propName) { // The NoType check is a hack to make typedefs work OK. if (!leftType.isNoType() && !rightType.isSubtypeOf(leftType)) { // Do not type-check interface methods, because we expect that @@ -621,9 +617,11 @@ boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType, } } - mismatch(t, n, + mismatch( + n, "assignment to property " + propName + " of " + typeRegistry.getReadableTypeName(owner), - rightType, leftType); + rightType, + leftType); return false; } else if (!leftType.isNoType() && !rightType.isSubtypeWithoutStructuralTyping(leftType)){ TypeMismatch.recordImplicitInterfaceUses(this.implicitInterfaceUses, n, rightType, leftType); @@ -633,8 +631,7 @@ boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType, } /** - * Expect that the first type can be assigned to a symbol of the second - * type. + * Expect that the first type can be assigned to a symbol of the second type. * * @param t The node traversal. * @param n The node to issue warnings on. @@ -643,10 +640,9 @@ boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType, * @param msg An extra message for the mismatch warning, if necessary. * @return True if the types matched, false otherwise. */ - boolean expectCanAssignTo(NodeTraversal t, Node n, JSType rightType, - JSType leftType, String msg) { + boolean expectCanAssignTo(Node n, JSType rightType, JSType leftType, String msg) { if (!rightType.isSubtypeOf(leftType)) { - mismatch(t, n, msg, rightType, leftType); + mismatch(n, msg, rightType, leftType); return false; } else if (!rightType.isSubtypeWithoutStructuralTyping(leftType)) { TypeMismatch.recordImplicitInterfaceUses(this.implicitInterfaceUses, n, rightType, leftType); @@ -656,8 +652,7 @@ boolean expectCanAssignTo(NodeTraversal t, Node n, JSType rightType, } /** - * Expect that the type of an argument matches the type of the parameter - * that it's fulfilling. + * Expect that the type of an argument matches the type of the parameter that it's fulfilling. * * @param t The node traversal. * @param n The node to issue warnings on. @@ -666,13 +661,16 @@ boolean expectCanAssignTo(NodeTraversal t, Node n, JSType rightType, * @param callNode The call node, to help with the warning message. * @param ordinal The argument ordinal, to help with the warning message. */ - void expectArgumentMatchesParameter(NodeTraversal t, Node n, JSType argType, - JSType paramType, Node callNode, int ordinal) { + void expectArgumentMatchesParameter( + Node n, JSType argType, JSType paramType, Node callNode, int ordinal) { if (!argType.isSubtypeOf(paramType)) { - mismatch(t, n, - SimpleFormat.format("actual parameter %d of %s does not match formal parameter", ordinal, - typeRegistry.getReadableTypeNameNoDeref(callNode.getFirstChild())), - argType, paramType); + mismatch( + n, + SimpleFormat.format( + "actual parameter %d of %s does not match formal parameter", + ordinal, typeRegistry.getReadableTypeNameNoDeref(callNode.getFirstChild())), + argType, + paramType); } else if (!argType.isSubtypeWithoutStructuralTyping(paramType)){ TypeMismatch.recordImplicitInterfaceUses(this.implicitInterfaceUses, n, argType, paramType); TypeMismatch.recordImplicitUseOfNativeObject(this.mismatches, n, argType, paramType); @@ -687,8 +685,7 @@ void expectArgumentMatchesParameter(NodeTraversal t, Node n, JSType argType, * @param superObject The expected super instance type. * @param subObject The sub instance type. */ - void expectSuperType(NodeTraversal t, Node n, ObjectType superObject, - ObjectType subObject) { + void expectSuperType(Node n, ObjectType superObject, ObjectType subObject) { FunctionType subCtor = subObject.getConstructor(); ObjectType implicitProto = subObject.getImplicitPrototype(); ObjectType declaredSuper = @@ -704,7 +701,7 @@ void expectSuperType(NodeTraversal t, Node n, ObjectType superObject, registerMismatch( superObject, declaredSuper, - report(t.makeError(n, MISSING_EXTENDS_TAG_WARNING, subObject.toString()))); + report(JSError.make(n, MISSING_EXTENDS_TAG_WARNING, subObject.toString()))); } else { mismatch(n, "mismatch in declaration of superclass type", superObject, declaredSuper); } @@ -762,37 +759,37 @@ void expectExtends(Node n, FunctionType subCtor, FunctionType astSuperCtor) { * *

Most of these checks occur during TypedScopeCreator, so we just handle very basic cases here * - *

For example, assuming `Foo` is a constructor, `Foo.prototype = 3;` will warn because `3` - * is not an object. + *

For example, assuming `Foo` is a constructor, `Foo.prototype = 3;` will warn because `3` is + * not an object. * * @param ownerType The type of the object whose prototype is being changed. (e.g. `Foo` above) * @param node Node to issue warnings on (e.g. `3` above) * @param rightType the rvalue type being assigned to the prototype (e.g. `number` above) */ - void expectCanAssignToPrototype(NodeTraversal t, JSType ownerType, Node node, JSType rightType) { + void expectCanAssignToPrototype(JSType ownerType, Node node, JSType rightType) { if (ownerType.isFunctionType()) { FunctionType functionType = ownerType.toMaybeFunctionType(); if (functionType.isConstructor()) { - expectObject(t, node, rightType, "cannot override prototype with non-object"); + expectObject(node, rightType, "cannot override prototype with non-object"); } } } /** - * Expect that the first type can be cast to the second type. The first type - * must have some relationship with the second. + * Expect that the first type can be cast to the second type. The first type must have some + * relationship with the second. * * @param t The node traversal. * @param n The node where warnings should point. * @param targetType The type being cast to. * @param sourceType The type being cast from. */ - void expectCanCast(NodeTraversal t, Node n, JSType targetType, JSType sourceType) { + void expectCanCast(Node n, JSType targetType, JSType sourceType) { if (!sourceType.canCastTo(targetType)) { registerMismatch( sourceType, targetType, - report(t.makeError(n, INVALID_CAST, sourceType.toString(), targetType.toString()))); + report(JSError.make(n, INVALID_CAST, sourceType.toString(), targetType.toString()))); } else if (!sourceType.isSubtypeWithoutStructuralTyping(targetType)){ TypeMismatch.recordImplicitInterfaceUses( this.implicitInterfaceUses, n, sourceType, targetType); @@ -883,28 +880,27 @@ TypedVar expectUndeclaredVariable(String sourceName, CompilerInput input, } /** - * Expect that all properties on interfaces that this type implements are - * implemented and correctly typed. + * Expect that all properties on interfaces that this type implements are implemented and + * correctly typed. */ - void expectAllInterfaceProperties(NodeTraversal t, Node n, - FunctionType type) { + void expectAllInterfaceProperties(Node n, FunctionType type) { ObjectType instance = type.getInstanceType(); for (ObjectType implemented : type.getAllImplementedInterfaces()) { if (implemented.getImplicitPrototype() != null) { for (String prop : implemented.getImplicitPrototype().getOwnPropertyNames()) { - expectInterfaceProperty(t, n, instance, implemented, prop); + expectInterfaceProperty(n, instance, implemented, prop); } } } } /** - * Expect that the property in an interface that this type implements is - * implemented and correctly typed. + * Expect that the property in an interface that this type implements is implemented and correctly + * typed. */ - private void expectInterfaceProperty(NodeTraversal t, Node n, - ObjectType instance, ObjectType implementedInterface, String prop) { + private void expectInterfaceProperty( + Node n, ObjectType instance, ObjectType implementedInterface, String prop) { StaticTypedSlot propSlot = instance.getSlot(prop); if (propSlot == null) { // Not implemented @@ -946,7 +942,7 @@ private void expectInterfaceProperty(NodeTraversal t, Node n, FunctionType constructor = implementedInterface.toObjectType().getConstructor(); JSError err = - t.makeError( + JSError.make( propNode, HIDDEN_INTERFACE_PROPERTY_MISMATCH, prop, @@ -1009,13 +1005,8 @@ void expectAbstractMethodsImplemented(Node n, FunctionType ctorType) { } } - /** Report a type mismatch */ - private void mismatch(NodeTraversal unusedT, Node n, String msg, JSType found, JSType required) { - mismatch(n, msg, found, required); - } - - private void mismatch(NodeTraversal t, Node n, String msg, JSType found, JSTypeNative required) { - mismatch(t, n, msg, found, getNativeType(required)); + private void mismatch(Node n, String msg, JSType found, JSTypeNative required) { + mismatch(n, msg, found, getNativeType(required)); } private void mismatch(Node n, String msg, JSType found, JSType required) {