Skip to content

Commit

Permalink
Report type errors on just the right/left side of a comparison
Browse files Browse the repository at this point in the history
The current behavior is that, given:

var /** ?number */ x;
if (2 >
    x){}

the error message is:

input0:3: WARNING - right side of numeric comparison
found   : (null|number)
required: number
if (2 >
    ^^^^^

which is confusing.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=200138089
  • Loading branch information
lauraharker authored and blickly committed Jun 12, 2018
1 parent bd7b518 commit b49d4d0
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/com/google/javascript/jscomp/TypeCheck.java
Expand Up @@ -730,18 +730,20 @@ public void visit(NodeTraversal t, Node n, Node parent) {
case LE:
case GT:
case GE:
leftType = getJSType(n.getFirstChild());
rightType = getJSType(n.getLastChild());
Node leftSide = n.getFirstChild();
Node rightSide = n.getLastChild();
leftType = getJSType(leftSide);
rightType = getJSType(rightSide);
if (rightType.isUnknownType()) {
// validate comparable left
validator.expectStringOrNumber(t, n, leftType, "left side of comparison");
validator.expectStringOrNumber(t, leftSide, leftType, "left side of comparison");
} else if (leftType.isUnknownType()) {
// validate comparable right
validator.expectStringOrNumber(t, n, rightType, "right side of comparison");
validator.expectStringOrNumber(t, rightSide, rightType, "right side of comparison");
} else if (rightType.isNumber()) {
validator.expectNumber(t, n, leftType, "left side of numeric comparison");
validator.expectNumber(t, leftSide, leftType, "left side of numeric comparison");
} else if (leftType.isNumber()) {
validator.expectNumber(t, n, rightType, "right side of numeric comparison");
validator.expectNumber(t, rightSide, rightType, "right side of numeric comparison");
} else if (this.strictOperatorChecks) {
String errorMsg = "expected matching types in comparison";
this.validator.expectMatchingTypes(n, leftType, rightType, errorMsg);
Expand All @@ -752,11 +754,13 @@ 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, n, leftType, message);
validator.expectNotNullOrUndefined(t, n, leftType, message, getNativeType(STRING_TYPE));
validator.expectString(t, leftSide, leftType, message);
validator.expectNotNullOrUndefined(
t, leftSide, leftType, message, getNativeType(STRING_TYPE));
message = "right side of comparison";
validator.expectString(t, n, rightType, message);
validator.expectNotNullOrUndefined(t, n, rightType, message, getNativeType(STRING_TYPE));
validator.expectString(t, rightSide, rightType, message);
validator.expectNotNullOrUndefined(
t, rightSide, rightType, message, getNativeType(STRING_TYPE));
}
ensureTyped(n, BOOLEAN_TYPE);
break;
Expand Down

0 comments on commit b49d4d0

Please sign in to comment.