Move type parameter checks to the grammar checker.

This commit is contained in:
Cyrus Najmabadi 2014-11-19 00:11:59 -08:00
parent 7fb21a25f6
commit de63023b1e
4 changed files with 105 additions and 28 deletions

View File

@ -1614,14 +1614,7 @@ module ts {
function parseTypeParameters(): NodeArray<TypeParameterDeclaration> {
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<TypeParameterDeclaration>): 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<ParameterDeclaration>): 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 {

View File

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

View File

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

View File

@ -0,0 +1,10 @@
class C {
constructor<>() { }
constructor<> () { }
constructor <>() { }
constructor <> () { }
constructor< >() { }
constructor< > () { }
constructor < >() { }
constructor < > () { }
}