Merge pull request #1202 from Microsoft/fixParsingShorthand

Improve error messages in parsing for short-hand property assignment
This commit is contained in:
Yui 2014-11-19 14:12:34 -08:00
commit dc0d93e150
9 changed files with 28 additions and 39 deletions

View File

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

View File

@ -488,7 +488,7 @@
"code": 1161
},
"A object member cannot be declared optional.": {
"An object member cannot be declared optional.": {
"category": "Error",
"code": 1160
},

View File

@ -2733,6 +2733,7 @@ module ts {
function parsePropertyAssignment(): Declaration {
var nodePos = scanner.getStartPos();
var tokenIsIdentifier = isIdentifier();
var nameToken = token;
var propertyName = parsePropertyName();
var node: Declaration;
@ -2752,12 +2753,12 @@ module ts {
// Disallow optional property assignment
if (token === SyntaxKind.QuestionToken) {
var questionStart = scanner.getTokenPos();
grammarErrorAtPos(questionStart, scanner.getStartPos() - questionStart, Diagnostics.A_object_member_cannot_be_declared_optional);
grammarErrorAtPos(questionStart, scanner.getStartPos() - questionStart, Diagnostics.An_object_member_cannot_be_declared_optional);
nextToken();
}
// Parse to check if it is short-hand property assignment or normal property assignment
if (token !== SyntaxKind.ColonToken && nameToken === SyntaxKind.Identifier) {
if ((token === SyntaxKind.CommaToken || token === SyntaxKind.CloseBraceToken) && tokenIsIdentifier) {
node = <ShortHandPropertyDeclaration>createNode(SyntaxKind.ShorthandPropertyAssignment, nodePos);
node.name = propertyName;
}

View File

@ -1,14 +1,11 @@
tests/cases/compiler/incompleteObjectLiteral1.ts(1,14): error TS1005: ',' expected.
tests/cases/compiler/incompleteObjectLiteral1.ts(1,14): error TS1005: ':' expected.
tests/cases/compiler/incompleteObjectLiteral1.ts(1,16): error TS1128: Declaration or statement expected.
tests/cases/compiler/incompleteObjectLiteral1.ts(1,12): error TS2304: Cannot find name 'aa'.
==== tests/cases/compiler/incompleteObjectLiteral1.ts (3 errors) ====
==== tests/cases/compiler/incompleteObjectLiteral1.ts (2 errors) ====
var tt = { aa; }
~
!!! error TS1005: ',' expected.
!!! error TS1005: ':' expected.
~
!!! error TS1128: Declaration or statement expected.
~~
!!! error TS2304: Cannot find name 'aa'.
var x = tt;

View File

@ -7,18 +7,17 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(9,8): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(10,10): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(12,1): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,6): error TS1005: ',' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,6): error TS1005: ',' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,6): error TS1005: '=' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(18,1): error TS1128: Declaration or statement expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,6): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,6): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,6): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(20,17): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(5,9): error TS2378: A 'get' accessor must return a value or consist of a single 'throw' statement.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,5): error TS2300: Duplicate identifier 'a'.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,7): error TS2304: Cannot find name 'b'.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,5): error TS2300: Duplicate identifier 'a'.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,5): error TS2300: Duplicate identifier 'a'.
==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts (18 errors) ====
==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts (17 errors) ====
// errors
var y = {
"stringLiteral",
@ -55,22 +54,20 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
var x = {
a.b,
~
!!! error TS1005: ',' expected.
!!! error TS1005: ':' expected.
~
!!! error TS2300: Duplicate identifier 'a'.
~
!!! error TS2304: Cannot find name 'b'.
a["ss"],
~
!!! error TS1005: ',' expected.
!!! error TS1005: ':' expected.
~
!!! error TS2300: Duplicate identifier 'a'.
a[1],
~
!!! error TS1005: '=' expected.
!!! error TS1005: ':' expected.
~
!!! error TS2300: Duplicate identifier 'a'.
};
~
!!! error TS1128: Declaration or statement expected.
var v = { class }; // error
~

View File

@ -1,4 +1,4 @@
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorWithModule.ts(10,10): error TS1005: ',' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorWithModule.ts(10,10): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorWithModule.ts(14,3): error TS2339: Property 'y' does not exist on type 'typeof m'.
@ -14,7 +14,7 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
export var y = {
m.x // error
~
!!! error TS1005: ',' expected.
!!! error TS1005: ':' expected.
};
}

View File

@ -1,6 +1,6 @@
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(24,6): error TS1160: An object member cannot be declared optional.
==== tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWithOptionalProperties.ts (3 errors) ====
@ -33,5 +33,5 @@ tests/cases/conformance/types/objectTypeLiteral/methodSignatures/objectTypesWith
var b = {
x?: 1 // error
!!! error TS1160: A object member cannot be declared optional.
!!! error TS1160: An object member cannot be declared optional.
}

View File

@ -1,16 +1,13 @@
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts(1,14): error TS1005: ',' expected.
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts(1,14): error TS1005: ':' expected.
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts(1,16): error TS1128: Declaration or statement expected.
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts(1,12): error TS2304: Cannot find name 'aa'.
==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts (3 errors) ====
==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser512097.ts (2 errors) ====
var tt = { aa; } // After this point, no useful parsing occurs in the entire file
~
!!! error TS1005: ',' expected.
!!! error TS1005: ':' expected.
~
!!! error TS1128: Declaration or statement expected.
~~
!!! error TS2304: Cannot find name 'aa'.
if (true) {
}

View File

@ -1,14 +1,11 @@
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts(2,1): error TS1005: ',' expected.
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts(2,1): error TS1005: ':' expected.
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts(2,7): error TS1005: ':' expected.
tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts(1,11): error TS2304: Cannot find name 'a'.
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts (3 errors) ====
==== tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts (2 errors) ====
var v = { a
~
!!! error TS2304: Cannot find name 'a'.
return;
~~~~~~
!!! error TS1005: ',' expected.
!!! error TS1005: ':' expected.
~
!!! error TS1005: ':' expected.