Skip to content

Commit b014e2b

Browse files
authored
Show token hints for missing closing braces (microsoft#36317)
* Add error for missing brace in object literal * Add new baseline test * Update all affected tests
1 parent c6cfd66 commit b014e2b

14 files changed

+82
-6
lines changed

Diff for: src/compiler/parser.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -5110,13 +5110,22 @@ namespace ts {
51105110

51115111
function parseObjectLiteralExpression(): ObjectLiteralExpression {
51125112
const node = <ObjectLiteralExpression>createNode(SyntaxKind.ObjectLiteralExpression);
5113+
const openBracePosition = scanner.getTokenPos();
51135114
parseExpected(SyntaxKind.OpenBraceToken);
51145115
if (scanner.hasPrecedingLineBreak()) {
51155116
node.multiLine = true;
51165117
}
51175118

51185119
node.properties = parseDelimitedList(ParsingContext.ObjectLiteralMembers, parseObjectLiteralElement, /*considerSemicolonAsDelimiter*/ true);
5119-
parseExpected(SyntaxKind.CloseBraceToken);
5120+
if (!parseExpected(SyntaxKind.CloseBraceToken)) {
5121+
const lastError = lastOrUndefined(parseDiagnostics);
5122+
if (lastError && lastError.code === Diagnostics._0_expected.code) {
5123+
addRelatedInfo(
5124+
lastError,
5125+
createFileDiagnostic(sourceFile, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_to_match_the_token_here)
5126+
);
5127+
}
5128+
}
51205129
return finishNode(node);
51215130
}
51225131

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/compiler/missingCloseBraceInObjectLiteral.ts(5,1): error TS1005: '}' expected.
2+
3+
4+
==== tests/cases/compiler/missingCloseBraceInObjectLiteral.ts (1 errors) ====
5+
var foo = {
6+
a: 'a',
7+
b: 'b',
8+
c: 'c'
9+
10+
11+
!!! error TS1005: '}' expected.
12+
!!! related TS1007 tests/cases/compiler/missingCloseBraceInObjectLiteral.ts:1:11: The parser expected to find a '}' to match the '{' token here.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [missingCloseBraceInObjectLiteral.ts]
2+
var foo = {
3+
a: 'a',
4+
b: 'b',
5+
c: 'c'
6+
7+
8+
//// [missingCloseBraceInObjectLiteral.js]
9+
var foo = {
10+
a: 'a',
11+
b: 'b',
12+
c: 'c'
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/missingCloseBraceInObjectLiteral.ts ===
2+
var foo = {
3+
>foo : Symbol(foo, Decl(missingCloseBraceInObjectLiteral.ts, 0, 3))
4+
5+
a: 'a',
6+
>a : Symbol(a, Decl(missingCloseBraceInObjectLiteral.ts, 0, 11))
7+
8+
b: 'b',
9+
>b : Symbol(b, Decl(missingCloseBraceInObjectLiteral.ts, 1, 11))
10+
11+
c: 'c'
12+
>c : Symbol(c, Decl(missingCloseBraceInObjectLiteral.ts, 2, 11))
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/missingCloseBraceInObjectLiteral.ts ===
2+
var foo = {
3+
>foo : { a: string; b: string; c: string; }
4+
>{ a: 'a', b: 'b', c: 'c' : { a: string; b: string; c: string; }
5+
6+
a: 'a',
7+
>a : string
8+
>'a' : "a"
9+
10+
b: 'b',
11+
>b : string
12+
>'b' : "b"
13+
14+
c: 'c'
15+
>c : string
16+
>'c' : "c"
17+

Diff for: tests/baselines/reference/nestedClassDeclaration.errors.txt

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tests/cases/conformance/classes/nestedClassDeclaration.ts(17,1): error TS1128: D
3232
!!! error TS2304: Cannot find name 'C4'.
3333
~
3434
!!! error TS1005: ',' expected.
35+
!!! related TS1007 tests/cases/conformance/classes/nestedClassDeclaration.ts:14:9: The parser expected to find a '}' to match the '{' token here.
3536
}
3637
}
3738
~

Diff for: tests/baselines/reference/objectLiteralWithSemicolons4.errors.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ tests/cases/compiler/objectLiteralWithSemicolons4.ts(3,1): error TS1005: ',' exp
99
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
1010
;
1111
~
12-
!!! error TS1005: ',' expected.
12+
!!! error TS1005: ',' expected.
13+
!!! related TS1007 tests/cases/compiler/objectLiteralWithSemicolons4.ts:1:9: The parser expected to find a '}' to match the '{' token here.

Diff for: tests/baselines/reference/objectSpreadNegativeParse.errors.txt

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ tests/cases/conformance/types/spread/objectSpreadNegativeParse.ts(4,20): error T
2828
!!! error TS2304: Cannot find name 'matchMedia'.
2929
~
3030
!!! error TS1005: ',' expected.
31+
!!! related TS1007 tests/cases/conformance/types/spread/objectSpreadNegativeParse.ts:3:10: The parser expected to find a '}' to match the '{' token here.
3132
~
3233
!!! error TS1128: Declaration or statement expected.
3334
let o10 = { ...get x() { return 12; }};

Diff for: tests/baselines/reference/parseErrorIncorrectReturnToken.errors.txt

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ tests/cases/compiler/parseErrorIncorrectReturnToken.ts(12,1): error TS1128: Decl
2525
m(n: number) => string {
2626
~~
2727
!!! error TS1005: '{' expected.
28+
!!! related TS1007 tests/cases/compiler/parseErrorIncorrectReturnToken.ts:8:9: The parser expected to find a '}' to match the '{' token here.
2829
~~~~~~
2930
!!! error TS2693: 'string' only refers to a type, but is being used as a value here.
3031
~

Diff for: tests/baselines/reference/parserErrorRecovery_ObjectLiteral2.errors.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserEr
1414
~
1515
!!! error TS1005: ':' expected.
1616

17-
!!! error TS1005: '}' expected.
17+
!!! error TS1005: '}' expected.
18+
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts:1:9: The parser expected to find a '}' to match the '{' token here.

Diff for: tests/baselines/reference/parserErrorRecovery_ObjectLiteral3.errors.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserEr
1111
~
1212
!!! error TS1005: ':' expected.
1313

14-
!!! error TS1005: '}' expected.
14+
!!! error TS1005: '}' expected.
15+
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral3.ts:1:9: The parser expected to find a '}' to match the '{' token here.

Diff for: tests/baselines/reference/parserErrorRecovery_ObjectLiteral4.errors.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserEr
1111
~
1212
!!! error TS1005: ':' expected.
1313

14-
!!! error TS1005: '}' expected.
14+
!!! error TS1005: '}' expected.
15+
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral4.ts:1:9: The parser expected to find a '}' to match the '{' token here.

Diff for: tests/baselines/reference/parserErrorRecovery_ObjectLiteral5.errors.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserEr
88
~
99
!!! error TS1005: ':' expected.
1010

11-
!!! error TS1005: '}' expected.
11+
!!! error TS1005: '}' expected.
12+
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral5.ts:1:9: The parser expected to find a '}' to match the '{' token here.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var foo = {
2+
a: 'a',
3+
b: 'b',
4+
c: 'c'

0 commit comments

Comments
 (0)