-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Improve JSON parser error recovery #42657
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5417e7c
Improve JSON parser error recovery
andrewbranch 98e8d8d
Add error baselines
andrewbranch b6a2f39
Move tsconfig root checking out of common JSON checking function
andrewbranch 4e4b5a2
Use new function in parseConfigFileTextToJson
andrewbranch a8859af
Fix test
andrewbranch 41925b3
Replace non-null assertion with explicit debug assertion
andrewbranch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
namespace ts { | ||
describe("unittests:: jsonParserRecovery", () => { | ||
function parsesToValidSourceFileWithErrors(name: string, text: string) { | ||
it(name, () => { | ||
const file = parseJsonText(name, text); | ||
assert(file.parseDiagnostics.length, "Should have parse errors"); | ||
Harness.Baseline.runBaseline( | ||
`jsonParserRecovery/${name.replace(/[^a-z0-9_-]/ig, "_")}.errors.txt`, | ||
Harness.Compiler.getErrorBaseline([{ | ||
content: text, | ||
unitName: name | ||
}], file.parseDiagnostics)); | ||
|
||
// Will throw if parse tree does not cover full input text | ||
file.getChildren(); | ||
}); | ||
} | ||
|
||
parsesToValidSourceFileWithErrors("trailing identifier", "{} blah"); | ||
parsesToValidSourceFileWithErrors("TypeScript code", "interface Foo {} blah"); | ||
parsesToValidSourceFileWithErrors("Two comma-separated objects", "{}, {}"); | ||
parsesToValidSourceFileWithErrors("Two objects", "{} {}"); | ||
parsesToValidSourceFileWithErrors("JSX", ` | ||
interface Test {} | ||
|
||
const Header = () => ( | ||
<div> | ||
<h1>Header</h1> | ||
<style jsx> | ||
{\` | ||
h1 { | ||
color: red; | ||
} | ||
\`} | ||
</style> | ||
</div> | ||
)`); | ||
}); | ||
} |
37 changes: 37 additions & 0 deletions
37
tests/baselines/reference/jsonParserRecovery/JSX.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
JSX(2,9): error TS1005: '{' expected. | ||
JSX(2,19): error TS1005: ',' expected. | ||
JSX(2,24): error TS1005: ',' expected. | ||
JSX(4,9): error TS1012: Unexpected token. | ||
JSX(4,15): error TS1005: ':' expected. | ||
JSX(15,10): error TS1005: '}' expected. | ||
|
||
|
||
==== JSX (6 errors) ==== | ||
|
||
interface Test {} | ||
~~~~~~~~~ | ||
!!! error TS1005: '{' expected. | ||
~~~~ | ||
!!! error TS1005: ',' expected. | ||
~ | ||
!!! error TS1005: ',' expected. | ||
|
||
const Header = () => ( | ||
~~~~~ | ||
!!! error TS1012: Unexpected token. | ||
~~~~~~ | ||
!!! error TS1005: ':' expected. | ||
<div> | ||
<h1>Header</h1> | ||
<style jsx> | ||
{` | ||
h1 { | ||
color: red; | ||
} | ||
`} | ||
</style> | ||
</div> | ||
) | ||
|
||
!!! error TS1005: '}' expected. | ||
!!! related TS1007 JSX:4:9: The parser expected to find a '}' to match the '{' token here. |
10 changes: 10 additions & 0 deletions
10
tests/baselines/reference/jsonParserRecovery/Two_comma-separated_objects.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Two comma-separated objects(1,3): error TS1012: Unexpected token. | ||
Two comma-separated objects(1,5): error TS1136: Property assignment expected. | ||
|
||
|
||
==== Two comma-separated objects (2 errors) ==== | ||
{}, {} | ||
~ | ||
!!! error TS1012: Unexpected token. | ||
~ | ||
!!! error TS1136: Property assignment expected. |
7 changes: 7 additions & 0 deletions
7
tests/baselines/reference/jsonParserRecovery/Two_objects.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Two objects(1,4): error TS1012: Unexpected token. | ||
|
||
|
||
==== Two objects (1 errors) ==== | ||
{} {} | ||
~ | ||
!!! error TS1012: Unexpected token. |
20 changes: 20 additions & 0 deletions
20
tests/baselines/reference/jsonParserRecovery/TypeScript_code.errors.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
TypeScript code(1,1): error TS1005: '{' expected. | ||
TypeScript code(1,11): error TS1005: ',' expected. | ||
TypeScript code(1,15): error TS1005: ',' expected. | ||
TypeScript code(1,18): error TS1012: Unexpected token. | ||
TypeScript code(1,22): error TS1005: '}' expected. | ||
|
||
|
||
==== TypeScript code (5 errors) ==== | ||
interface Foo {} blah | ||
~~~~~~~~~ | ||
!!! error TS1005: '{' expected. | ||
~~~ | ||
!!! error TS1005: ',' expected. | ||
~ | ||
!!! 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. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this potentially be a polymorphism deopt?