From 649f5c01a4d5f13665b992aeb52a0ef53ef36a13 Mon Sep 17 00:00:00 2001 From: Vladimir Matveev Date: Wed, 24 Sep 2014 09:10:40 -0700 Subject: [PATCH] do not report cascading errors in instanceof operator --- src/compiler/checker.ts | 6 ++++-- ...tructorWithIncompleteTypeAnnotation.errors.txt | 8 +------- .../errorHandlingInInstanceOf.errors.txt | 15 +++++++++++++++ .../reference/errorHandlingInInstanceOf.js | 14 ++++++++++++++ tests/cases/compiler/errorHandlingInInstanceOf.ts | 6 ++++++ 5 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 tests/baselines/reference/errorHandlingInInstanceOf.errors.txt create mode 100644 tests/baselines/reference/errorHandlingInInstanceOf.js create mode 100644 tests/cases/compiler/errorHandlingInInstanceOf.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 227e99b712..ae19551cc1 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4935,10 +4935,12 @@ module ts { // The instanceof operator requires the left operand to be of type Any, an object type, or a type parameter type, // and the right operand to be of type Any or a subtype of the 'Function' interface type. // The result is always of the Boolean primitive type. - if (!isTypeAnyTypeObjectTypeOrTypeParameter(leftType)) { + // NOTE: do not raise error is leftType is unknown as related error was already reported + if (leftType !== unknownType && !isTypeAnyTypeObjectTypeOrTypeParameter(leftType)) { error(node.left, Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter); } - if (rightType !== anyType && !isTypeSubtypeOf(rightType, globalFunctionType)) { + // NOTE: do not raise error is right is unknown as related error was already reported + if (rightType !== unknownType && rightType !== anyType && !isTypeSubtypeOf(rightType, globalFunctionType)) { error(node.right, Diagnostics.The_right_hand_side_of_an_instanceof_expression_must_be_of_type_any_or_of_a_type_assignable_to_the_Function_interface_type); } return booleanType; diff --git a/tests/baselines/reference/constructorWithIncompleteTypeAnnotation.errors.txt b/tests/baselines/reference/constructorWithIncompleteTypeAnnotation.errors.txt index 003e5acd73..17b5398475 100644 --- a/tests/baselines/reference/constructorWithIncompleteTypeAnnotation.errors.txt +++ b/tests/baselines/reference/constructorWithIncompleteTypeAnnotation.errors.txt @@ -68,8 +68,6 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(40,28): error TS tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(41,21): error TS2304: Cannot find name 'retValue'. tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(47,17): error TS2304: Cannot find name 'console'. tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(53,13): error TS2304: Cannot find name 'console'. -tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(76,26): error TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter. -tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(76,44): error TS2359: The right-hand side of an 'instanceof' expression must be of type 'any' or of a type assignable to the 'Function' interface type. tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(89,23): error TS2364: Invalid left-hand side of assignment expression. tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(108,24): error TS2365: Operator '+' cannot be applied to types 'number' and 'boolean'. tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(159,31): error TS2304: Cannot find name 'Property'. @@ -98,7 +96,7 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(259,29): error T tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(259,37): error TS2304: Cannot find name 'string'. -==== tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts (98 errors) ==== +==== tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts (96 errors) ==== declare module "fs" { export class File { constructor(filename: string); @@ -242,10 +240,6 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(259,37): error T var local5 = null; var local6 = local5 instanceof fs.File; - ~~~~~~ -!!! error TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter. - ~~~~~~~ -!!! error TS2359: The right-hand side of an 'instanceof' expression must be of type 'any' or of a type assignable to the 'Function' interface type. var hex = 0xBADC0DE, Hex = 0XDEADBEEF; var float = 6.02e23, float2 = 6.02E-23 diff --git a/tests/baselines/reference/errorHandlingInInstanceOf.errors.txt b/tests/baselines/reference/errorHandlingInInstanceOf.errors.txt new file mode 100644 index 0000000000..e4db287fcd --- /dev/null +++ b/tests/baselines/reference/errorHandlingInInstanceOf.errors.txt @@ -0,0 +1,15 @@ +tests/cases/compiler/errorHandlingInInstanceOf.ts(1,5): error TS2304: Cannot find name 'x'. +tests/cases/compiler/errorHandlingInInstanceOf.ts(5,18): error TS2304: Cannot find name 'UnknownType'. + + +==== tests/cases/compiler/errorHandlingInInstanceOf.ts (2 errors) ==== + if (x instanceof String) { + ~ +!!! error TS2304: Cannot find name 'x'. + } + + var y: any; + if (y instanceof UnknownType) { + ~~~~~~~~~~~ +!!! error TS2304: Cannot find name 'UnknownType'. + } \ No newline at end of file diff --git a/tests/baselines/reference/errorHandlingInInstanceOf.js b/tests/baselines/reference/errorHandlingInInstanceOf.js new file mode 100644 index 0000000000..b98e2bf8a7 --- /dev/null +++ b/tests/baselines/reference/errorHandlingInInstanceOf.js @@ -0,0 +1,14 @@ +//// [errorHandlingInInstanceOf.ts] +if (x instanceof String) { +} + +var y: any; +if (y instanceof UnknownType) { +} + +//// [errorHandlingInInstanceOf.js] +if (x instanceof String) { +} +var y; +if (y instanceof UnknownType) { +} diff --git a/tests/cases/compiler/errorHandlingInInstanceOf.ts b/tests/cases/compiler/errorHandlingInInstanceOf.ts new file mode 100644 index 0000000000..081a19837a --- /dev/null +++ b/tests/cases/compiler/errorHandlingInInstanceOf.ts @@ -0,0 +1,6 @@ +if (x instanceof String) { +} + +var y: any; +if (y instanceof UnknownType) { +} \ No newline at end of file