-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Improve Recovery of Unterminated Regular Expressions #58289
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ff858bc
Improve Recovery of Unterminated Regular Expressions
graphemecluster c7b65ac
Merge branch 'main' into regex-recovery
graphemecluster a7621e0
Unrevert the change to incremental parser test
graphemecluster f1aa06f
Revert the change to `parseErrorAtPosition` in `parser.ts`
graphemecluster 978e195
I’m very wrong
graphemecluster 5c75b2d
Merge branch 'main' into regex-recovery
graphemecluster 76acd92
Use `scanRange` to prevent variable shadowing
graphemecluster e67692a
Correct flags scanning for non-BMP characters
graphemecluster 0247ea8
Merge branch 'main' into regex-recovery
rbuckton 11ad5f5
Revert "Correct flags scanning for non-BMP characters"
graphemecluster 77235de
Apply Suggested Changes
graphemecluster 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 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 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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 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,81 @@ | ||
import * as ts from "../_namespaces/ts.js"; | ||
|
||
describe("unittests:: regExpScannerRecovery", () => { | ||
const testCases = [ | ||
"/", | ||
"/[]", | ||
"/{}", | ||
"/()", | ||
"/foo", | ||
"/foo[]", | ||
"/foo{}", | ||
"/foo()", | ||
"/[]foo", | ||
"/{}foo", | ||
"/()foo", | ||
"/{[]}", | ||
"/([])", | ||
"/[)}({]", | ||
"/({[]})", | ||
"/\\[", | ||
"/\\{", | ||
"/\\(", | ||
"/[\\[]", | ||
"/(\\[)", | ||
"/{\\[}", | ||
"/[\\(]", | ||
"/(\\()", | ||
"/{\\(}", | ||
"/[\\{]", | ||
"/(\\{)", | ||
"/{\\{}", | ||
"/\\{(\\[\\([{])", | ||
"/\\]", | ||
"/\\}", | ||
"/\\)", | ||
"/[\\]]", | ||
"/(\\])", | ||
"/{\\]}", | ||
"/[\\)]", | ||
"/(\\))", | ||
"/{\\)}", | ||
"/[\\}]", | ||
"/(\\})", | ||
"/{\\}}", | ||
"/({[\\])]})", | ||
]; | ||
const whiteSpaceSequences = [ | ||
"", | ||
" ", | ||
"\t\f", | ||
"\u3000\u2003", | ||
]; | ||
for (const testCase of testCases) { | ||
for (const whiteSpaces of whiteSpaceSequences) { | ||
const testCaseWithWhiteSpaces = testCase + whiteSpaces; | ||
const sources = [ | ||
`const regex = ${testCaseWithWhiteSpaces};`, | ||
`(${testCaseWithWhiteSpaces});`, | ||
`([${testCaseWithWhiteSpaces}]);`, | ||
`({prop: ${testCaseWithWhiteSpaces}});`, | ||
`({prop: ([(${testCaseWithWhiteSpaces})])});`, | ||
`({[(${testCaseWithWhiteSpaces}).source]: 42});`, | ||
]; | ||
for (const source of sources) { | ||
it("stops parsing unterminated regexes at correct position: " + JSON.stringify(source), () => { | ||
const { parseDiagnostics } = ts.createLanguageServiceSourceFile( | ||
/*fileName*/ "", | ||
ts.ScriptSnapshot.fromString(source), | ||
ts.ScriptTarget.Latest, | ||
/*version*/ "0", | ||
/*setNodeParents*/ false, | ||
); | ||
const diagnostic = ts.find(parseDiagnostics, ({ code }) => code === ts.Diagnostics.Unterminated_regular_expression_literal.code); | ||
assert(diagnostic, "There should be an 'Unterminated regular expression literal.' error"); | ||
assert.equal(diagnostic.start, source.indexOf("/"), "Diagnostic should start at where the regex starts"); | ||
assert.equal(diagnostic.length, testCase.length, "Diagnostic should end at where the regex ends"); | ||
}); | ||
} | ||
} | ||
} | ||
}); |
This file contains 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 |
---|---|---|
@@ -1,13 +1,10 @@ | ||
parser645086_1.ts(1,13): error TS1005: ',' expected. | ||
parser645086_1.ts(1,14): error TS1134: Variable declaration expected. | ||
parser645086_1.ts(1,15): error TS1161: Unterminated regular expression literal. | ||
|
||
|
||
==== parser645086_1.ts (3 errors) ==== | ||
==== parser645086_1.ts (2 errors) ==== | ||
var v = /[]/]/ | ||
~ | ||
!!! error TS1005: ',' expected. | ||
~ | ||
!!! error TS1134: Variable declaration expected. | ||
|
||
!!! error TS1161: Unterminated regular expression literal. | ||
!!! error TS1134: Variable declaration expected. |
This file contains 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 |
---|---|---|
@@ -1,13 +1,10 @@ | ||
parser645086_2.ts(1,14): error TS1005: ',' expected. | ||
parser645086_2.ts(1,15): error TS1134: Variable declaration expected. | ||
parser645086_2.ts(1,16): error TS1161: Unterminated regular expression literal. | ||
|
||
|
||
==== parser645086_2.ts (3 errors) ==== | ||
==== parser645086_2.ts (2 errors) ==== | ||
var v = /[^]/]/ | ||
~ | ||
!!! error TS1005: ',' expected. | ||
~ | ||
!!! error TS1134: Variable declaration expected. | ||
|
||
!!! error TS1161: Unterminated regular expression literal. | ||
!!! error TS1134: Variable declaration expected. |
This file contains 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
parserMissingToken2.ts(1,2): error TS1161: Unterminated regular expression literal. | ||
parserMissingToken2.ts(1,1): error TS1161: Unterminated regular expression literal. | ||
|
||
|
||
==== parserMissingToken2.ts (1 errors) ==== | ||
/ b; | ||
~~~ | ||
!!! error TS1161: Unterminated regular expression literal. |
This file contains 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 |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
/ b; | ||
//// [parserMissingToken2.js] | ||
/ b;; | ||
/ b; |
This file contains 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 |
---|---|---|
|
@@ -2,6 +2,6 @@ | |
|
||
=== parserMissingToken2.ts === | ||
/ b; | ||
>/ b; : RegExp | ||
> : ^^^^^^ | ||
>/ b : RegExp | ||
> : ^^^^^^ | ||
|
11 changes: 4 additions & 7 deletions
11
tests/baselines/reference/parserRegularExpressionDivideAmbiguity4.errors.txt
This file contains 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 |
---|---|---|
@@ -1,13 +1,10 @@ | ||
parserRegularExpressionDivideAmbiguity4.ts(1,1): error TS2304: Cannot find name 'foo'. | ||
parserRegularExpressionDivideAmbiguity4.ts(1,6): error TS1161: Unterminated regular expression literal. | ||
parserRegularExpressionDivideAmbiguity4.ts(1,17): error TS1005: ')' expected. | ||
parserRegularExpressionDivideAmbiguity4.ts(1,5): error TS1161: Unterminated regular expression literal. | ||
|
||
|
||
==== parserRegularExpressionDivideAmbiguity4.ts (3 errors) ==== | ||
==== parserRegularExpressionDivideAmbiguity4.ts (2 errors) ==== | ||
foo(/notregexp); | ||
~~~ | ||
!!! error TS2304: Cannot find name 'foo'. | ||
|
||
!!! error TS1161: Unterminated regular expression literal. | ||
|
||
!!! error TS1005: ')' expected. | ||
~~~~~~~~~~ | ||
!!! error TS1161: Unterminated regular expression literal. |
This file contains 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 |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
foo(/notregexp); | ||
|
||
//// [parserRegularExpressionDivideAmbiguity4.js] | ||
foo(/notregexp);); | ||
foo(/notregexp); |
This file contains 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 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 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 |
---|---|---|
|
@@ -22,4 +22,4 @@ data = { 32: } / > ; | |
{ | ||
32; | ||
} | ||
/>;; | ||
/>; |
This file contains 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 |
---|---|---|
|
@@ -56,6 +56,6 @@ declare module JSX { | |
> : ^^^ | ||
>32 : 32 | ||
> : ^^ | ||
>/>; : RegExp | ||
> : ^^^^^^ | ||
>/> : RegExp | ||
> : ^^^^^^ | ||
|
4 changes: 2 additions & 2 deletions
4
tests/baselines/reference/unterminatedRegexAtEndOfSource1.errors.txt
This file contains 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
unterminatedRegexAtEndOfSource1.ts(1,10): error TS1161: Unterminated regular expression literal. | ||
unterminatedRegexAtEndOfSource1.ts(1,9): error TS1161: Unterminated regular expression literal. | ||
|
||
|
||
==== unterminatedRegexAtEndOfSource1.ts (1 errors) ==== | ||
var a = / | ||
~ | ||
!!! error TS1161: Unterminated regular expression literal. |
This file contains 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
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.
Should we include angle brackets (in addition to parens and braces) to the logic?
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.
With respect to treating them as unterminated? I don't think that's necessary.
(?<)
will still produce an error during the grammar check pass while still maintaining a proper bound for the RegExp body.Also, Annex B does not treat
/\k</
as an error since the RegExp does not define a named capture group, which indicates that<>
handling is not a lexical syntax error.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.
I'm not too terribly concerned about this case, to be honest. Parsing falls off the rails here because of malformed JSX, not a malformed RegExp.