Skip to content

Commit eaba180

Browse files
Merge pull request #1971 from Microsoft/commaSeparator
Allow commas as a separator between object/interface type members.
2 parents 50f9ce4 + c03c9a4 commit eaba180

13 files changed

+73
-65
lines changed

src/compiler/parser.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,17 +2366,14 @@ module ts {
23662366
}
23672367

23682368
function parseTypeMemberSemicolon() {
2369-
// Try to parse out an explicit or implicit (ASI) semicolon for a type member. If we
2370-
// don't have one, then an appropriate error will be reported.
2371-
if (parseSemicolon()) {
2369+
// We allow type members to be separated by commas or (possibly ASI) semicolons.
2370+
// First check if it was a comma. If so, we're done with the member.
2371+
if (parseOptional(SyntaxKind.CommaToken)) {
23722372
return;
23732373
}
23742374

2375-
// If we don't have a semicolon, then the user may have written a comma instead
2376-
// accidently (pretty easy to do since commas are so prevalent as list separators). So
2377-
// just consume the comma and keep going. Note: we'll have already reported the error
2378-
// about the missing semicolon above.
2379-
parseOptional(SyntaxKind.CommaToken);
2375+
// Didn't have a comma. We must have a (possible ASI) semicolon.
2376+
parseSemicolon();
23802377
}
23812378

23822379
function parseSignatureMember(kind: SyntaxKind): SignatureDeclaration {

tests/baselines/reference/ExportObjectLiteralAndObjectTypeLiteralWithAccessibleTypesInNestedMemberTypeAnnotations.errors.txt

Lines changed: 0 additions & 23 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
=== tests/cases/conformance/internalModules/exportDeclarations/ExportObjectLiteralAndObjectTypeLiteralWithAccessibleTypesInNestedMemberTypeAnnotations.ts ===
2+
module A {
3+
>A : typeof A
4+
5+
class Point {
6+
>Point : Point
7+
8+
constructor(public x: number, public y: number) { }
9+
>x : number
10+
>y : number
11+
}
12+
13+
export var UnitSquare : {
14+
>UnitSquare : { top: { left: Point; right: Point; }; bottom: { left: Point; right: Point; }; }
15+
16+
top: { left: Point, right: Point },
17+
>top : { left: Point; right: Point; }
18+
>left : Point
19+
>Point : Point
20+
>right : Point
21+
>Point : Point
22+
23+
bottom: { left: Point, right: Point }
24+
>bottom : { left: Point; right: Point; }
25+
>left : Point
26+
>Point : Point
27+
>right : Point
28+
>Point : Point
29+
30+
} = null;
31+
}

tests/baselines/reference/complicatedPrivacy.errors.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
tests/cases/compiler/complicatedPrivacy.ts(24,38): error TS1005: ';' expected.
1+
tests/cases/compiler/complicatedPrivacy.ts(11,24): error TS1054: A 'get' accessor cannot have parameters.
2+
tests/cases/compiler/complicatedPrivacy.ts(35,5): error TS1170: Computed property names are not allowed in type literals.
23
tests/cases/compiler/complicatedPrivacy.ts(35,6): error TS2304: Cannot find name 'number'.
34
tests/cases/compiler/complicatedPrivacy.ts(73,49): error TS2305: Module 'mglo5' has no exported member 'i6'.
45

56

6-
==== tests/cases/compiler/complicatedPrivacy.ts (3 errors) ====
7+
==== tests/cases/compiler/complicatedPrivacy.ts (4 errors) ====
78
module m1 {
89
export module m2 {
910

@@ -15,6 +16,8 @@ tests/cases/compiler/complicatedPrivacy.ts(73,49): error TS2305: Module 'mglo5'
1516

1617
export class C2 implements m3.i3 {
1718
public get p1(arg) {
19+
~~
20+
!!! error TS1054: A 'get' accessor cannot have parameters.
1821
return new C1();
1922
}
2023

@@ -28,8 +31,6 @@ tests/cases/compiler/complicatedPrivacy.ts(73,49): error TS2305: Module 'mglo5'
2831
}
2932

3033
export function f2(arg1: { x?: C1, y: number }) {
31-
~
32-
!!! error TS1005: ';' expected.
3334
}
3435

3536
export function f3(): {
@@ -41,6 +42,8 @@ tests/cases/compiler/complicatedPrivacy.ts(73,49): error TS2305: Module 'mglo5'
4142
export function f4(arg1:
4243
{
4344
[number]: C1; // Used to be indexer, now it is a computed property
45+
~~~~~~~~
46+
!!! error TS1170: Computed property names are not allowed in type literals.
4447
~~~~~~
4548
!!! error TS2304: Cannot find name 'number'.
4649
}) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//// [interfaceWithCommaSeparators.ts]
2+
var v: { bar(): void, baz }
3+
interface Foo { bar(): void, baz }
4+
5+
//// [interfaceWithCommaSeparators.js]
6+
var v;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/compiler/interfaceWithCommaSeparators.ts ===
2+
var v: { bar(): void, baz }
3+
>v : { bar(): void; baz: any; }
4+
>bar : () => void
5+
>baz : any
6+
7+
interface Foo { bar(): void, baz }
8+
>Foo : Foo
9+
>bar : () => void
10+
>baz : any
11+

tests/baselines/reference/objectLiteralShorthandPropertiesAssignmentError.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
33
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts(5,16): error TS1131: Property or signature expected.
44
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts(5,22): error TS2403: Subsequent variable declarations must have the same type. Variable 'id' must be of type 'number', but here has type 'any'.
55
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts(5,25): error TS1128: Declaration or statement expected.
6-
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts(6,53): error TS1005: ';' expected.
76
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts(6,79): error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ id: string; name: number; }'.
87
Types of property 'id' are incompatible.
98
Type 'number' is not assignable to type 'string'.
@@ -12,7 +11,7 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
1211
Type 'number' is not assignable to type 'boolean'.
1312

1413

15-
==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts (7 errors) ====
14+
==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentError.ts (6 errors) ====
1615
var id: number = 10000;
1716
var name: string = "my name";
1817

@@ -28,8 +27,6 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
2827
~
2928
!!! error TS1128: Declaration or statement expected.
3029
function foo(name: string, id: number): { id: string, name: number } { return { name, id }; } // error
31-
~
32-
!!! error TS1005: ';' expected.
3330
~~~~~~~~~~~~
3431
!!! error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ id: string; name: number; }'.
3532
!!! error TS2322: Types of property 'id' are incompatible.
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(4,5): error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ b: string; id: number; }'.
22
Property 'b' is missing in type '{ name: string; id: number; }'.
3-
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(5,55): error TS1005: ';' expected.
43
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(5,79): error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ name: number; id: string; }'.
54
Types of property 'name' are incompatible.
65
Type 'string' is not assignable to type 'number'.
7-
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(6,55): error TS1005: ';' expected.
86
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(7,16): error TS1131: Property or signature expected.
97
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(7,22): error TS2403: Subsequent variable declarations must have the same type. Variable 'id' must be of type 'number', but here has type 'any'.
108
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(7,25): error TS1128: Declaration or statement expected.
119
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(8,5): error TS2322: Type '{ name: number; id: string; }' is not assignable to type '{ name: string; id: number; }'.
1210
Types of property 'name' are incompatible.
1311
Type 'number' is not assignable to type 'string'.
14-
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts(8,28): error TS1005: ';' expected.
1512

1613

17-
==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts (9 errors) ====
14+
==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesAssignmentErrorFromMissingIdentifier.ts (6 errors) ====
1815
var id: number = 10000;
1916
var name: string = "my name";
2017

@@ -23,15 +20,11 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
2320
!!! error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ b: string; id: number; }'.
2421
!!! error TS2322: Property 'b' is missing in type '{ name: string; id: number; }'.
2522
function bar(name: string, id: number): { name: number, id: string } { return { name, id }; } // error
26-
~
27-
!!! error TS1005: ';' expected.
2823
~~~~~~~~~~~~
2924
!!! error TS2322: Type '{ name: string; id: number; }' is not assignable to type '{ name: number; id: string; }'.
3025
!!! error TS2322: Types of property 'name' are incompatible.
3126
!!! error TS2322: Type 'string' is not assignable to type 'number'.
3227
function foo(name: string, id: number): { name: string, id: number } { return { name, id }; } // error
33-
~
34-
!!! error TS1005: ';' expected.
3528
var person1: { name, id }; // error : Can't use shorthand in the type position
3629
~~~~
3730
!!! error TS1131: Property or signature expected.
@@ -43,6 +36,4 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
4336
~~~~~~~
4437
!!! error TS2322: Type '{ name: number; id: string; }' is not assignable to type '{ name: string; id: number; }'.
4538
!!! error TS2322: Types of property 'name' are incompatible.
46-
!!! error TS2322: Type 'number' is not assignable to type 'string'.
47-
~
48-
!!! error TS1005: ';' expected.
39+
!!! error TS2322: Type 'number' is not assignable to type 'string'.

tests/baselines/reference/objectTypeLiteralSyntax2.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
tests/cases/conformance/types/objectTypeLiteral/objectTypeLiteralSyntax2.ts(2,16): error TS1005: ';' expected.
21
tests/cases/conformance/types/objectTypeLiteral/objectTypeLiteralSyntax2.ts(12,22): error TS1005: ';' expected.
32

43

5-
==== tests/cases/conformance/types/objectTypeLiteral/objectTypeLiteralSyntax2.ts (2 errors) ====
4+
==== tests/cases/conformance/types/objectTypeLiteral/objectTypeLiteralSyntax2.ts (1 errors) ====
65
var x: {
76
foo: string,
8-
~
9-
!!! error TS1005: ';' expected.
107
bar: string
118
}
129

tests/baselines/reference/parserCommaInTypeMemberList1.errors.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)