From de63023b1e7c7a007b4451c7e2406cf8f265652f Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Wed, 19 Nov 2014 00:11:59 -0800 Subject: [PATCH] Move type parameter checks to the grammar checker. --- src/compiler/parser.ts | 54 +++++++++------- ...ontShowCompilerGeneratedMembers.errors.txt | 8 +-- .../parserConstructorDeclaration12.errors.txt | 61 +++++++++++++++++++ .../parserConstructorDeclaration12.ts | 10 +++ 4 files changed, 105 insertions(+), 28 deletions(-) create mode 100644 tests/baselines/reference/parserConstructorDeclaration12.errors.txt create mode 100644 tests/cases/compiler/parserConstructorDeclaration12.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 0a4658f42b5..25b6f11fa5b 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -1614,14 +1614,7 @@ module ts { function parseTypeParameters(): NodeArray { if (token === SyntaxKind.LessThanToken) { - var pos = getNodePos(); - var result = parseBracketedList(ParsingContext.TypeParameters, parseTypeParameter, SyntaxKind.LessThanToken, SyntaxKind.GreaterThanToken); - if (!result.length) { - var start = getTokenPos(pos); - var length = getNodePos() - start; - errorAtPos(start, length, Diagnostics.Type_parameter_list_cannot_be_empty); - } - return result; + return parseBracketedList(ParsingContext.TypeParameters, parseTypeParameter, SyntaxKind.LessThanToken, SyntaxKind.GreaterThanToken); } } @@ -4121,11 +4114,13 @@ module ts { } function visitArrowFunction(node: FunctionExpression) { - checkParameterList(node.parameters); + checkTypeParameterList(node.typeParameters) || + checkParameterList(node.parameters); } function visitCallSignature(node: ConstructorDeclaration) { - checkParameterList(node.parameters); + checkTypeParameterList(node.typeParameters) || + checkParameterList(node.parameters); } function visitCatchBlock(node: CatchBlock) { @@ -4141,7 +4136,8 @@ module ts { } function visitConstructor(node: ConstructorDeclaration) { - checkParameterList(node.parameters) || + checkTypeParameterList(node.typeParameters) || + checkParameterList(node.parameters) || checkConstructorTypeParameters(node) || checkConstructorTypeAnnotation(node); } @@ -4159,11 +4155,13 @@ module ts { } function visitConstructorType(node: SignatureDeclaration) { - checkParameterList(node.parameters); + checkTypeParameterList(node.typeParameters) || + checkParameterList(node.parameters); } function visitConstructSignature(node: FunctionLikeDeclaration) { - checkParameterList(node.parameters); + checkTypeParameterList(node.typeParameters) || + checkParameterList(node.parameters); } function visitEnumDeclaration(enumDecl: EnumDeclaration) { @@ -4214,20 +4212,24 @@ module ts { } function visitFunctionDeclaration(node: FunctionLikeDeclaration) { - checkParameterList(node.parameters); + checkTypeParameterList(node.typeParameters) || + checkParameterList(node.parameters); } function visitFunctionExpression(node: FunctionExpression) { - checkParameterList(node.parameters); + checkTypeParameterList(node.typeParameters) || + checkParameterList(node.parameters); } function visitFunctionType(node: SignatureDeclaration) { - checkParameterList(node.parameters); + checkTypeParameterList(node.typeParameters) || + checkParameterList(node.parameters); } function visitGetAccessor(node: MethodDeclaration) { - checkParameterList(node.parameters) || - checkAccessor(node); + checkTypeParameterList(node.typeParameters) || + checkParameterList(node.parameters) || + checkAccessor(node); } function visitIndexSignature(node: SignatureDeclaration): void { @@ -4268,7 +4270,8 @@ module ts { } function visitMethod(node: MethodDeclaration) { - checkParameterList(node.parameters); + checkTypeParameterList(node.typeParameters) || + checkParameterList(node.parameters); } function visitModuleDeclaration(node: ModuleDeclaration): void { @@ -4366,6 +4369,14 @@ module ts { } } + function checkTypeParameterList(typeParameters: NodeArray): boolean { + if (typeParameters && typeParameters.length === 0) { + var start = typeParameters.pos - "<".length; + var end = typeParameters.end + ">".length; + return grammarErrorAtPos(start, end - start, Diagnostics.Type_parameter_list_cannot_be_empty); + } + } + function checkParameterList(parameters: NodeArray): boolean { var seenOptionalParameter = false; var parameterCount = parameters.length; @@ -4426,8 +4437,9 @@ module ts { } function visitSetAccessor(node: MethodDeclaration) { - checkParameterList(node.parameters) || - checkAccessor(node); + checkTypeParameterList(node.typeParameters) || + checkParameterList(node.parameters) || + checkAccessor(node); } function checkAccessor(accessor: MethodDeclaration): boolean { diff --git a/tests/baselines/reference/dontShowCompilerGeneratedMembers.errors.txt b/tests/baselines/reference/dontShowCompilerGeneratedMembers.errors.txt index cd2402161c3..5f6f15791f6 100644 --- a/tests/baselines/reference/dontShowCompilerGeneratedMembers.errors.txt +++ b/tests/baselines/reference/dontShowCompilerGeneratedMembers.errors.txt @@ -1,22 +1,16 @@ -tests/cases/compiler/dontShowCompilerGeneratedMembers.ts(3,5): error TS1098: Type parameter list cannot be empty. -tests/cases/compiler/dontShowCompilerGeneratedMembers.ts(3,6): error TS1005: '(' expected. tests/cases/compiler/dontShowCompilerGeneratedMembers.ts(3,6): error TS1139: Type parameter declaration expected. tests/cases/compiler/dontShowCompilerGeneratedMembers.ts(4,1): error TS1109: Expression expected. tests/cases/compiler/dontShowCompilerGeneratedMembers.ts(1,5): error TS2322: Type 'number' is not assignable to type '{ (): any; x: number; }'. Property 'x' is missing in type 'Number'. -==== tests/cases/compiler/dontShowCompilerGeneratedMembers.ts (5 errors) ==== +==== tests/cases/compiler/dontShowCompilerGeneratedMembers.ts (3 errors) ==== var f: { ~ !!! error TS2322: Type 'number' is not assignable to type '{ (): any; x: number; }'. !!! error TS2322: Property 'x' is missing in type 'Number'. x: number; <- - ~ -!!! error TS1098: Type parameter list cannot be empty. - ~ -!!! error TS1005: '(' expected. ~ !!! error TS1139: Type parameter declaration expected. }; diff --git a/tests/baselines/reference/parserConstructorDeclaration12.errors.txt b/tests/baselines/reference/parserConstructorDeclaration12.errors.txt new file mode 100644 index 00000000000..1af651d6ca9 --- /dev/null +++ b/tests/baselines/reference/parserConstructorDeclaration12.errors.txt @@ -0,0 +1,61 @@ +tests/cases/compiler/parserConstructorDeclaration12.ts(2,14): error TS1098: Type parameter list cannot be empty. +tests/cases/compiler/parserConstructorDeclaration12.ts(3,14): error TS1098: Type parameter list cannot be empty. +tests/cases/compiler/parserConstructorDeclaration12.ts(4,15): error TS1098: Type parameter list cannot be empty. +tests/cases/compiler/parserConstructorDeclaration12.ts(5,15): error TS1098: Type parameter list cannot be empty. +tests/cases/compiler/parserConstructorDeclaration12.ts(6,14): error TS1098: Type parameter list cannot be empty. +tests/cases/compiler/parserConstructorDeclaration12.ts(7,14): error TS1098: Type parameter list cannot be empty. +tests/cases/compiler/parserConstructorDeclaration12.ts(8,15): error TS1098: Type parameter list cannot be empty. +tests/cases/compiler/parserConstructorDeclaration12.ts(9,15): error TS1098: Type parameter list cannot be empty. +tests/cases/compiler/parserConstructorDeclaration12.ts(2,3): error TS2392: Multiple constructor implementations are not allowed. +tests/cases/compiler/parserConstructorDeclaration12.ts(3,3): error TS2392: Multiple constructor implementations are not allowed. +tests/cases/compiler/parserConstructorDeclaration12.ts(4,3): error TS2392: Multiple constructor implementations are not allowed. +tests/cases/compiler/parserConstructorDeclaration12.ts(5,3): error TS2392: Multiple constructor implementations are not allowed. +tests/cases/compiler/parserConstructorDeclaration12.ts(6,3): error TS2392: Multiple constructor implementations are not allowed. +tests/cases/compiler/parserConstructorDeclaration12.ts(7,3): error TS2392: Multiple constructor implementations are not allowed. +tests/cases/compiler/parserConstructorDeclaration12.ts(8,3): error TS2392: Multiple constructor implementations are not allowed. +tests/cases/compiler/parserConstructorDeclaration12.ts(9,3): error TS2392: Multiple constructor implementations are not allowed. + + +==== tests/cases/compiler/parserConstructorDeclaration12.ts (16 errors) ==== + class C { + constructor<>() { } + ~~ +!!! error TS1098: Type parameter list cannot be empty. + ~~~~~~~~~~~~~~~~~~~ +!!! error TS2392: Multiple constructor implementations are not allowed. + constructor<> () { } + ~~ +!!! error TS1098: Type parameter list cannot be empty. + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2392: Multiple constructor implementations are not allowed. + constructor <>() { } + ~~ +!!! error TS1098: Type parameter list cannot be empty. + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2392: Multiple constructor implementations are not allowed. + constructor <> () { } + ~~ +!!! error TS1098: Type parameter list cannot be empty. + ~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2392: Multiple constructor implementations are not allowed. + constructor< >() { } + ~~ +!!! error TS1098: Type parameter list cannot be empty. + ~~~~~~~~~~~~~~~~~~~~ +!!! error TS2392: Multiple constructor implementations are not allowed. + constructor< > () { } + ~~ +!!! error TS1098: Type parameter list cannot be empty. + ~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2392: Multiple constructor implementations are not allowed. + constructor < >() { } + ~~ +!!! error TS1098: Type parameter list cannot be empty. + ~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2392: Multiple constructor implementations are not allowed. + constructor < > () { } + ~~ +!!! error TS1098: Type parameter list cannot be empty. + ~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2392: Multiple constructor implementations are not allowed. + } \ No newline at end of file diff --git a/tests/cases/compiler/parserConstructorDeclaration12.ts b/tests/cases/compiler/parserConstructorDeclaration12.ts new file mode 100644 index 00000000000..27d7daff1ef --- /dev/null +++ b/tests/cases/compiler/parserConstructorDeclaration12.ts @@ -0,0 +1,10 @@ +class C { + constructor<>() { } + constructor<> () { } + constructor <>() { } + constructor <> () { } + constructor< >() { } + constructor< > () { } + constructor < >() { } + constructor < > () { } +} \ No newline at end of file