Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parser regression for bad related diagnostic on missing matching brackets #44158

Merged
merged 8 commits into from Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/compiler/parser.ts
Expand Up @@ -5501,11 +5501,10 @@ namespace ts {

function parseArrayLiteralExpression(): ArrayLiteralExpression {
const pos = getNodePos();
const openBracketPosition = scanner.getTokenPos();
const openBracketParsed = parseExpected(SyntaxKind.OpenBracketToken);
const multiLine = scanner.hasPrecedingLineBreak();
const elements = parseDelimitedList(ParsingContext.ArrayLiteralMembers, parseArgumentOrArrayLiteralElement);
parseExpectedMatchingBrackets(SyntaxKind.OpenBracketToken, SyntaxKind.CloseBracketToken, openBracketParsed, openBracketPosition);
parseExpectedMatchingBrackets(SyntaxKind.OpenBracketToken, SyntaxKind.CloseBracketToken, openBracketParsed, pos);
return finishNode(factory.createArrayLiteralExpression(elements, multiLine), pos);
}

Expand Down Expand Up @@ -5570,11 +5569,10 @@ namespace ts {

function parseObjectLiteralExpression(): ObjectLiteralExpression {
const pos = getNodePos();
const openBracePosition = scanner.getTokenPos();
const openBraceParsed = parseExpected(SyntaxKind.OpenBraceToken);
const multiLine = scanner.hasPrecedingLineBreak();
const properties = parseDelimitedList(ParsingContext.ObjectLiteralMembers, parseObjectLiteralElement, /*considerSemicolonAsDelimiter*/ true);
parseExpectedMatchingBrackets(SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, openBraceParsed, openBracePosition);
parseExpectedMatchingBrackets(SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, openBraceParsed, pos);
return finishNode(factory.createObjectLiteralExpression(properties, multiLine), pos);
}

Expand Down Expand Up @@ -5656,12 +5654,11 @@ namespace ts {
function parseBlock(ignoreMissingOpenBrace: boolean, diagnosticMessage?: DiagnosticMessage): Block {
const pos = getNodePos();
const hasJSDoc = hasPrecedingJSDocComment();
const openBracePosition = scanner.getTokenPos();
const openBraceParsed = parseExpected(SyntaxKind.OpenBraceToken, diagnosticMessage);
if (openBraceParsed || ignoreMissingOpenBrace) {
const multiLine = scanner.hasPrecedingLineBreak();
const statements = parseList(ParsingContext.BlockStatements, parseStatement);
parseExpectedMatchingBrackets(SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, openBraceParsed, openBracePosition);
parseExpectedMatchingBrackets(SyntaxKind.OpenBraceToken, SyntaxKind.CloseBraceToken, openBraceParsed, pos);
const result = withJSDoc(finishNode(factory.createBlock(statements, multiLine), pos), hasJSDoc);
if (token() === SyntaxKind.EqualsToken) {
parseErrorAtCurrentToken(Diagnostics.Declaration_or_statement_expected_This_follows_a_block_of_statements_so_if_you_intended_to_write_a_destructuring_assignment_you_might_need_to_wrap_the_the_whole_assignment_in_parentheses);
Expand Down Expand Up @@ -5717,7 +5714,7 @@ namespace ts {
const pos = getNodePos();
const hasJSDoc = hasPrecedingJSDocComment();
parseExpected(SyntaxKind.IfKeyword);
const openParenPosition = scanner.getTokenPos();
const openParenPosition = getNodePos();
const openParenParsed = parseExpected(SyntaxKind.OpenParenToken);
const expression = allowInAnd(parseExpression);
parseExpectedMatchingBrackets(SyntaxKind.OpenParenToken, SyntaxKind.CloseParenToken, openParenParsed, openParenPosition);
Expand All @@ -5732,7 +5729,7 @@ namespace ts {
parseExpected(SyntaxKind.DoKeyword);
const statement = parseStatement();
parseExpected(SyntaxKind.WhileKeyword);
const openParenPosition = scanner.getTokenPos();
const openParenPosition = getNodePos();
const openParenParsed = parseExpected(SyntaxKind.OpenParenToken);
const expression = allowInAnd(parseExpression);
parseExpectedMatchingBrackets(SyntaxKind.OpenParenToken, SyntaxKind.CloseParenToken, openParenParsed, openParenPosition);
Expand All @@ -5749,7 +5746,7 @@ namespace ts {
const pos = getNodePos();
const hasJSDoc = hasPrecedingJSDocComment();
parseExpected(SyntaxKind.WhileKeyword);
const openParenPosition = scanner.getTokenPos();
const openParenPosition = getNodePos();
const openParenParsed = parseExpected(SyntaxKind.OpenParenToken);
const expression = allowInAnd(parseExpression);
parseExpectedMatchingBrackets(SyntaxKind.OpenParenToken, SyntaxKind.CloseParenToken, openParenParsed, openParenPosition);
Expand Down Expand Up @@ -5828,7 +5825,7 @@ namespace ts {
const pos = getNodePos();
const hasJSDoc = hasPrecedingJSDocComment();
parseExpected(SyntaxKind.WithKeyword);
const openParenPosition = scanner.getTokenPos();
const openParenPosition = getNodePos();
const openParenParsed = parseExpected(SyntaxKind.OpenParenToken);
const expression = allowInAnd(parseExpression);
parseExpectedMatchingBrackets(SyntaxKind.OpenParenToken, SyntaxKind.CloseParenToken, openParenParsed, openParenPosition);
Expand Down
Expand Up @@ -121,7 +121,7 @@ tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts(261,1): error TS
if (retValue != 0 ^= {
~~
!!! error TS1005: ')' expected.
!!! related TS1007 tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts:22:20: The parser expected to find a ')' to match the '(' token here.
!!! related TS1007 tests/cases/compiler/constructorWithIncompleteTypeAnnotation.ts:22:19: The parser expected to find a ')' to match the '(' token here.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh-oh, I didn't expect any baseline changes. I counted the spaces and 19 is correct counting from 0; 20 is correct counting from 1. I'm not sure which we do for chars, but I see lines counting from 1 in tests/baselines/reference/jsonParserRecovery/TypeScript_code.errors.txt

I think this change is wrong. Sorry for steering you wrong. =(

~


Expand Down
Expand Up @@ -16,4 +16,4 @@ tests/cases/compiler/errorRecoveryWithDotFollowedByNamespaceKeyword.ts(9,2): err
}

!!! 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:3:18: The parser expected to find a '}' to match the '{' token here.
2 changes: 1 addition & 1 deletion tests/baselines/reference/exportInFunction.errors.txt
Expand Up @@ -7,4 +7,4 @@ tests/cases/compiler/exportInFunction.ts(3,1): error TS1005: '}' expected.


!!! error TS1005: '}' expected.
!!! related TS1007 tests/cases/compiler/exportInFunction.ts:1:14: The parser expected to find a '}' to match the '{' token here.
!!! related TS1007 tests/cases/compiler/exportInFunction.ts:1:13: The parser expected to find a '}' to match the '{' token here.
3 changes: 1 addition & 2 deletions tests/baselines/reference/jsonParserRecovery/JSX.errors.txt
Expand Up @@ -33,5 +33,4 @@ JSX(15,10): error TS1005: '}' expected.
</div>
)

!!! error TS1005: '}' expected.
!!! related TS1007 JSX:4:9: The parser expected to find a '}' to match the '{' token here.
!!! error TS1005: '}' expected.
Expand Up @@ -16,5 +16,4 @@ TypeScript code(1,22): error TS1005: '}' expected.
~~~~
!!! error TS1012: Unexpected token.

!!! error TS1005: '}' expected.
!!! related TS1007 TypeScript code:1:18: The parser expected to find a '}' to match the '{' token here.
!!! error TS1005: '}' expected.
Expand Up @@ -7,5 +7,4 @@ trailing identifier(1,8): error TS1005: '}' expected.
~~~~
!!! error TS1012: Unexpected token.

!!! error TS1005: '}' expected.
!!! related TS1007 trailing identifier:1:4: The parser expected to find a '}' to match the '{' token here.
!!! error TS1005: '}' expected.
2 changes: 1 addition & 1 deletion tests/baselines/reference/missingCloseBrace.errors.txt
Expand Up @@ -13,4 +13,4 @@ tests/cases/compiler/missingCloseBrace.ts(9,1): error TS1005: '}' expected.


!!! error TS1005: '}' expected.
!!! related TS1007 tests/cases/compiler/missingCloseBrace.ts:1:22: The parser expected to find a '}' to match the '{' token here.
!!! related TS1007 tests/cases/compiler/missingCloseBrace.ts:1:21: The parser expected to find a '}' to match the '{' token here.
Expand Up @@ -9,4 +9,4 @@ tests/cases/compiler/missingCloseBraceInObjectLiteral.ts(5,1): error TS1005: '}'


!!! error TS1005: '}' expected.
!!! related TS1007 tests/cases/compiler/missingCloseBraceInObjectLiteral.ts:1:11: The parser expected to find a '}' to match the '{' token here.
!!! related TS1007 tests/cases/compiler/missingCloseBraceInObjectLiteral.ts:1:10: The parser expected to find a '}' to match the '{' token here.
Expand Up @@ -5,4 +5,4 @@ tests/cases/compiler/missingCloseBracketInArray.ts(1,48): error TS1005: ']' expe
var alphas:string[] = alphas = ["1","2","3","4"

!!! error TS1005: ']' expected.
!!! related TS1007 tests/cases/compiler/missingCloseBracketInArray.ts:1:32: The parser expected to find a ']' to match the '[' token here.
!!! related TS1007 tests/cases/compiler/missingCloseBracketInArray.ts:1:31: The parser expected to find a ']' to match the '[' token here.
Expand Up @@ -9,7 +9,7 @@ tests/cases/compiler/missingCloseParenStatements.ts(11,35): error TS1005: ')' ex
if ( a1 && (a2 + a3 > 0) {
~
!!! error TS1005: ')' expected.
!!! related TS1007 tests/cases/compiler/missingCloseParenStatements.ts:2:4: The parser expected to find a ')' to match the '(' token here.
!!! related TS1007 tests/cases/compiler/missingCloseParenStatements.ts:2:3: The parser expected to find a ')' to match the '(' token here.
while( (a2 > 0) && a1
{
~
Expand All @@ -21,12 +21,12 @@ tests/cases/compiler/missingCloseParenStatements.ts(11,35): error TS1005: ')' ex
with ((a2 + a3 > 0) && a1 {
~
!!! error TS1005: ')' expected.
!!! related TS1007 tests/cases/compiler/missingCloseParenStatements.ts:8:18: The parser expected to find a ')' to match the '(' token here.
!!! related TS1007 tests/cases/compiler/missingCloseParenStatements.ts:8:17: The parser expected to find a ')' to match the '(' token here.
console.log(x);
}
} while (i < 5 && (a1 > 5);
~
!!! error TS1005: ')' expected.
!!! related TS1007 tests/cases/compiler/missingCloseParenStatements.ts:11:17: The parser expected to find a ')' to match the '(' token here.
!!! related TS1007 tests/cases/compiler/missingCloseParenStatements.ts:11:16: The parser expected to find a ')' to match the '(' token here.
}
}
Expand Up @@ -11,7 +11,7 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IfStatements/parserErro
}
~
!!! error TS1005: ')' expected.
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IfStatements/parserErrorRecoveryIfStatement2.ts:3:8: The parser expected to find a ')' to match the '(' token here.
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IfStatements/parserErrorRecoveryIfStatement2.ts:3:7: The parser expected to find a ')' to match the '(' token here.
f2() {
}
f3() {
Expand Down
Expand Up @@ -11,7 +11,7 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IfStatements/parserErro
}
~
!!! error TS1005: ')' expected.
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IfStatements/parserErrorRecoveryIfStatement3.ts:3:8: The parser expected to find a ')' to match the '(' token here.
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/IfStatements/parserErrorRecoveryIfStatement3.ts:3:7: The parser expected to find a ')' to match the '(' token here.
f2() {
}
f3() {
Expand Down
Expand Up @@ -15,4 +15,4 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserEr
!!! error TS1005: ':' expected.

!!! error TS1005: '}' expected.
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts:1:9: The parser expected to find a '}' to match the '{' token here.
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral2.ts:1:8: The parser expected to find a '}' to match the '{' token here.
Expand Up @@ -12,4 +12,4 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserEr
!!! error TS1005: ':' expected.

!!! error TS1005: '}' expected.
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral3.ts:1:9: The parser expected to find a '}' to match the '{' token here.
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral3.ts:1:8: The parser expected to find a '}' to match the '{' token here.
Expand Up @@ -12,4 +12,4 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserEr
!!! error TS1005: ':' expected.

!!! error TS1005: '}' expected.
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral4.ts:1:9: The parser expected to find a '}' to match the '{' token here.
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral4.ts:1:8: The parser expected to find a '}' to match the '{' token here.
Expand Up @@ -9,4 +9,4 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserEr
!!! error TS1005: ':' expected.

!!! error TS1005: '}' expected.
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral5.ts:1:9: The parser expected to find a '}' to match the '{' token here.
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserErrorRecovery_ObjectLiteral5.ts:1:8: The parser expected to find a '}' to match the '{' token here.
Expand Up @@ -16,4 +16,4 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/SwitchStatements/parser
}

!!! 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.
!!! related TS1007 tests/cases/conformance/parser/ecmascript5/ErrorRecovery/SwitchStatements/parserErrorRecovery_SwitchStatement2.ts:2:16: The parser expected to find a '}' to match the '{' token here.
Expand Up @@ -3,9 +3,9 @@
2
  

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


Expand All @@ -14,6 +14,6 @@


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