From 34bb53f54c942b59c19d39922f0bf799a160a1bf Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 18 Nov 2014 18:20:41 -0800 Subject: [PATCH] Use the entire indexer node for grammar error reporting. --- src/compiler/parser.ts | 24 ++++----- ...rowfunctionsOptionalArgsErrors3.errors.txt | 8 +++ ...rowfunctionsOptionalArgsErrors4.errors.txt | 49 +++++++++++++++++++ tests/baselines/reference/giant.errors.txt | 16 +++--- ...SignatureMustHaveTypeAnnotation.errors.txt | 2 +- .../reference/indexTypeCheck.errors.txt | 4 +- .../indexWithoutParamType.errors.txt | 2 +- .../reference/intTypeCheck.errors.txt | 4 +- .../parserIndexSignature11.errors.txt | 2 +- .../parserIndexSignature9.errors.txt | 2 +- .../reference/parserObjectType6.errors.txt | 2 +- .../fatarrowfunctionsOptionalArgsErrors3.ts | 1 + .../fatarrowfunctionsOptionalArgsErrors4.ts | 22 +++++++++ 13 files changed, 106 insertions(+), 32 deletions(-) create mode 100644 tests/baselines/reference/fatarrowfunctionsOptionalArgsErrors3.errors.txt create mode 100644 tests/baselines/reference/fatarrowfunctionsOptionalArgsErrors4.errors.txt create mode 100644 tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors3.ts create mode 100644 tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors4.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 1c598e5baa6..7b59aeb724b 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1719,52 +1719,46 @@ module ts { var indexerLength = scanner.getStartPos() - indexerStart; node.type = parseTypeAnnotation(); parseSemicolon(); + finishNode(node) + if (file._parserDiagnostics.length === errorCountBeforeIndexSignature) { - checkIndexSignature(node, indexerStart, indexerLength); + checkIndexSignature(node); } - return finishNode(node); + + return node; } - function checkIndexSignature(node: SignatureDeclaration, indexerStart: number, indexerLength: number): void { + function checkIndexSignature(node: SignatureDeclaration): void { var parameter = node.parameters[0]; if (node.parameters.length !== 1) { - var arityDiagnostic = Diagnostics.An_index_signature_must_have_exactly_one_parameter; if (parameter) { - grammarErrorOnNode(parameter.name, arityDiagnostic); + grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_must_have_exactly_one_parameter); } else { - grammarErrorAtPos(indexerStart, indexerLength, arityDiagnostic); + grammarErrorOnNode(node, Diagnostics.An_index_signature_must_have_exactly_one_parameter); } - return; } else if (parameter.flags & NodeFlags.Rest) { grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_cannot_have_a_rest_parameter); - return; } else if (parameter.flags & NodeFlags.Modifier) { grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_cannot_have_an_accessibility_modifier); - return; } else if (parameter.flags & NodeFlags.QuestionMark) { grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_cannot_have_a_question_mark); - return; } else if (parameter.initializer) { grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_cannot_have_an_initializer); - return; } else if (!parameter.type) { grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_must_have_a_type_annotation); - return; } else if (parameter.type.kind !== SyntaxKind.StringKeyword && parameter.type.kind !== SyntaxKind.NumberKeyword) { grammarErrorOnNode(parameter.name, Diagnostics.An_index_signature_parameter_type_must_be_string_or_number); - return; } else if (!node.type) { - grammarErrorAtPos(indexerStart, indexerLength, Diagnostics.An_index_signature_must_have_a_type_annotation); - return; + grammarErrorOnNode(node, Diagnostics.An_index_signature_must_have_a_type_annotation); } } diff --git a/tests/baselines/reference/fatarrowfunctionsOptionalArgsErrors3.errors.txt b/tests/baselines/reference/fatarrowfunctionsOptionalArgsErrors3.errors.txt new file mode 100644 index 00000000000..631ccc2e221 --- /dev/null +++ b/tests/baselines/reference/fatarrowfunctionsOptionalArgsErrors3.errors.txt @@ -0,0 +1,8 @@ +tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors3.ts(1,5): error TS1003: Identifier expected. + + +==== tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors3.ts (1 errors) ==== + (...) => 105; + ~ +!!! error TS1003: Identifier expected. + \ No newline at end of file diff --git a/tests/baselines/reference/fatarrowfunctionsOptionalArgsErrors4.errors.txt b/tests/baselines/reference/fatarrowfunctionsOptionalArgsErrors4.errors.txt new file mode 100644 index 00000000000..8de0eb92a02 --- /dev/null +++ b/tests/baselines/reference/fatarrowfunctionsOptionalArgsErrors4.errors.txt @@ -0,0 +1,49 @@ +tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors4.ts(1,14): error TS1015: Parameter cannot have question mark and initializer. +tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors4.ts(2,15): error TS1015: Parameter cannot have question mark and initializer. +tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors4.ts(3,21): error TS1015: Parameter cannot have question mark and initializer. +tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors4.ts(4,7): error TS1015: Parameter cannot have question mark and initializer. +tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors4.ts(4,39): error TS1015: Parameter cannot have question mark and initializer. +tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors4.ts(17,10): error TS1015: Parameter cannot have question mark and initializer. +tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors4.ts(19,13): error TS1015: Parameter cannot have question mark and initializer. +tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors4.ts(6,5): error TS2304: Cannot find name 'foo'. + + +==== tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors4.ts (8 errors) ==== + false ? (arg?: number = 0) => 47 : null; + ~~~ +!!! error TS1015: Parameter cannot have question mark and initializer. + false ? ((arg?: number = 0) => 57) : null; + ~~~ +!!! error TS1015: Parameter cannot have question mark and initializer. + false ? null : (arg?: number = 0) => 67; + ~~~ +!!! error TS1015: Parameter cannot have question mark and initializer. + ((arg?:number = 1) => 0) + '' + ((arg?:number = 2) => 106); + ~~~ +!!! error TS1015: Parameter cannot have question mark and initializer. + ~~~ +!!! error TS1015: Parameter cannot have question mark and initializer. + + foo( + ~~~ +!!! error TS2304: Cannot find name 'foo'. + (a) => 110, + ((a) => 111), + (a) => { + return 112; + }, + (a? ) => 113, + (a, b? ) => 114, + (a: number) => 115, + (a: number = 0) => 116, + (a = 0) => 117, + (a?: number = 0) => 118, + ~ +!!! error TS1015: Parameter cannot have question mark and initializer. + (...a: number[]) => 119, + (a, b? = 0, ...c: number[]) => 120, + ~ +!!! error TS1015: Parameter cannot have question mark and initializer. + (a) => (b) => (c) => 121, + false? (a) => 0 : (b) => 122 + ); \ No newline at end of file diff --git a/tests/baselines/reference/giant.errors.txt b/tests/baselines/reference/giant.errors.txt index 5ef1393915d..830fa091ccb 100644 --- a/tests/baselines/reference/giant.errors.txt +++ b/tests/baselines/reference/giant.errors.txt @@ -388,7 +388,7 @@ tests/cases/compiler/giant.ts(668,9): error TS2386: Overload signatures must all ~ !!! error TS1022: An index signature parameter must have a type annotation. [p1: string]; - ~~~~~~~~~~~~ + ~~~~~~~~~~~~~ !!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ @@ -502,7 +502,7 @@ tests/cases/compiler/giant.ts(668,9): error TS2386: Overload signatures must all ~ !!! error TS1022: An index signature parameter must have a type annotation. [p1: string]; - ~~~~~~~~~~~~ + ~~~~~~~~~~~~~ !!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ @@ -633,7 +633,7 @@ tests/cases/compiler/giant.ts(668,9): error TS2386: Overload signatures must all ~ !!! error TS1022: An index signature parameter must have a type annotation. [p1: string]; - ~~~~~~~~~~~~ + ~~~~~~~~~~~~~ !!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ @@ -866,7 +866,7 @@ tests/cases/compiler/giant.ts(668,9): error TS2386: Overload signatures must all ~ !!! error TS1022: An index signature parameter must have a type annotation. [p1: string]; - ~~~~~~~~~~~~ + ~~~~~~~~~~~~~ !!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ @@ -980,7 +980,7 @@ tests/cases/compiler/giant.ts(668,9): error TS2386: Overload signatures must all ~ !!! error TS1022: An index signature parameter must have a type annotation. [p1: string]; - ~~~~~~~~~~~~ + ~~~~~~~~~~~~~ !!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ @@ -1111,7 +1111,7 @@ tests/cases/compiler/giant.ts(668,9): error TS2386: Overload signatures must all ~ !!! error TS1022: An index signature parameter must have a type annotation. [p1: string]; - ~~~~~~~~~~~~ + ~~~~~~~~~~~~~ !!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ @@ -1380,7 +1380,7 @@ tests/cases/compiler/giant.ts(668,9): error TS2386: Overload signatures must all ~ !!! error TS1022: An index signature parameter must have a type annotation. [p1: string]; - ~~~~~~~~~~~~ + ~~~~~~~~~~~~~ !!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ @@ -1484,7 +1484,7 @@ tests/cases/compiler/giant.ts(668,9): error TS2386: Overload signatures must all ~ !!! error TS1022: An index signature parameter must have a type annotation. [p1: string]; - ~~~~~~~~~~~~ + ~~~~~~~~~~~~~ !!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ diff --git a/tests/baselines/reference/indexSignatureMustHaveTypeAnnotation.errors.txt b/tests/baselines/reference/indexSignatureMustHaveTypeAnnotation.errors.txt index 52934b7ae9e..e7437fcb056 100644 --- a/tests/baselines/reference/indexSignatureMustHaveTypeAnnotation.errors.txt +++ b/tests/baselines/reference/indexSignatureMustHaveTypeAnnotation.errors.txt @@ -10,7 +10,7 @@ tests/cases/compiler/indexSignatureMustHaveTypeAnnotation.ts(12,5): error TS1021 ~ !!! error TS1022: An index signature parameter must have a type annotation. [x: string]; - ~~~~~~~~~~~ + ~~~~~~~~~~~~ !!! error TS1021: An index signature must have a type annotation. } diff --git a/tests/baselines/reference/indexTypeCheck.errors.txt b/tests/baselines/reference/indexTypeCheck.errors.txt index 71bc03eca96..efa02c3ff1f 100644 --- a/tests/baselines/reference/indexTypeCheck.errors.txt +++ b/tests/baselines/reference/indexTypeCheck.errors.txt @@ -11,10 +11,10 @@ tests/cases/compiler/indexTypeCheck.ts(51,1): error TS2342: An index expression ==== tests/cases/compiler/indexTypeCheck.ts (8 errors) ==== interface Red { [n:number]; // ok - ~~~~~~~~~~ + ~~~~~~~~~~~ !!! error TS1021: An index signature must have a type annotation. [s:string]; // ok - ~~~~~~~~~~ + ~~~~~~~~~~~ !!! error TS1021: An index signature must have a type annotation. } diff --git a/tests/baselines/reference/indexWithoutParamType.errors.txt b/tests/baselines/reference/indexWithoutParamType.errors.txt index 292583302a0..01a5b44a2e0 100644 --- a/tests/baselines/reference/indexWithoutParamType.errors.txt +++ b/tests/baselines/reference/indexWithoutParamType.errors.txt @@ -3,5 +3,5 @@ tests/cases/compiler/indexWithoutParamType.ts(1,10): error TS1096: An index sign ==== tests/cases/compiler/indexWithoutParamType.ts (1 errors) ==== var y: { []; } // Error - ~~ + ~~~ !!! error TS1096: An index signature must have exactly one parameter. \ No newline at end of file diff --git a/tests/baselines/reference/intTypeCheck.errors.txt b/tests/baselines/reference/intTypeCheck.errors.txt index e6cfc73230a..d58df0c2ad2 100644 --- a/tests/baselines/reference/intTypeCheck.errors.txt +++ b/tests/baselines/reference/intTypeCheck.errors.txt @@ -130,7 +130,7 @@ tests/cases/compiler/intTypeCheck.ts(203,17): error TS2351: Cannot use 'new' wit ~ !!! error TS1022: An index signature parameter must have a type annotation. [p1: string]; - ~~~~~~~~~~~~ + ~~~~~~~~~~~~~ !!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ @@ -171,7 +171,7 @@ tests/cases/compiler/intTypeCheck.ts(203,17): error TS2351: Cannot use 'new' wit ~ !!! error TS1022: An index signature parameter must have a type annotation. [p1: string]; - ~~~~~~~~~~~~ + ~~~~~~~~~~~~~ !!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ diff --git a/tests/baselines/reference/parserIndexSignature11.errors.txt b/tests/baselines/reference/parserIndexSignature11.errors.txt index 1aa23b64514..585e72a3516 100644 --- a/tests/baselines/reference/parserIndexSignature11.errors.txt +++ b/tests/baselines/reference/parserIndexSignature11.errors.txt @@ -9,7 +9,7 @@ tests/cases/conformance/parser/ecmascript5/IndexSignatures/parserIndexSignature1 ~ !!! error TS1022: An index signature parameter must have a type annotation. [p1: string]; - ~~~~~~~~~~~~ + ~~~~~~~~~~~~~ !!! error TS1021: An index signature must have a type annotation. [p2: string, p3: number]; ~~ diff --git a/tests/baselines/reference/parserIndexSignature9.errors.txt b/tests/baselines/reference/parserIndexSignature9.errors.txt index e7686c22474..1a7034c4f14 100644 --- a/tests/baselines/reference/parserIndexSignature9.errors.txt +++ b/tests/baselines/reference/parserIndexSignature9.errors.txt @@ -4,6 +4,6 @@ tests/cases/conformance/parser/ecmascript5/IndexSignatures/parserIndexSignature9 ==== tests/cases/conformance/parser/ecmascript5/IndexSignatures/parserIndexSignature9.ts (1 errors) ==== interface I { []: number - ~~ + ~~~~~~~~~~ !!! error TS1096: An index signature must have exactly one parameter. } \ No newline at end of file diff --git a/tests/baselines/reference/parserObjectType6.errors.txt b/tests/baselines/reference/parserObjectType6.errors.txt index 5ad8f5e0053..afdc6b8f4d8 100644 --- a/tests/baselines/reference/parserObjectType6.errors.txt +++ b/tests/baselines/reference/parserObjectType6.errors.txt @@ -8,6 +8,6 @@ tests/cases/conformance/parser/ecmascript5/ObjectTypes/parserObjectType6.ts(2,7) ~ !!! error TS2304: Cannot find name 'B'. []; - ~~ + ~~~ !!! error TS1096: An index signature must have exactly one parameter. }; \ No newline at end of file diff --git a/tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors3.ts b/tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors3.ts new file mode 100644 index 00000000000..77949419711 --- /dev/null +++ b/tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors3.ts @@ -0,0 +1 @@ +(...) => 105; diff --git a/tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors4.ts b/tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors4.ts new file mode 100644 index 00000000000..b4daf2cc3ab --- /dev/null +++ b/tests/cases/compiler/fatarrowfunctionsOptionalArgsErrors4.ts @@ -0,0 +1,22 @@ + false ? (arg?: number = 0) => 47 : null; + false ? ((arg?: number = 0) => 57) : null; + false ? null : (arg?: number = 0) => 67; + ((arg?:number = 1) => 0) + '' + ((arg?:number = 2) => 106); + + foo( + (a) => 110, + ((a) => 111), + (a) => { + return 112; + }, + (a? ) => 113, + (a, b? ) => 114, + (a: number) => 115, + (a: number = 0) => 116, + (a = 0) => 117, + (a?: number = 0) => 118, + (...a: number[]) => 119, + (a, b? = 0, ...c: number[]) => 120, + (a) => (b) => (c) => 121, + false? (a) => 0 : (b) => 122 + ); \ No newline at end of file