From 3dfa8bef24f6e736d7bcbdfd58327441bfd000bc Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 18 Nov 2014 22:46:35 -0800 Subject: [PATCH] Move contructor grammar checks into the grammar walker. --- src/compiler/parser.ts | 22 +++++++++++++------ ...torWithIncompleteTypeAnnotation.errors.txt | 5 +---- .../parserConstructorDeclaration11.errors.txt | 5 +---- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 1cc5e09d680..775ed08ca47 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -3373,12 +3373,6 @@ module ts { node.parameters = sig.parameters; node.type = sig.type; node.body = parseAndCheckFunctionBody(/*isConstructor*/ true); - if (node.typeParameters) { - grammarErrorAtPos(node.typeParameters.pos, node.typeParameters.end - node.typeParameters.pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration); - } - if (node.type) { - grammarErrorOnNode(node.type, Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration); - } return finishNode(node); } @@ -4167,7 +4161,21 @@ module ts { } function visitConstructor(node: ConstructorDeclaration) { - checkParameterList(node.parameters); + checkParameterList(node.parameters) || + checkConstructorTypeParameters(node) || + checkConstructorTypeAnnotation(node); + } + + function checkConstructorTypeParameters(node: ConstructorDeclaration) { + if (node.typeParameters) { + return grammarErrorAtPos(node.typeParameters.pos, node.typeParameters.end - node.typeParameters.pos, Diagnostics.Type_parameters_cannot_appear_on_a_constructor_declaration); + } + } + + function checkConstructorTypeAnnotation(node: ConstructorDeclaration) { + if (node.type) { + return grammarErrorOnNode(node.type, Diagnostics.Type_annotation_cannot_appear_on_a_constructor_declaration); + } } function visitConstructorType(node: SignatureDeclaration) { diff --git a/tests/baselines/reference/constructorWithIncompleteTypeAnnotation.errors.txt b/tests/baselines/reference/constructorWithIncompleteTypeAnnotation.errors.txt index 696f173e930..c18dfb8e354 100644 --- a/tests/baselines/reference/constructorWithIncompleteTypeAnnotation.errors.txt +++ b/tests/baselines/reference/constructorWithIncompleteTypeAnnotation.errors.txt @@ -40,7 +40,6 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(238,9): error TS tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(238,26): error TS1005: ';' expected. tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(239,13): error TS1108: A 'return' statement can only be used within a function body. tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(241,5): error TS1128: Declaration or statement expected. -tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(254,67): error TS1093: Type annotation cannot appear on a constructor declaration. tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(254,69): error TS1110: Type expected. tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(256,9): error TS1128: Declaration or statement expected. tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(256,31): error TS1005: ',' expected. @@ -94,7 +93,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 (94 errors) ==== +==== tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts (93 errors) ==== declare module "fs" { export class File { constructor(filename: string); @@ -486,8 +485,6 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(259,37): error T private otherValue = 42; constructor(private value: number, public name: string) : } - -!!! error TS1093: Type annotation cannot appear on a constructor declaration. ~ !!! error TS1110: Type expected. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/baselines/reference/parserConstructorDeclaration11.errors.txt b/tests/baselines/reference/parserConstructorDeclaration11.errors.txt index e8a799fc36a..7e5c305fb80 100644 --- a/tests/baselines/reference/parserConstructorDeclaration11.errors.txt +++ b/tests/baselines/reference/parserConstructorDeclaration11.errors.txt @@ -1,12 +1,9 @@ tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration11.ts(2,14): error TS1098: Type parameter list cannot be empty. -tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration11.ts(2,15): error TS1092: Type parameters cannot appear on a constructor declaration. -==== tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration11.ts (2 errors) ==== +==== tests/cases/conformance/parser/ecmascript5/ConstructorDeclarations/parserConstructorDeclaration11.ts (1 errors) ==== class C { constructor<>() { } ~~ !!! error TS1098: Type parameter list cannot be empty. - -!!! error TS1092: Type parameters cannot appear on a constructor declaration. } \ No newline at end of file