diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 4fb13c564b..03a1d3f06c 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -15,6 +15,10 @@ "category": "Error", "code": 1006 }, + "The parser expected to find a '}' to match the '{' token here.": { + "category": "Error", + "code": 1007 + }, "Trailing comma not allowed.": { "category": "Error", "code": 1009 diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 49cdea63d5..0cda1e4d1d 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5059,13 +5059,22 @@ namespace ts { // STATEMENTS function parseBlock(ignoreMissingOpenBrace: boolean, diagnosticMessage?: DiagnosticMessage): Block { const node = createNode(SyntaxKind.Block); + const openBracePosition = scanner.getTokenPos(); if (parseExpected(SyntaxKind.OpenBraceToken, diagnosticMessage) || ignoreMissingOpenBrace) { if (scanner.hasPrecedingLineBreak()) { node.multiLine = true; } node.statements = parseList(ParsingContext.BlockStatements, parseStatement); - parseExpected(SyntaxKind.CloseBraceToken); + if (!parseExpected(SyntaxKind.CloseBraceToken)) { + const lastError = lastOrUndefined(parseDiagnostics); + if (lastError && lastError.code === Diagnostics._0_expected.code) { + addRelatedInfo( + lastError, + createFileDiagnostic(sourceFile, openBracePosition, 1, Diagnostics.The_parser_expected_to_find_a_to_match_the_token_here) + ); + } + } } else { node.statements = createMissingList(); diff --git a/tests/baselines/reference/errorRecoveryWithDotFollowedByNamespaceKeyword.errors.txt b/tests/baselines/reference/errorRecoveryWithDotFollowedByNamespaceKeyword.errors.txt index f22c018e9e..3a7c89e2f4 100644 --- a/tests/baselines/reference/errorRecoveryWithDotFollowedByNamespaceKeyword.errors.txt +++ b/tests/baselines/reference/errorRecoveryWithDotFollowedByNamespaceKeyword.errors.txt @@ -15,4 +15,6 @@ tests/cases/compiler/errorRecoveryWithDotFollowedByNamespaceKeyword.ts(9,2): err export function baz() { } } -!!! error TS1005: '}' expected. \ No newline at end of file +!!! error TS1005: '}' expected. +!!! related TS1007 tests/cases/compiler/errorRecoveryWithDotFollowedByNamespaceKeyword.ts:3:19: The parser expected to find a '}' to match the '{' token here. +!!! related TS1007 tests/cases/compiler/errorRecoveryWithDotFollowedByNamespaceKeyword.ts:2:20: The parser expected to find a '}' to match the '{' token here. \ No newline at end of file diff --git a/tests/baselines/reference/exportInFunction.errors.txt b/tests/baselines/reference/exportInFunction.errors.txt index cfd005952f..bfd43b2347 100644 --- a/tests/baselines/reference/exportInFunction.errors.txt +++ b/tests/baselines/reference/exportInFunction.errors.txt @@ -6,4 +6,5 @@ tests/cases/compiler/exportInFunction.ts(3,1): error TS1005: '}' expected. export = 0; -!!! error TS1005: '}' expected. \ No newline at end of file +!!! error TS1005: '}' expected. +!!! related TS1007 tests/cases/compiler/exportInFunction.ts:1:14: The parser expected to find a '}' to match the '{' token here. \ No newline at end of file diff --git a/tests/baselines/reference/missingCloseBrace.errors.txt b/tests/baselines/reference/missingCloseBrace.errors.txt new file mode 100644 index 0000000000..27db81d27d --- /dev/null +++ b/tests/baselines/reference/missingCloseBrace.errors.txt @@ -0,0 +1,16 @@ +tests/cases/compiler/missingCloseBrace.ts(9,1): error TS1005: '}' expected. + + +==== tests/cases/compiler/missingCloseBrace.ts (1 errors) ==== + function base_init() { + { + + } + + function me() { + + } + + +!!! error TS1005: '}' expected. +!!! related TS1007 tests/cases/compiler/missingCloseBrace.ts:1:22: The parser expected to find a '}' to match the '{' token here. \ No newline at end of file diff --git a/tests/baselines/reference/missingCloseBrace.js b/tests/baselines/reference/missingCloseBrace.js new file mode 100644 index 0000000000..114dcfa2c2 --- /dev/null +++ b/tests/baselines/reference/missingCloseBrace.js @@ -0,0 +1,18 @@ +//// [missingCloseBrace.ts] +function base_init() { + { + + } + + function me() { + + } + + +//// [missingCloseBrace.js] +function base_init() { + { + } + function me() { + } +} diff --git a/tests/baselines/reference/missingCloseBrace.symbols b/tests/baselines/reference/missingCloseBrace.symbols new file mode 100644 index 0000000000..7e88528d90 --- /dev/null +++ b/tests/baselines/reference/missingCloseBrace.symbols @@ -0,0 +1,12 @@ +=== tests/cases/compiler/missingCloseBrace.ts === +function base_init() { +>base_init : Symbol(base_init, Decl(missingCloseBrace.ts, 0, 0)) + { + + } + + function me() { +>me : Symbol(me, Decl(missingCloseBrace.ts, 3, 5)) + + } + diff --git a/tests/baselines/reference/missingCloseBrace.types b/tests/baselines/reference/missingCloseBrace.types new file mode 100644 index 0000000000..4cba116ac5 --- /dev/null +++ b/tests/baselines/reference/missingCloseBrace.types @@ -0,0 +1,12 @@ +=== tests/cases/compiler/missingCloseBrace.ts === +function base_init() { +>base_init : () => void + { + + } + + function me() { +>me : () => void + + } + diff --git a/tests/baselines/reference/parserErrorRecovery_SwitchStatement2.errors.txt b/tests/baselines/reference/parserErrorRecovery_SwitchStatement2.errors.txt index 86bc6fbfd7..2fca88d8be 100644 --- a/tests/baselines/reference/parserErrorRecovery_SwitchStatement2.errors.txt +++ b/tests/baselines/reference/parserErrorRecovery_SwitchStatement2.errors.txt @@ -15,4 +15,5 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/SwitchStatements/parser !!! error TS1130: 'case' or 'default' expected. } -!!! error TS1005: '}' expected. \ No newline at end of file +!!! error TS1005: '}' expected. +!!! 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. \ No newline at end of file diff --git a/tests/baselines/reference/parserFuzz1.errors.txt b/tests/baselines/reference/parserFuzz1.errors.txt index e90dda5524..e11d25c48f 100644 --- a/tests/baselines/reference/parserFuzz1.errors.txt +++ b/tests/baselines/reference/parserFuzz1.errors.txt @@ -20,4 +20,5 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserFuzz1.ts(2,15): e ~~~~~~ !!! error TS1005: ';' expected. -!!! error TS1005: '{' expected. \ No newline at end of file +!!! error TS1005: '{' expected. +!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/parserFuzz1.ts:1:9: The parser expected to find a '}' to match the '{' token here. \ No newline at end of file diff --git a/tests/baselines/reference/prettyContextNotDebugAssertion.errors.txt b/tests/baselines/reference/prettyContextNotDebugAssertion.errors.txt index 008d3d3d81..2bb96b27a9 100644 --- a/tests/baselines/reference/prettyContextNotDebugAssertion.errors.txt +++ b/tests/baselines/reference/prettyContextNotDebugAssertion.errors.txt @@ -3,11 +3,17 @@ 2    + tests/cases/compiler/index.ts:1:11 + 1 if (true) { +    ~ + The parser expected to find a '}' to match the '{' token here. + ==== tests/cases/compiler/index.ts (1 errors) ==== if (true) { !!! error TS1005: '}' expected. +!!! related TS1007 tests/cases/compiler/index.ts:1:11: The parser expected to find a '}' to match the '{' token here. Found 1 error. diff --git a/tests/cases/compiler/missingCloseBrace.ts b/tests/cases/compiler/missingCloseBrace.ts new file mode 100644 index 0000000000..26fee6a813 --- /dev/null +++ b/tests/cases/compiler/missingCloseBrace.ts @@ -0,0 +1,8 @@ +function base_init() { + { + + } + + function me() { + + }