mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 16:38:05 -06:00
Move optional property checking to the grammar checker.
This commit is contained in:
parent
90b7a1c40a
commit
cd75243d83
@ -124,7 +124,7 @@ module ts {
|
||||
Tagged_templates_are_only_available_when_targeting_ECMAScript_6_and_higher: { code: 1159, category: DiagnosticCategory.Error, key: "Tagged templates are only available when targeting ECMAScript 6 and higher." },
|
||||
Unterminated_template_literal: { code: 1160, category: DiagnosticCategory.Error, key: "Unterminated template literal." },
|
||||
Unterminated_regular_expression_literal: { code: 1161, category: DiagnosticCategory.Error, key: "Unterminated regular expression literal." },
|
||||
A_object_member_cannot_be_declared_optional: { code: 1160, category: DiagnosticCategory.Error, key: "A object member cannot be declared optional." },
|
||||
An_object_member_cannot_be_declared_optional: { code: 1160, category: DiagnosticCategory.Error, key: "An object member cannot be declared optional." },
|
||||
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
|
||||
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
|
||||
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },
|
||||
|
||||
@ -479,18 +479,17 @@
|
||||
"category": "Error",
|
||||
"code": 1159
|
||||
},
|
||||
"Unterminated template literal.": {
|
||||
"An object member cannot be declared optional.": {
|
||||
"category": "Error",
|
||||
"code": 1160
|
||||
"code": 1160
|
||||
},
|
||||
"Unterminated regular expression literal.": {
|
||||
"Unterminated template literal.": {
|
||||
"category": "Error",
|
||||
"code": 1161
|
||||
},
|
||||
|
||||
"A object member cannot be declared optional.": {
|
||||
"Unterminated regular expression literal.": {
|
||||
"category": "Error",
|
||||
"code": 1160
|
||||
"code": 1162
|
||||
},
|
||||
|
||||
"Duplicate identifier '{0}'.": {
|
||||
|
||||
@ -2264,7 +2264,6 @@ module ts {
|
||||
}
|
||||
|
||||
if (parseOptional(SyntaxKind.OpenBracketToken)) {
|
||||
|
||||
var indexedAccess = <IndexedAccess>createNode(SyntaxKind.IndexedAccess, expr.pos);
|
||||
indexedAccess.object = expr;
|
||||
|
||||
@ -2441,10 +2440,12 @@ module ts {
|
||||
(<PropertyDeclaration>node).initializer = makeFunctionExpression(SyntaxKind.FunctionExpression, node.pos, undefined, sig, body);
|
||||
return finishNode(node);
|
||||
}
|
||||
// Disallow optional property assignment
|
||||
|
||||
var flags: NodeFlags = 0;
|
||||
|
||||
// Disallowing of optional property assignments happens in the grammar checker.
|
||||
if (token === SyntaxKind.QuestionToken) {
|
||||
var questionStart = scanner.getTokenPos();
|
||||
grammarErrorAtPos(questionStart, scanner.getStartPos() - questionStart, Diagnostics.A_object_member_cannot_be_declared_optional);
|
||||
flags |= NodeFlags.QuestionMark;
|
||||
nextToken();
|
||||
}
|
||||
|
||||
@ -2459,6 +2460,8 @@ module ts {
|
||||
parseExpected(SyntaxKind.ColonToken);
|
||||
(<PropertyDeclaration>node).initializer = parseAssignmentExpression(false);
|
||||
}
|
||||
|
||||
node.flags = flags;
|
||||
return finishNode(node);
|
||||
}
|
||||
|
||||
@ -3740,42 +3743,44 @@ module ts {
|
||||
|
||||
function dispatch(node: Node) {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.ArrowFunction: return visitArrowFunction(<FunctionExpression>node);
|
||||
case SyntaxKind.BinaryExpression: return visitBinaryExpression(<BinaryExpression>node);
|
||||
case SyntaxKind.BreakStatement: return visitBreakOrContinueStatement(<BreakOrContinueStatement>node);
|
||||
case SyntaxKind.CallExpression: return visitCallExpression(<CallExpression>node);
|
||||
case SyntaxKind.CallSignature: return visitCallSignature(<SignatureDeclaration>node);
|
||||
case SyntaxKind.CatchBlock: return visitCatchBlock(<CatchBlock>node);
|
||||
case SyntaxKind.ClassDeclaration: return visitClassDeclaration(<ClassDeclaration>node);
|
||||
case SyntaxKind.Constructor: return visitConstructor(<ConstructorDeclaration>node);
|
||||
case SyntaxKind.ConstructorType: return visitConstructorType(<SignatureDeclaration>node);
|
||||
case SyntaxKind.ConstructSignature: return visitConstructSignature(<SignatureDeclaration>node);
|
||||
case SyntaxKind.ContinueStatement: return visitBreakOrContinueStatement(<BreakOrContinueStatement>node);
|
||||
case SyntaxKind.EnumDeclaration: return visitEnumDeclaration(<EnumDeclaration>node);
|
||||
case SyntaxKind.ForInStatement: return visitForInStatement(<ForInStatement>node);
|
||||
case SyntaxKind.ForStatement: return visitForStatement(<ForStatement>node);
|
||||
case SyntaxKind.FunctionDeclaration: return visitFunctionDeclaration(<FunctionLikeDeclaration>node);
|
||||
case SyntaxKind.FunctionExpression: return visitFunctionExpression(<FunctionExpression>node);
|
||||
case SyntaxKind.FunctionType: return visitFunctionType(<SignatureDeclaration>node);
|
||||
case SyntaxKind.GetAccessor: return visitGetAccessor(<MethodDeclaration>node);
|
||||
case SyntaxKind.IndexSignature: return visitIndexSignature(<SignatureDeclaration>node);
|
||||
case SyntaxKind.InterfaceDeclaration: return visitInterfaceDeclaration(<InterfaceDeclaration>node);
|
||||
case SyntaxKind.LabeledStatement: return visitLabeledStatement(<LabeledStatement>node);
|
||||
case SyntaxKind.Method: return visitMethod(<MethodDeclaration>node);
|
||||
case SyntaxKind.ModuleDeclaration: return visitModuleDeclaration(<ModuleDeclaration>node);
|
||||
case SyntaxKind.NewExpression: return visitNewExpression(<NewExpression>node);
|
||||
case SyntaxKind.ObjectLiteral: return visitObjectLiteral(<ObjectLiteral>node);
|
||||
case SyntaxKind.Parameter: return visitParameter(<ParameterDeclaration>node);
|
||||
case SyntaxKind.PostfixOperator: return visitPostfixOperator(<UnaryExpression>node);
|
||||
case SyntaxKind.PrefixOperator: return visitPrefixOperator(<UnaryExpression>node);
|
||||
case SyntaxKind.SetAccessor: return visitSetAccessor(<MethodDeclaration>node);
|
||||
case SyntaxKind.SwitchStatement: return visitSwitchStatement(<SwitchStatement>node);
|
||||
case SyntaxKind.TaggedTemplateExpression: return visitTaggedTemplateExpression(<TaggedTemplateExpression>node);
|
||||
case SyntaxKind.TupleType: return visitTupleType(<TupleTypeNode>node);
|
||||
case SyntaxKind.TypeReference: return visitTypeReference(<TypeReferenceNode>node);
|
||||
case SyntaxKind.VariableDeclaration: return visitVariableDeclaration(<VariableDeclaration>node);
|
||||
case SyntaxKind.VariableStatement: return visitVariableStatement(<VariableStatement>node);
|
||||
case SyntaxKind.WithStatement: return visitWithStatement(<WithStatement>node);
|
||||
case SyntaxKind.ArrowFunction: return visitArrowFunction(<FunctionExpression>node);
|
||||
case SyntaxKind.BinaryExpression: return visitBinaryExpression(<BinaryExpression>node);
|
||||
case SyntaxKind.BreakStatement: return visitBreakOrContinueStatement(<BreakOrContinueStatement>node);
|
||||
case SyntaxKind.CallExpression: return visitCallExpression(<CallExpression>node);
|
||||
case SyntaxKind.CallSignature: return visitCallSignature(<SignatureDeclaration>node);
|
||||
case SyntaxKind.CatchBlock: return visitCatchBlock(<CatchBlock>node);
|
||||
case SyntaxKind.ClassDeclaration: return visitClassDeclaration(<ClassDeclaration>node);
|
||||
case SyntaxKind.Constructor: return visitConstructor(<ConstructorDeclaration>node);
|
||||
case SyntaxKind.ConstructorType: return visitConstructorType(<SignatureDeclaration>node);
|
||||
case SyntaxKind.ConstructSignature: return visitConstructSignature(<SignatureDeclaration>node);
|
||||
case SyntaxKind.ContinueStatement: return visitBreakOrContinueStatement(<BreakOrContinueStatement>node);
|
||||
case SyntaxKind.EnumDeclaration: return visitEnumDeclaration(<EnumDeclaration>node);
|
||||
case SyntaxKind.ForInStatement: return visitForInStatement(<ForInStatement>node);
|
||||
case SyntaxKind.ForStatement: return visitForStatement(<ForStatement>node);
|
||||
case SyntaxKind.FunctionDeclaration: return visitFunctionDeclaration(<FunctionLikeDeclaration>node);
|
||||
case SyntaxKind.FunctionExpression: return visitFunctionExpression(<FunctionExpression>node);
|
||||
case SyntaxKind.FunctionType: return visitFunctionType(<SignatureDeclaration>node);
|
||||
case SyntaxKind.GetAccessor: return visitGetAccessor(<MethodDeclaration>node);
|
||||
case SyntaxKind.IndexSignature: return visitIndexSignature(<SignatureDeclaration>node);
|
||||
case SyntaxKind.InterfaceDeclaration: return visitInterfaceDeclaration(<InterfaceDeclaration>node);
|
||||
case SyntaxKind.LabeledStatement: return visitLabeledStatement(<LabeledStatement>node);
|
||||
case SyntaxKind.Method: return visitMethod(<MethodDeclaration>node);
|
||||
case SyntaxKind.ModuleDeclaration: return visitModuleDeclaration(<ModuleDeclaration>node);
|
||||
case SyntaxKind.NewExpression: return visitNewExpression(<NewExpression>node);
|
||||
case SyntaxKind.ObjectLiteral: return visitObjectLiteral(<ObjectLiteral>node);
|
||||
case SyntaxKind.Parameter: return visitParameter(<ParameterDeclaration>node);
|
||||
case SyntaxKind.PostfixOperator: return visitPostfixOperator(<UnaryExpression>node);
|
||||
case SyntaxKind.PrefixOperator: return visitPrefixOperator(<UnaryExpression>node);
|
||||
case SyntaxKind.PropertyAssignment: return visitPropertyAssignment(<PropertyDeclaration>node);
|
||||
case SyntaxKind.SetAccessor: return visitSetAccessor(<MethodDeclaration>node);
|
||||
case SyntaxKind.ShorthandPropertyAssignment: return visitShorthandPropertyAssignment(<ShortHandPropertyDeclaration>node);
|
||||
case SyntaxKind.SwitchStatement: return visitSwitchStatement(<SwitchStatement>node);
|
||||
case SyntaxKind.TaggedTemplateExpression: return visitTaggedTemplateExpression(<TaggedTemplateExpression>node);
|
||||
case SyntaxKind.TupleType: return visitTupleType(<TupleTypeNode>node);
|
||||
case SyntaxKind.TypeReference: return visitTypeReference(<TypeReferenceNode>node);
|
||||
case SyntaxKind.VariableDeclaration: return visitVariableDeclaration(<VariableDeclaration>node);
|
||||
case SyntaxKind.VariableStatement: return visitVariableStatement(<VariableStatement>node);
|
||||
case SyntaxKind.WithStatement: return visitWithStatement(<WithStatement>node);
|
||||
}
|
||||
}
|
||||
|
||||
@ -4346,6 +4351,17 @@ module ts {
|
||||
}
|
||||
}
|
||||
|
||||
function visitPropertyAssignment(node: PropertyDeclaration) {
|
||||
checkForInvalidQuestionMark(node, Diagnostics.An_object_member_cannot_be_declared_optional);
|
||||
}
|
||||
|
||||
function checkForInvalidQuestionMark(node: Declaration, message: DiagnosticMessage) {
|
||||
if (node.flags & NodeFlags.QuestionMark) {
|
||||
var pos = skipTrivia(sourceText, node.name.end);
|
||||
return grammarErrorAtPos(pos, "?".length, message);
|
||||
}
|
||||
}
|
||||
|
||||
function visitSetAccessor(node: MethodDeclaration) {
|
||||
checkTypeParameterList(node.typeParameters) ||
|
||||
checkParameterList(node.parameters) ||
|
||||
@ -4391,6 +4407,10 @@ module ts {
|
||||
}
|
||||
}
|
||||
|
||||
function visitShorthandPropertyAssignment(node: ShortHandPropertyDeclaration): void {
|
||||
checkForInvalidQuestionMark(node, Diagnostics.An_object_member_cannot_be_declared_optional);
|
||||
}
|
||||
|
||||
function visitSwitchStatement(node: SwitchStatement) {
|
||||
var firstDefaultClause: CaseOrDefaultClause;
|
||||
|
||||
|
||||
@ -0,0 +1,9 @@
|
||||
tests/cases/compiler/objectTypeWithOptionalProperty1.ts(2,10): error TS1160: An object member cannot be declared optional.
|
||||
|
||||
|
||||
==== tests/cases/compiler/objectTypeWithOptionalProperty1.ts (1 errors) ====
|
||||
var b = {
|
||||
x?: 1 // error
|
||||
~
|
||||
!!! error TS1160: An object member cannot be declared optional.
|
||||
}
|
||||
@ -1,9 +1,8 @@
|
||||
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: A object member cannot be declared optional.
|
||||
|
||||
|
||||
==== tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts (3 errors) ====
|
||||
==== tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts (2 errors) ====
|
||||
// Basic uses of optional properties
|
||||
|
||||
var a: {
|
||||
@ -32,6 +31,4 @@ tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWith
|
||||
|
||||
var b = {
|
||||
x?: 1 // error
|
||||
|
||||
!!! error TS1160: A object member cannot be declared optional.
|
||||
}
|
||||
3
tests/cases/compiler/objectTypeWithOptionalProperty1.ts
Normal file
3
tests/cases/compiler/objectTypeWithOptionalProperty1.ts
Normal file
@ -0,0 +1,3 @@
|
||||
var b = {
|
||||
x?: 1 // error
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user