Skip to content

Parse an object literal property as shorthand unless followed by '(' or ':' #28121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4736,8 +4736,7 @@ namespace ts {
// CoverInitializedName[Yield] :
// IdentifierReference[?Yield] Initializer[In, ?Yield]
// this is necessary because ObjectLiteral productions are also used to cover grammar for ObjectAssignmentPattern
const isShorthandPropertyAssignment =
tokenIsIdentifier && (token() === SyntaxKind.CommaToken || token() === SyntaxKind.CloseBraceToken || token() === SyntaxKind.EqualsToken);
const isShorthandPropertyAssignment = tokenIsIdentifier && (token() !== SyntaxKind.ColonToken);
if (isShorthandPropertyAssignment) {
node.kind = SyntaxKind.ShorthandPropertyAssignment;
const equalsToken = parseOptionalToken(SyntaxKind.EqualsToken);
Expand Down
3 changes: 2 additions & 1 deletion src/testRunner/unittests/convertCompilerOptionsFromJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,8 @@ namespace ts {
{
compilerOptions: {
target: undefined,
module: ModuleKind.ESNext
module: ModuleKind.ESNext,
experimentalDecorators: true,
},
hasParseErrors: true
}
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsconfigParsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ namespace ts {

it("returns object with error when json is invalid", () => {
const parsed = parseConfigFileTextToJson("/apath/tsconfig.json", "invalid");
assert.deepEqual(parsed.config, { invalid: undefined });
assert.deepEqual(parsed.config, {});
const expected = createCompilerDiagnostic(Diagnostics._0_expected, "{");
const error = parsed.error!;
assert.equal(error.messageText, expected.messageText);
Expand Down
9 changes: 6 additions & 3 deletions tests/baselines/reference/incompleteObjectLiteral1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
tests/cases/compiler/incompleteObjectLiteral1.ts(1,14): error TS1005: ':' expected.
tests/cases/compiler/incompleteObjectLiteral1.ts(1,12): error TS2304: Cannot find name 'aa'.
tests/cases/compiler/incompleteObjectLiteral1.ts(1,14): error TS1005: ',' expected.


==== tests/cases/compiler/incompleteObjectLiteral1.ts (1 errors) ====
==== tests/cases/compiler/incompleteObjectLiteral1.ts (2 errors) ====
var tt = { aa; }
~~
!!! error TS2304: Cannot find name 'aa'.
~
!!! error TS1005: ':' expected.
!!! error TS1005: ',' expected.
var x = tt;
2 changes: 1 addition & 1 deletion tests/baselines/reference/incompleteObjectLiteral1.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ var tt = { aa; }
var x = tt;

//// [incompleteObjectLiteral1.js]
var tt = { aa: };
var tt = { aa: aa };
var x = tt;
1 change: 0 additions & 1 deletion tests/baselines/reference/incompleteObjectLiteral1.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var tt = { aa; }
>tt : { aa: any; }
>{ aa; } : { aa: any; }
>aa : any
> : any

var x = tt;
>x : { aa: any; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ 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(15,5): error TS2304: Cannot find name 'a'.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(15,6): error TS1005: ',' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,5): error TS2304: Cannot find name 'a'.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,6): error TS1005: ',' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(16,12): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,5): error TS2304: Cannot find name 'a'.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,6): error TS1005: ',' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(17,9): error TS1005: ':' expected.
tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts(20,17): error TS1005: ':' expected.


==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts (13 errors) ====
==== tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts (18 errors) ====
// errors
var y = {
"stringLiteral",
Expand Down Expand Up @@ -47,13 +52,23 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr

var x = {
a.b,
~
!!! error TS2304: Cannot find name 'a'.
~
!!! error TS1005: ':' expected.
!!! error TS1005: ',' expected.
a["ss"],
~
!!! error TS2304: Cannot find name 'a'.
~
!!! error TS1005: ',' expected.
~
!!! error TS1005: ':' expected.
a[1],
~
!!! error TS2304: Cannot find name 'a'.
~
!!! error TS1005: ',' expected.
~
!!! error TS1005: ':' expected.
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var x = {
var v = { class }; // error

//// [objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.js]
var _a;
// errors
var y = {
"stringLiteral": ,
Expand All @@ -33,9 +34,12 @@ var y = {
"class": ,
"typeof":
};
var x = {
a: .b,
a: ["ss"],
a: [1]
};
var x = (_a = {
a: a, : .b,
a: a
},
_a["ss"] = ,
_a.a = a,
_a[1] = ,
_a);
var v = { "class": }; // error
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ var x = {

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

a["ss"],
>a : Symbol(a, Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 13, 9), Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 14, 8), Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 15, 12))
>["ss"] : Symbol(["ss"], Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 15, 5))
>"ss" : Symbol(["ss"], Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 15, 5))

a[1],
>a : Symbol(a, Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 13, 9), Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 14, 8), Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 15, 12))
>[1] : Symbol([1], Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 16, 5))
>1 : Symbol([1], Decl(objectLiteralShorthandPropertiesErrorFromNotUsingIdentifier.ts, 16, 5))

};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,27 @@ var y = {
> : any

var x = {
>x : { a: number[]; }
>{ a.b, a["ss"], a[1],} : { a: number[]; }
>x : { a: any; (Missing): any; ["ss"]: any; [1]: any; }
>{ a.b, a["ss"], a[1],} : { a: any; (Missing): any; ["ss"]: any; [1]: any; }

a.b,
>a : any
> : any
>.b : any
> : any
>b : any

a["ss"],
>a : any
>["ss"] : string[]
>["ss"] : any
>"ss" : "ss"
> : any

a[1],
>a : any
>[1] : number[]
>[1] : any
>1 : 1
> : any

};

Expand Down
Original file line number Diff line number Diff line change
@@ -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'.


Expand All @@ -14,7 +14,7 @@ tests/cases/conformance/es6/shorthandPropertyAssignment/objectLiteralShorthandPr
export var y = {
m.x // error
~
!!! error TS1005: ':' expected.
!!! error TS1005: ',' expected.
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var n;
(function (n) {
var z = 10000;
n.y = {
m: .x // error
m: m, : .x // error
};
})(n || (n = {}));
m.y.x;
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module n {

m.x // error
>m : Symbol(m, Decl(objectLiteralShorthandPropertiesErrorWithModule.ts, 8, 20))
> : Symbol((Missing), Decl(objectLiteralShorthandPropertiesErrorWithModule.ts, 9, 9))

};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ module n {
>10000 : 10000

export var y = {
>y : { m: any; }
>{ m.x // error } : { m: any; }
>y : { m: typeof m; (Missing): any; }
>{ m.x // error } : { m: typeof m; (Missing): any; }

m.x // error
>m : any
>m : typeof m
> : any
>.x : any
> : any
>x : any
Expand Down
16 changes: 11 additions & 5 deletions tests/baselines/reference/objectLiteralWithSemicolons1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,12): error TS1005: ':' expected.
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,15): error TS1005: ':' expected.
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,11): error TS2304: Cannot find name 'a'.
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,12): error TS1005: ',' expected.
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,14): error TS2304: Cannot find name 'b'.
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,15): error TS1005: ',' expected.
tests/cases/compiler/objectLiteralWithSemicolons1.ts(1,17): error TS2304: Cannot find name 'c'.


==== tests/cases/compiler/objectLiteralWithSemicolons1.ts (3 errors) ====
==== tests/cases/compiler/objectLiteralWithSemicolons1.ts (5 errors) ====
var v = { a; b; c }
~
!!! error TS2304: Cannot find name 'a'.
~
!!! error TS1005: ':' expected.
!!! error TS1005: ',' expected.
~
!!! error TS2304: Cannot find name 'b'.
~
!!! error TS1005: ':' expected.
!!! error TS1005: ',' expected.
~
!!! error TS2304: Cannot find name 'c'.
2 changes: 1 addition & 1 deletion tests/baselines/reference/objectLiteralWithSemicolons1.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
var v = { a; b; c }

//// [objectLiteralWithSemicolons1.js]
var v = { a: , b: , c: c };
var v = { a: a, b: b, c: c };
2 changes: 0 additions & 2 deletions tests/baselines/reference/objectLiteralWithSemicolons1.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ var v = { a; b; c }
>v : { a: any; b: any; c: any; }
>{ a; b; c } : { a: any; b: any; c: any; }
>a : any
> : any
>b : any
> : any
>c : any

16 changes: 11 additions & 5 deletions tests/baselines/reference/objectLiteralWithSemicolons2.errors.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
tests/cases/compiler/objectLiteralWithSemicolons2.ts(2,4): error TS1005: ':' expected.
tests/cases/compiler/objectLiteralWithSemicolons2.ts(3,4): error TS1005: ':' expected.
tests/cases/compiler/objectLiteralWithSemicolons2.ts(2,3): error TS2304: Cannot find name 'a'.
tests/cases/compiler/objectLiteralWithSemicolons2.ts(2,4): error TS1005: ',' expected.
tests/cases/compiler/objectLiteralWithSemicolons2.ts(3,3): error TS2304: Cannot find name 'b'.
tests/cases/compiler/objectLiteralWithSemicolons2.ts(3,4): error TS1005: ',' expected.
tests/cases/compiler/objectLiteralWithSemicolons2.ts(4,3): error TS2304: Cannot find name 'c'.


==== tests/cases/compiler/objectLiteralWithSemicolons2.ts (3 errors) ====
==== tests/cases/compiler/objectLiteralWithSemicolons2.ts (5 errors) ====
var v = {
a;
~
!!! error TS2304: Cannot find name 'a'.
~
!!! error TS1005: ':' expected.
!!! error TS1005: ',' expected.
b;
~
!!! error TS2304: Cannot find name 'b'.
~
!!! error TS1005: ':' expected.
!!! error TS1005: ',' expected.
c
~
!!! error TS2304: Cannot find name 'c'.
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/objectLiteralWithSemicolons2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var v = {

//// [objectLiteralWithSemicolons2.js]
var v = {
a: ,
b: ,
a: a,
b: b,
c: c
};
2 changes: 0 additions & 2 deletions tests/baselines/reference/objectLiteralWithSemicolons2.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ var v = {

a;
>a : any
> : any

b;
>b : any
> : any

c
>c : any
Expand Down
23 changes: 16 additions & 7 deletions tests/baselines/reference/objectLiteralWithSemicolons3.errors.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
tests/cases/compiler/objectLiteralWithSemicolons3.ts(2,4): error TS1005: ':' expected.
tests/cases/compiler/objectLiteralWithSemicolons3.ts(3,4): error TS1005: ':' expected.
tests/cases/compiler/objectLiteralWithSemicolons3.ts(4,4): error TS1005: ':' expected.
tests/cases/compiler/objectLiteralWithSemicolons3.ts(2,3): error TS2304: Cannot find name 'a'.
tests/cases/compiler/objectLiteralWithSemicolons3.ts(2,4): error TS1005: ',' expected.
tests/cases/compiler/objectLiteralWithSemicolons3.ts(3,3): error TS2304: Cannot find name 'b'.
tests/cases/compiler/objectLiteralWithSemicolons3.ts(3,4): error TS1005: ',' expected.
tests/cases/compiler/objectLiteralWithSemicolons3.ts(4,3): error TS2304: Cannot find name 'c'.
tests/cases/compiler/objectLiteralWithSemicolons3.ts(4,4): error TS1005: ',' expected.


==== tests/cases/compiler/objectLiteralWithSemicolons3.ts (3 errors) ====
==== tests/cases/compiler/objectLiteralWithSemicolons3.ts (6 errors) ====
var v = {
a;
~
!!! error TS2304: Cannot find name 'a'.
~
!!! error TS1005: ':' expected.
!!! error TS1005: ',' expected.
b;
~
!!! error TS2304: Cannot find name 'b'.
~
!!! error TS1005: ':' expected.
!!! error TS1005: ',' expected.
c;
~
!!! error TS2304: Cannot find name 'c'.
~
!!! error TS1005: ':' expected.
!!! error TS1005: ',' expected.
}
6 changes: 3 additions & 3 deletions tests/baselines/reference/objectLiteralWithSemicolons3.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var v = {

//// [objectLiteralWithSemicolons3.js]
var v = {
a: ,
b: ,
c:
a: a,
b: b,
c: c
};
3 changes: 0 additions & 3 deletions tests/baselines/reference/objectLiteralWithSemicolons3.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ var v = {

a;
>a : any
> : any

b;
>b : any
> : any

c;
>c : any
> : any
}
Loading