Skip to content

Commit f8cacf9

Browse files
marcin-serwinDanielRosenwasser
authored andcommitted
Issue/34870 (#35586)
* Add new error message when missing brace * Add test for missing close brace * Update other tests which were affected
1 parent 64a8867 commit f8cacf9

12 files changed

+95
-5
lines changed

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
"category": "Error",
1616
"code": 1006
1717
},
18+
"The parser expected to find a '}' to match the '{' token here.": {
19+
"category": "Error",
20+
"code": 1007
21+
},
1822
"Trailing comma not allowed.": {
1923
"category": "Error",
2024
"code": 1009

src/compiler/parser.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5059,13 +5059,22 @@ namespace ts {
50595059
// STATEMENTS
50605060
function parseBlock(ignoreMissingOpenBrace: boolean, diagnosticMessage?: DiagnosticMessage): Block {
50615061
const node = <Block>createNode(SyntaxKind.Block);
5062+
const openBracePosition = scanner.getTokenPos();
50625063
if (parseExpected(SyntaxKind.OpenBraceToken, diagnosticMessage) || ignoreMissingOpenBrace) {
50635064
if (scanner.hasPrecedingLineBreak()) {
50645065
node.multiLine = true;
50655066
}
50665067

50675068
node.statements = parseList(ParsingContext.BlockStatements, parseStatement);
5068-
parseExpected(SyntaxKind.CloseBraceToken);
5069+
if (!parseExpected(SyntaxKind.CloseBraceToken)) {
5070+
const lastError = lastOrUndefined(parseDiagnostics);
5071+
if (lastError && lastError.code === Diagnostics._0_expected.code) {
5072+
addRelatedInfo(
5073+
lastError,
5074+
createFileDiagnostic(sourceFile, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_to_match_the_token_here)
5075+
);
5076+
}
5077+
}
50695078
}
50705079
else {
50715080
node.statements = createMissingList<Statement>();

tests/baselines/reference/errorRecoveryWithDotFollowedByNamespaceKeyword.errors.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@ tests/cases/compiler/errorRecoveryWithDotFollowedByNamespaceKeyword.ts(9,2): err
1515
export function baz() { }
1616
}
1717

18-
!!! error TS1005: '}' expected.
18+
!!! error TS1005: '}' expected.
19+
!!! related TS1007 tests/cases/compiler/errorRecoveryWithDotFollowedByNamespaceKeyword.ts:3:19: The parser expected to find a '}' to match the '{' token here.
20+
!!! related TS1007 tests/cases/compiler/errorRecoveryWithDotFollowedByNamespaceKeyword.ts:2:20: The parser expected to find a '}' to match the '{' token here.

tests/baselines/reference/exportInFunction.errors.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ tests/cases/compiler/exportInFunction.ts(3,1): error TS1005: '}' expected.
66
export = 0;
77

88

9-
!!! error TS1005: '}' expected.
9+
!!! error TS1005: '}' expected.
10+
!!! related TS1007 tests/cases/compiler/exportInFunction.ts:1:14: The parser expected to find a '}' to match the '{' token here.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
tests/cases/compiler/missingCloseBrace.ts(9,1): error TS1005: '}' expected.
2+
3+
4+
==== tests/cases/compiler/missingCloseBrace.ts (1 errors) ====
5+
function base_init() {
6+
{
7+
8+
}
9+
10+
function me() {
11+
12+
}
13+
14+
15+
!!! error TS1005: '}' expected.
16+
!!! related TS1007 tests/cases/compiler/missingCloseBrace.ts:1:22: The parser expected to find a '}' to match the '{' token here.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [missingCloseBrace.ts]
2+
function base_init() {
3+
{
4+
5+
}
6+
7+
function me() {
8+
9+
}
10+
11+
12+
//// [missingCloseBrace.js]
13+
function base_init() {
14+
{
15+
}
16+
function me() {
17+
}
18+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/missingCloseBrace.ts ===
2+
function base_init() {
3+
>base_init : Symbol(base_init, Decl(missingCloseBrace.ts, 0, 0))
4+
{
5+
6+
}
7+
8+
function me() {
9+
>me : Symbol(me, Decl(missingCloseBrace.ts, 3, 5))
10+
11+
}
12+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/compiler/missingCloseBrace.ts ===
2+
function base_init() {
3+
>base_init : () => void
4+
{
5+
6+
}
7+
8+
function me() {
9+
>me : () => void
10+
11+
}
12+

tests/baselines/reference/parserErrorRecovery_SwitchStatement2.errors.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/SwitchStatements/parser
1515
!!! error TS1130: 'case' or 'default' expected.
1616
}
1717

18-
!!! error TS1005: '}' expected.
18+
!!! error TS1005: '}' expected.
19+
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/SwitchStatements/parserErrorRecovery_SwitchStatement2.ts:2:17: The parser expected to find a '}' to match the '{' token here.

tests/baselines/reference/parserFuzz1.errors.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserFuzz1.ts(2,15): e
2020
~~~~~~
2121
!!! error TS1005: ';' expected.
2222

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

tests/baselines/reference/prettyContextNotDebugAssertion.errors.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
2
44
  
55

6+
tests/cases/compiler/index.ts:1:11
7+
1 if (true) {
8+
   ~
9+
The parser expected to find a '}' to match the '{' token here.
10+
611

712
==== tests/cases/compiler/index.ts (1 errors) ====
813
if (true) {
914

1015

1116
!!! error TS1005: '}' expected.
17+
!!! related TS1007 tests/cases/compiler/index.ts:1:11: The parser expected to find a '}' to match the '{' token here.
1218
Found 1 error.
1319

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function base_init() {
2+
{
3+
4+
}
5+
6+
function me() {
7+
8+
}

0 commit comments

Comments
 (0)