Skip to content

Commit 36dfd77

Browse files
author
Andy
authored
Parse an object literal property as shorthand unless followed by '(' or ':' (microsoft#28121)
1 parent abce9ae commit 36dfd77

File tree

40 files changed

+163
-93
lines changed

40 files changed

+163
-93
lines changed

src/compiler/parser.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4738,8 +4738,7 @@ namespace ts {
47384738
// CoverInitializedName[Yield] :
47394739
// IdentifierReference[?Yield] Initializer[In, ?Yield]
47404740
// this is necessary because ObjectLiteral productions are also used to cover grammar for ObjectAssignmentPattern
4741-
const isShorthandPropertyAssignment =
4742-
tokenIsIdentifier && (token() === SyntaxKind.CommaToken || token() === SyntaxKind.CloseBraceToken || token() === SyntaxKind.EqualsToken);
4741+
const isShorthandPropertyAssignment = tokenIsIdentifier && (token() !== SyntaxKind.ColonToken);
47434742
if (isShorthandPropertyAssignment) {
47444743
node.kind = SyntaxKind.ShorthandPropertyAssignment;
47454744
const equalsToken = parseOptionalToken(SyntaxKind.EqualsToken);

src/testRunner/unittests/convertCompilerOptionsFromJson.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,8 @@ namespace ts {
596596
{
597597
compilerOptions: {
598598
target: undefined,
599-
module: ModuleKind.ESNext
599+
module: ModuleKind.ESNext,
600+
experimentalDecorators: true,
600601
},
601602
hasParseErrors: true
602603
}

src/testRunner/unittests/tsconfigParsing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ namespace ts {
141141

142142
it("returns object with error when json is invalid", () => {
143143
const parsed = parseConfigFileTextToJson("/apath/tsconfig.json", "invalid");
144-
assert.deepEqual(parsed.config, { invalid: undefined });
144+
assert.deepEqual(parsed.config, {});
145145
const expected = createCompilerDiagnostic(Diagnostics._0_expected, "{");
146146
const error = parsed.error!;
147147
assert.equal(error.messageText, expected.messageText);
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
tests/cases/compiler/incompleteObjectLiteral1.ts(1,14): error TS1005: ':' expected.
1+
tests/cases/compiler/incompleteObjectLiteral1.ts(1,12): error TS2304: Cannot find name 'aa'.
2+
tests/cases/compiler/incompleteObjectLiteral1.ts(1,14): error TS1005: ',' expected.
23

34

4-
==== tests/cases/compiler/incompleteObjectLiteral1.ts (1 errors) ====
5+
==== tests/cases/compiler/incompleteObjectLiteral1.ts (2 errors) ====
56
var tt = { aa; }
7+
~~
8+
!!! error TS2304: Cannot find name 'aa'.
69
~
7-
!!! error TS1005: ':' expected.
10+
!!! error TS1005: ',' expected.
811
var x = tt;

tests/baselines/reference/incompleteObjectLiteral1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ var tt = { aa; }
33
var x = tt;
44

55
//// [incompleteObjectLiteral1.js]
6-
var tt = { aa: };
6+
var tt = { aa: aa };
77
var x = tt;

tests/baselines/reference/incompleteObjectLiteral1.types

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ var tt = { aa; }
33
>tt : { aa: any; }
44
>{ aa; } : { aa: any; }
55
>aa : any
6-
> : any
76

87
var x = tt;
98
>x : { aa: any; }

tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.errors.txt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
77
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(9,8): error TS1005: ':' expected.
88
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(10,10): error TS1005: ':' expected.
99
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(12,1): error TS1005: ':' expected.
10-
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,6): error TS1005: ':' expected.
11-
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,6): error TS1005: ':' expected.
12-
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,6): error TS1005: ':' expected.
10+
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,5): error TS2304: Cannot find name 'a'.
11+
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,6): error TS1005: ',' expected.
12+
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,5): error TS2304: Cannot find name 'a'.
13+
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,6): error TS1005: ',' expected.
14+
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,12): error TS1005: ':' expected.
15+
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,5): error TS2304: Cannot find name 'a'.
16+
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,6): error TS1005: ',' expected.
17+
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,9): error TS1005: ':' expected.
1318
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(20,17): error TS1005: ':' expected.
1419

1520

16-
==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts (13 errors) ====
21+
==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts (18 errors) ====
1722
// errors
1823
var y = {
1924
"stringLiteral",
@@ -47,13 +52,23 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
4752

4853
var x = {
4954
a.b,
55+
~
56+
!!! error TS2304: Cannot find name 'a'.
5057
~
51-
!!! error TS1005: ':' expected.
58+
!!! error TS1005: ',' expected.
5259
a["ss"],
60+
~
61+
!!! error TS2304: Cannot find name 'a'.
5362
~
63+
!!! error TS1005: ',' expected.
64+
~
5465
!!! error TS1005: ':' expected.
5566
a[1],
67+
~
68+
!!! error TS2304: Cannot find name 'a'.
5669
~
70+
!!! error TS1005: ',' expected.
71+
~
5772
!!! error TS1005: ':' expected.
5873
};
5974

tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var x = {
2121
var v = { class }; // error
2222

2323
//// [objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.js]
24+
var _a;
2425
// errors
2526
var y = {
2627
"stringLiteral": ,
@@ -33,9 +34,12 @@ var y = {
3334
"class": ,
3435
"typeof":
3536
};
36-
var x = {
37-
a: .b,
38-
a: ["ss"],
39-
a: [1]
40-
};
37+
var x = (_a = {
38+
a: a, : .b,
39+
a: a
40+
},
41+
_a["ss"] = ,
42+
_a.a = a,
43+
_a[1] = ,
44+
_a);
4145
var v = { "class": }; // error

tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.symbols

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,17 @@ var x = {
3737

3838
a.b,
3939
>a : Symbol(a, Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 13, 9), Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 14, 8), Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 15, 12))
40+
> : Symbol((Missing), Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 14, 5))
4041

4142
a["ss"],
4243
>a : Symbol(a, Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 13, 9), Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 14, 8), Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 15, 12))
44+
>["ss"] : Symbol(["ss"], Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 15, 5))
45+
>"ss" : Symbol(["ss"], Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 15, 5))
4346

4447
a[1],
4548
>a : Symbol(a, Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 13, 9), Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 14, 8), Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 15, 12))
49+
>[1] : Symbol([1], Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 16, 5))
50+
>1 : Symbol([1], Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 16, 5))
4651

4752
};
4853

tests/baselines/reference/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.types

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,27 @@ var y = {
4141
> : any
4242

4343
var x = {
44-
>x : { a: number[]; }
45-
>{ a.b, a["ss"], a[1],} : { a: number[]; }
44+
>x : { a: any; (Missing): any; ["ss"]: any; [1]: any; }
45+
>{ a.b, a["ss"], a[1],} : { a: any; (Missing): any; ["ss"]: any; [1]: any; }
4646

4747
a.b,
4848
>a : any
49+
> : any
4950
>.b : any
5051
> : any
5152
>b : any
5253

5354
a["ss"],
5455
>a : any
55-
>["ss"] : string[]
56+
>["ss"] : any
5657
>"ss" : "ss"
58+
> : any
5759

5860
a[1],
5961
>a : any
60-
>[1] : number[]
62+
>[1] : any
6163
>1 : 1
64+
> : any
6265

6366
};
6467

tests/baselines/reference/objectLiteralShorthandPropertiesErrorWithModule.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorWithModule.ts(10,10): error TS1005: ':' expected.
1+
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorWithModule.ts(10,10): error TS1005: ',' expected.
22
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorWithModule.ts(14,3): error TS2339: Property 'y' does not exist on type 'typeof m'.
33

44

@@ -14,7 +14,7 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
1414
export var y = {
1515
m.x // error
1616
~
17-
!!! error TS1005: ':' expected.
17+
!!! error TS1005: ',' expected.
1818
};
1919
}
2020

tests/baselines/reference/objectLiteralShorthandPropertiesErrorWithModule.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var n;
2525
(function (n) {
2626
var z = 10000;
2727
n.y = {
28-
m: .x // error
28+
m: m, : .x // error
2929
};
3030
})(n || (n = {}));
3131
m.y.x;

tests/baselines/reference/objectLiteralShorthandPropertiesErrorWithModule.symbols

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module n {
2121

2222
m.x // error
2323
>m : Symbol(m, Decl(objectLiteralShorthandPropertiesErrorWithModule.ts, 8, 20))
24+
> : Symbol((Missing), Decl(objectLiteralShorthandPropertiesErrorWithModule.ts, 9, 9))
2425

2526
};
2627
}

tests/baselines/reference/objectLiteralShorthandPropertiesErrorWithModule.types

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ module n {
1919
>10000 : 10000
2020

2121
export var y = {
22-
>y : { m: any; }
23-
>{ m.x // error } : { m: any; }
22+
>y : { m: typeof m; (Missing): any; }
23+
>{ m.x // error } : { m: typeof m; (Missing): any; }
2424

2525
m.x // error
26-
>m : any
26+
>m : typeof m
27+
> : any
2728
>.x : any
2829
> : any
2930
>x : any
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,12): error TS1005: ':' expected.
2-
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,15): error TS1005: ':' expected.
1+
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,11): error TS2304: Cannot find name 'a'.
2+
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,12): error TS1005: ',' expected.
3+
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,14): error TS2304: Cannot find name 'b'.
4+
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,15): error TS1005: ',' expected.
35
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,17): error TS2304: Cannot find name 'c'.
46

57

6-
==== tests/cases/compiler/objectLiteralWithSemicolons1.ts (3 errors) ====
8+
==== tests/cases/compiler/objectLiteralWithSemicolons1.ts (5 errors) ====
79
var v = { a; b; c }
10+
~
11+
!!! error TS2304: Cannot find name 'a'.
812
~
9-
!!! error TS1005: ':' expected.
13+
!!! error TS1005: ',' expected.
14+
~
15+
!!! error TS2304: Cannot find name 'b'.
1016
~
11-
!!! error TS1005: ':' expected.
17+
!!! error TS1005: ',' expected.
1218
~
1319
!!! error TS2304: Cannot find name 'c'.

tests/baselines/reference/objectLiteralWithSemicolons1.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
var v = { a; b; c }
33

44
//// [objectLiteralWithSemicolons1.js]
5-
var v = { a: , b: , c: c };
5+
var v = { a: a, b: b, c: c };

tests/baselines/reference/objectLiteralWithSemicolons1.types

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ var v = { a; b; c }
33
>v : { a: any; b: any; c: any; }
44
>{ a; b; c } : { a: any; b: any; c: any; }
55
>a : any
6-
> : any
76
>b : any
8-
> : any
97
>c : any
108

tests/baselines/reference/objectLiteralWithSemicolons2.errors.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
tests/cases/compiler/objectLiteralWithSemicolons2.ts(2,4): error TS1005: ':' expected.
2-
tests/cases/compiler/objectLiteralWithSemicolons2.ts(3,4): error TS1005: ':' expected.
1+
tests/cases/compiler/objectLiteralWithSemicolons2.ts(2,3): error TS2304: Cannot find name 'a'.
2+
tests/cases/compiler/objectLiteralWithSemicolons2.ts(2,4): error TS1005: ',' expected.
3+
tests/cases/compiler/objectLiteralWithSemicolons2.ts(3,3): error TS2304: Cannot find name 'b'.
4+
tests/cases/compiler/objectLiteralWithSemicolons2.ts(3,4): error TS1005: ',' expected.
35
tests/cases/compiler/objectLiteralWithSemicolons2.ts(4,3): error TS2304: Cannot find name 'c'.
46

57

6-
==== tests/cases/compiler/objectLiteralWithSemicolons2.ts (3 errors) ====
8+
==== tests/cases/compiler/objectLiteralWithSemicolons2.ts (5 errors) ====
79
var v = {
810
a;
11+
~
12+
!!! error TS2304: Cannot find name 'a'.
913
~
10-
!!! error TS1005: ':' expected.
14+
!!! error TS1005: ',' expected.
1115
b;
16+
~
17+
!!! error TS2304: Cannot find name 'b'.
1218
~
13-
!!! error TS1005: ':' expected.
19+
!!! error TS1005: ',' expected.
1420
c
1521
~
1622
!!! error TS2304: Cannot find name 'c'.

tests/baselines/reference/objectLiteralWithSemicolons2.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var v = {
77

88
//// [objectLiteralWithSemicolons2.js]
99
var v = {
10-
a: ,
11-
b: ,
10+
a: a,
11+
b: b,
1212
c: c
1313
};

tests/baselines/reference/objectLiteralWithSemicolons2.types

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ var v = {
55

66
a;
77
>a : any
8-
> : any
98

109
b;
1110
>b : any
12-
> : any
1311

1412
c
1513
>c : any
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
tests/cases/compiler/objectLiteralWithSemicolons3.ts(2,4): error TS1005: ':' expected.
2-
tests/cases/compiler/objectLiteralWithSemicolons3.ts(3,4): error TS1005: ':' expected.
3-
tests/cases/compiler/objectLiteralWithSemicolons3.ts(4,4): error TS1005: ':' expected.
1+
tests/cases/compiler/objectLiteralWithSemicolons3.ts(2,3): error TS2304: Cannot find name 'a'.
2+
tests/cases/compiler/objectLiteralWithSemicolons3.ts(2,4): error TS1005: ',' expected.
3+
tests/cases/compiler/objectLiteralWithSemicolons3.ts(3,3): error TS2304: Cannot find name 'b'.
4+
tests/cases/compiler/objectLiteralWithSemicolons3.ts(3,4): error TS1005: ',' expected.
5+
tests/cases/compiler/objectLiteralWithSemicolons3.ts(4,3): error TS2304: Cannot find name 'c'.
6+
tests/cases/compiler/objectLiteralWithSemicolons3.ts(4,4): error TS1005: ',' expected.
47

58

6-
==== tests/cases/compiler/objectLiteralWithSemicolons3.ts (3 errors) ====
9+
==== tests/cases/compiler/objectLiteralWithSemicolons3.ts (6 errors) ====
710
var v = {
811
a;
12+
~
13+
!!! error TS2304: Cannot find name 'a'.
914
~
10-
!!! error TS1005: ':' expected.
15+
!!! error TS1005: ',' expected.
1116
b;
17+
~
18+
!!! error TS2304: Cannot find name 'b'.
1219
~
13-
!!! error TS1005: ':' expected.
20+
!!! error TS1005: ',' expected.
1421
c;
22+
~
23+
!!! error TS2304: Cannot find name 'c'.
1524
~
16-
!!! error TS1005: ':' expected.
25+
!!! error TS1005: ',' expected.
1726
}

tests/baselines/reference/objectLiteralWithSemicolons3.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var v = {
77

88
//// [objectLiteralWithSemicolons3.js]
99
var v = {
10-
a: ,
11-
b: ,
12-
c:
10+
a: a,
11+
b: b,
12+
c: c
1313
};

tests/baselines/reference/objectLiteralWithSemicolons3.types

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ var v = {
55

66
a;
77
>a : any
8-
> : any
98

109
b;
1110
>b : any
12-
> : any
1311

1412
c;
1513
>c : any
16-
> : any
1714
}

0 commit comments

Comments
 (0)