Move optional method/property checking to the grammar checker.
This commit is contained in:
parent
bffd1d2ae2
commit
7716535511
2 changed files with 15 additions and 4 deletions
|
@ -2968,7 +2968,9 @@ module ts {
|
|||
|
||||
var questionStart = scanner.getTokenPos();
|
||||
if (parseOptional(SyntaxKind.QuestionToken)) {
|
||||
errorAtPos(questionStart, scanner.getStartPos() - questionStart, Diagnostics.A_class_member_cannot_be_declared_optional);
|
||||
// Note: this is not legal as per the grammar. But we allow it in the parser and
|
||||
// report an error in the grammar checker.
|
||||
flags |= NodeFlags.QuestionMark;
|
||||
}
|
||||
|
||||
if (token === SyntaxKind.OpenParenToken || token === SyntaxKind.LessThanToken) {
|
||||
|
@ -4179,7 +4181,8 @@ module ts {
|
|||
function visitMethod(node: MethodDeclaration) {
|
||||
checkTypeParameterList(node.typeParameters) ||
|
||||
checkParameterList(node.parameters) ||
|
||||
checkForBodyInAmbientContext(node.body, /*isConstructor:*/ false);
|
||||
checkForBodyInAmbientContext(node.body, /*isConstructor:*/ false) ||
|
||||
(node.parent.kind === SyntaxKind.ClassDeclaration && checkForInvalidQuestionMark(node, Diagnostics.A_class_member_cannot_be_declared_optional));
|
||||
}
|
||||
|
||||
function checkForBodyInAmbientContext(body: Block | Expression, isConstructor: boolean): boolean {
|
||||
|
@ -4391,8 +4394,13 @@ module ts {
|
|||
}
|
||||
|
||||
function visitProperty(node: PropertyDeclaration) {
|
||||
(node.parent.kind === SyntaxKind.ClassDeclaration && checkForInvalidQuestionMark(node, Diagnostics.A_class_member_cannot_be_declared_optional)) ||
|
||||
checkForInitializerInAmbientContext(node);
|
||||
}
|
||||
|
||||
function checkForInitializerInAmbientContext(node: PropertyDeclaration) {
|
||||
if (inAmbientContext && node.initializer) {
|
||||
grammarErrorOnFirstToken(node.initializer, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
|
||||
return grammarErrorOnFirstToken(node.initializer, Diagnostics.Initializers_are_not_allowed_in_ambient_contexts);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts(12,6): error TS1112: A class member cannot be declared optional.
|
||||
tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts(20,6): error TS1112: A class member cannot be declared optional.
|
||||
tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts(24,6): error TS1160: An object member cannot be declared optional.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts (2 errors) ====
|
||||
==== tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts (3 errors) ====
|
||||
// Basic uses of optional properties
|
||||
|
||||
var a: {
|
||||
|
@ -31,4 +32,6 @@ tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWith
|
|||
|
||||
var b = {
|
||||
x?: 1 // error
|
||||
~
|
||||
!!! error TS1160: An object member cannot be declared optional.
|
||||
}
|
Loading…
Reference in a new issue