Use the entire indexer node for grammar error reporting.

This commit is contained in:
Cyrus Najmabadi 2014-11-18 18:20:41 -08:00
parent 08f0672b03
commit 34bb53f54c
13 changed files with 106 additions and 32 deletions

View File

@ -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);
}
}

View File

@ -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.

View File

@ -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
);

View File

@ -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];
~~

View File

@ -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.
}

View File

@ -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.
}

View File

@ -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.

View File

@ -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];
~~

View File

@ -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];
~~

View File

@ -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.
}

View File

@ -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.
};

View File

@ -0,0 +1 @@
(...) => 105;

View File

@ -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
);