Skip to content

Commit

Permalink
don't try to add related diagnostic for closing bracket when parser c…
Browse files Browse the repository at this point in the history
…an't find opening bracket
  • Loading branch information
jessetrinity committed May 19, 2021
1 parent 3f9e724 commit 70e8a60
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/compiler/parser.ts
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 0 additions & 2 deletions tests/baselines/reference/reservedWords2.errors.txt
Expand Up @@ -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)'.
Expand All @@ -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;
~~~~~~
Expand Down
7 changes: 7 additions & 0 deletions tests/cases/fourslash/smartIndentMissingBracketsDoKeyword.ts
@@ -0,0 +1,7 @@
/// <reference path='fourslash.ts'/>

////do {/*1*/

goTo.marker("1");
edit.insert("\n");
verify.indentationIs(4);
7 changes: 7 additions & 0 deletions tests/cases/fourslash/smartIndentMissingBracketsIfKeyword.ts
@@ -0,0 +1,7 @@
/// <reference path='fourslash.ts'/>

////if /*1*/

goTo.marker("1");
edit.insert("\n");
verify.indentationIs(4);
@@ -0,0 +1,7 @@
/// <reference path='fourslash.ts'/>

////while /*1*/

goTo.marker("1");
edit.insert("\n");
verify.indentationIs(4);
@@ -0,0 +1,7 @@
/// <reference path='fourslash.ts'/>

////with /*1*/

goTo.marker("1");
edit.insert("\n");
verify.indentationIs(0);

0 comments on commit 70e8a60

Please sign in to comment.