From 70e8a609eef4410ca41227180f239d9ecf495b49 Mon Sep 17 00:00:00 2001 From: Jesse Trinity Date: Tue, 18 May 2021 20:42:33 -0700 Subject: [PATCH] don't try to add related diagnostic for closing bracket when parser can't find opening bracket --- src/compiler/parser.ts | 20 +++++++++++-------- .../reference/reservedWords2.errors.txt | 2 -- .../smartIndentMissingBracketsDoKeyword.ts | 7 +++++++ .../smartIndentMissingBracketsIfKeyword.ts | 7 +++++++ .../smartIndentMissingBracketsWhileKeyword.ts | 7 +++++++ .../smartIndentMissingBracketsWithKeyword.ts | 7 +++++++ 6 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 tests/cases/fourslash/smartIndentMissingBracketsDoKeyword.ts create mode 100644 tests/cases/fourslash/smartIndentMissingBracketsIfKeyword.ts create mode 100644 tests/cases/fourslash/smartIndentMissingBracketsWhileKeyword.ts create mode 100644 tests/cases/fourslash/smartIndentMissingBracketsWithKeyword.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 6c0ee33b57e76..bf273a9c768d7 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -5666,9 +5666,10 @@ namespace ts { const hasJSDoc = hasPrecedingJSDocComment(); parseExpected(SyntaxKind.IfKeyword); const openParenPosition = scanner.getTokenPos(); - parseExpected(SyntaxKind.OpenParenToken); + const openParenExists = parseExpected(SyntaxKind.OpenParenToken); const expression = allowInAnd(parseExpression); - parseExpectedMatchingBrackets(SyntaxKind.OpenParenToken, SyntaxKind.CloseParenToken, openParenPosition); + openParenExists ? parseExpectedMatchingBrackets(SyntaxKind.OpenParenToken, SyntaxKind.CloseParenToken, openParenPosition) + : parseExpected(SyntaxKind.CloseBraceToken); const thenStatement = parseStatement(); const elseStatement = parseOptional(SyntaxKind.ElseKeyword) ? parseStatement() : undefined; return withJSDoc(finishNode(factory.createIfStatement(expression, thenStatement, elseStatement), pos), hasJSDoc); @@ -5681,9 +5682,10 @@ namespace ts { const statement = parseStatement(); parseExpected(SyntaxKind.WhileKeyword); const openParenPosition = scanner.getTokenPos(); - parseExpected(SyntaxKind.OpenParenToken); + const openParenExists = parseExpected(SyntaxKind.OpenParenToken); const expression = allowInAnd(parseExpression); - parseExpectedMatchingBrackets(SyntaxKind.OpenParenToken, SyntaxKind.CloseParenToken, openParenPosition); + openParenExists ? parseExpectedMatchingBrackets(SyntaxKind.OpenParenToken, SyntaxKind.CloseParenToken, openParenPosition) + : parseExpected(SyntaxKind.CloseParenToken); // From: https://mail.mozilla.org/pipermail/es-discuss/2011-August/016188.html // 157 min --- All allen at wirfs-brock.com CONF --- "do{;}while(false)false" prohibited in @@ -5698,9 +5700,10 @@ namespace ts { const hasJSDoc = hasPrecedingJSDocComment(); parseExpected(SyntaxKind.WhileKeyword); const openParenPosition = scanner.getTokenPos(); - parseExpected(SyntaxKind.OpenParenToken); + const openParenExists = parseExpected(SyntaxKind.OpenParenToken); const expression = allowInAnd(parseExpression); - parseExpectedMatchingBrackets(SyntaxKind.OpenParenToken, SyntaxKind.CloseParenToken, openParenPosition); + openParenExists ? parseExpectedMatchingBrackets(SyntaxKind.OpenParenToken, SyntaxKind.CloseParenToken, openParenPosition) + : parseExpected(SyntaxKind.CloseParenToken); const statement = parseStatement(); return withJSDoc(finishNode(factory.createWhileStatement(expression, statement), pos), hasJSDoc); } @@ -5777,9 +5780,10 @@ namespace ts { const hasJSDoc = hasPrecedingJSDocComment(); parseExpected(SyntaxKind.WithKeyword); const openParenPosition = scanner.getTokenPos(); - parseExpected(SyntaxKind.OpenParenToken); + const openParenExists = parseExpected(SyntaxKind.OpenParenToken); const expression = allowInAnd(parseExpression); - parseExpectedMatchingBrackets(SyntaxKind.OpenParenToken, SyntaxKind.CloseParenToken, openParenPosition); + openParenExists ? parseExpectedMatchingBrackets(SyntaxKind.OpenParenToken, SyntaxKind.CloseParenToken, openParenPosition) + : parseExpected(SyntaxKind.CloseParenToken); const statement = doInsideOfContext(NodeFlags.InWithStatement, parseStatement); return withJSDoc(finishNode(factory.createWithStatement(expression, statement), pos), hasJSDoc); } diff --git a/tests/baselines/reference/reservedWords2.errors.txt b/tests/baselines/reference/reservedWords2.errors.txt index 6ecb9471631bd..599010b436872 100644 --- a/tests/baselines/reference/reservedWords2.errors.txt +++ b/tests/baselines/reference/reservedWords2.errors.txt @@ -45,7 +45,6 @@ tests/cases/compiler/reservedWords2.ts(12,17): error TS1138: Parameter declarati !!! error TS2580: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`. ~ !!! error TS1005: ')' expected. -!!! related TS1007 tests/cases/compiler/reservedWords2.ts:1:14: The parser expected to find a ')' to match the '(' token here. import * as while from "foo" !!! error TS2300: Duplicate identifier '(Missing)'. @@ -59,7 +58,6 @@ tests/cases/compiler/reservedWords2.ts(12,17): error TS1138: Parameter declarati !!! error TS2304: Cannot find name 'from'. ~~~~~ !!! error TS1005: ')' expected. -!!! related TS1007 tests/cases/compiler/reservedWords2.ts:2:20: The parser expected to find a ')' to match the '(' token here. var typeof = 10; ~~~~~~ diff --git a/tests/cases/fourslash/smartIndentMissingBracketsDoKeyword.ts b/tests/cases/fourslash/smartIndentMissingBracketsDoKeyword.ts new file mode 100644 index 0000000000000..984499db5ae5b --- /dev/null +++ b/tests/cases/fourslash/smartIndentMissingBracketsDoKeyword.ts @@ -0,0 +1,7 @@ +/// + +////do {/*1*/ + +goTo.marker("1"); +edit.insert("\n"); +verify.indentationIs(4); diff --git a/tests/cases/fourslash/smartIndentMissingBracketsIfKeyword.ts b/tests/cases/fourslash/smartIndentMissingBracketsIfKeyword.ts new file mode 100644 index 0000000000000..0e260cdf1427e --- /dev/null +++ b/tests/cases/fourslash/smartIndentMissingBracketsIfKeyword.ts @@ -0,0 +1,7 @@ +/// + +////if /*1*/ + +goTo.marker("1"); +edit.insert("\n"); +verify.indentationIs(4); diff --git a/tests/cases/fourslash/smartIndentMissingBracketsWhileKeyword.ts b/tests/cases/fourslash/smartIndentMissingBracketsWhileKeyword.ts new file mode 100644 index 0000000000000..03e9be630ca3c --- /dev/null +++ b/tests/cases/fourslash/smartIndentMissingBracketsWhileKeyword.ts @@ -0,0 +1,7 @@ +/// + +////while /*1*/ + +goTo.marker("1"); +edit.insert("\n"); +verify.indentationIs(4); diff --git a/tests/cases/fourslash/smartIndentMissingBracketsWithKeyword.ts b/tests/cases/fourslash/smartIndentMissingBracketsWithKeyword.ts new file mode 100644 index 0000000000000..733fc0560a7c3 --- /dev/null +++ b/tests/cases/fourslash/smartIndentMissingBracketsWithKeyword.ts @@ -0,0 +1,7 @@ +/// + +////with /*1*/ + +goTo.marker("1"); +edit.insert("\n"); +verify.indentationIs(0);