From f8cacf97e159f4f886eede159f579cf2331256c3 Mon Sep 17 00:00:00 2001 From: Toxyxer Date: Tue, 10 Dec 2019 01:31:23 +0100 Subject: [PATCH] Issue/34870 (#35586) * Add new error message when missing brace * Add test for missing close brace * Update other tests which were affected --- src/compiler/diagnosticMessages.json | 4 ++++ src/compiler/parser.ts | 11 ++++++++++- ...ithDotFollowedByNamespaceKeyword.errors.txt | 4 +++- .../reference/exportInFunction.errors.txt | 3 ++- .../reference/missingCloseBrace.errors.txt | 16 ++++++++++++++++ tests/baselines/reference/missingCloseBrace.js | 18 ++++++++++++++++++ .../reference/missingCloseBrace.symbols | 12 ++++++++++++ .../reference/missingCloseBrace.types | 12 ++++++++++++ ...erErrorRecovery_SwitchStatement2.errors.txt | 3 ++- .../baselines/reference/parserFuzz1.errors.txt | 3 ++- .../prettyContextNotDebugAssertion.errors.txt | 6 ++++++ tests/cases/compiler/missingCloseBrace.ts | 8 ++++++++ 12 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 tests/baselines/reference/missingCloseBrace.errors.txt create mode 100644 tests/baselines/reference/missingCloseBrace.js create mode 100644 tests/baselines/reference/missingCloseBrace.symbols create mode 100644 tests/baselines/reference/missingCloseBrace.types create mode 100644 tests/cases/compiler/missingCloseBrace.ts diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 4fb13c564bc..03a1d3f06c2 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 49cdea63d58..0cda1e4d1d8 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 f22c018e9ea..3a7c89e2f41 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 cfd005952f9..bfd43b23477 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 00000000000..27db81d27da --- /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 00000000000..114dcfa2c24 --- /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 00000000000..7e88528d903 --- /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 00000000000..4cba116ac54 --- /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 86bc6fbfd74..2fca88d8beb 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 e90dda55244..e11d25c48fa 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 008d3d3d817..2bb96b27a9d 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 00000000000..26fee6a813d --- /dev/null +++ b/tests/cases/compiler/missingCloseBrace.ts @@ -0,0 +1,8 @@ +function base_init() { + { + + } + + function me() { + + }