Show token hints for missing closing braces (#36317)

* Add error for missing brace in object literal

* Add new baseline test

* Update all affected tests
This commit is contained in:
Sheon Han 2020-03-12 18:52:02 -04:00 committed by GitHub
parent c6cfd66fa8
commit b014e2b568
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 82 additions and 6 deletions

View File

@ -5110,13 +5110,22 @@ namespace ts {
function parseObjectLiteralExpression(): ObjectLiteralExpression {
const node = <ObjectLiteralExpression>createNode(SyntaxKind.ObjectLiteralExpression);
const openBracePosition = scanner.getTokenPos();
parseExpected(SyntaxKind.OpenBraceToken);
if (scanner.hasPrecedingLineBreak()) {
node.multiLine = true;
}
node.properties = parseDelimitedList(ParsingContext.ObjectLiteralMembers, parseObjectLiteralElement, /*considerSemicolonAsDelimiter*/ true);
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)
);
}
}
return finishNode(node);
}

View File

@ -0,0 +1,12 @@
tests/cases/compiler/missingCloseBraceInObjectLiteral.ts(5,1): error TS1005: '}' expected.
==== tests/cases/compiler/missingCloseBraceInObjectLiteral.ts (1 errors) ====
var foo = {
a: 'a',
b: 'b',
c: 'c'
!!! error TS1005: '}' expected.
!!! related TS1007 tests/cases/compiler/missingCloseBraceInObjectLiteral.ts:1:11: The parser expected to find a '}' to match the '{' token here.

View File

@ -0,0 +1,13 @@
//// [missingCloseBraceInObjectLiteral.ts]
var foo = {
a: 'a',
b: 'b',
c: 'c'
//// [missingCloseBraceInObjectLiteral.js]
var foo = {
a: 'a',
b: 'b',
c: 'c'
};

View File

@ -0,0 +1,13 @@
=== tests/cases/compiler/missingCloseBraceInObjectLiteral.ts ===
var foo = {
>foo : Symbol(foo, Decl(missingCloseBraceInObjectLiteral.ts, 0, 3))
a: 'a',
>a : Symbol(a, Decl(missingCloseBraceInObjectLiteral.ts, 0, 11))
b: 'b',
>b : Symbol(b, Decl(missingCloseBraceInObjectLiteral.ts, 1, 11))
c: 'c'
>c : Symbol(c, Decl(missingCloseBraceInObjectLiteral.ts, 2, 11))

View File

@ -0,0 +1,17 @@
=== tests/cases/compiler/missingCloseBraceInObjectLiteral.ts ===
var foo = {
>foo : { a: string; b: string; c: string; }
>{ a: 'a', b: 'b', c: 'c' : { a: string; b: string; c: string; }
a: 'a',
>a : string
>'a' : "a"
b: 'b',
>b : string
>'b' : "b"
c: 'c'
>c : string
>'c' : "c"

View File

@ -32,6 +32,7 @@ tests/cases/conformance/classes/nestedClassDeclaration.ts(17,1): error TS1128: D
!!! error TS2304: Cannot find name 'C4'.
~
!!! error TS1005: ',' expected.
!!! related TS1007 tests/cases/conformance/classes/nestedClassDeclaration.ts:14:9: The parser expected to find a '}' to match the '{' token here.
}
}
~

View File

@ -9,4 +9,5 @@ tests/cases/compiler/objectLiteralWithSemicolons4.ts(3,1): error TS1005: ',' exp
!!! error TS18004: No value exists in scope for the shorthand property 'a'. Either declare one or provide an initializer.
;
~
!!! error TS1005: ',' expected.
!!! error TS1005: ',' expected.
!!! related TS1007 tests/cases/compiler/objectLiteralWithSemicolons4.ts:1:9: The parser expected to find a '}' to match the '{' token here.

View File

@ -28,6 +28,7 @@ tests/cases/conformance/types/spread/objectSpreadNegativeParse.ts(4,20): error T
!!! error TS2304: Cannot find name 'matchMedia'.
~
!!! error TS1005: ',' expected.
!!! related TS1007 tests/cases/conformance/types/spread/objectSpreadNegativeParse.ts:3:10: The parser expected to find a '}' to match the '{' token here.
~
!!! error TS1128: Declaration or statement expected.
let o10 = { ...get x() { return 12; }};

View File

@ -25,6 +25,7 @@ tests/cases/compiler/parseErrorIncorrectReturnToken.ts(12,1): error TS1128: Decl
m(n: number) => string {
~~
!!! error TS1005: '{' expected.
!!! related TS1007 tests/cases/compiler/parseErrorIncorrectReturnToken.ts:8:9: The parser expected to find a '}' to match the '{' token here.
~~~~~~
!!! error TS2693: 'string' only refers to a type, but is being used as a value here.
~

View File

@ -14,4 +14,5 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserEr
~
!!! error TS1005: ':' expected.
!!! 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.

View File

@ -11,4 +11,5 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserEr
~
!!! error TS1005: ':' expected.
!!! 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.

View File

@ -11,4 +11,5 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserEr
~
!!! error TS1005: ':' expected.
!!! 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.

View File

@ -8,4 +8,5 @@ tests/cases/conformance/parser/ecmascript5/ErrorRecovery/ObjectLiterals/parserEr
~
!!! error TS1005: ':' expected.
!!! 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.

View File

@ -0,0 +1,4 @@
var foo = {
a: 'a',
b: 'b',
c: 'c'