-
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
+223
−125
Merged
Changes from 9 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
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
graphemecluster marked this conversation as resolved.
Show resolved
Hide resolved
|
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:: regExpParserRecovery", () => { | ||
const testCases = [ | ||
"/", | ||
"/[]", | ||
"/{}", | ||
"/()", | ||
"/foo", | ||
"/foo[]", | ||
"/foo{}", | ||
"/foo()", | ||
"/[]foo", | ||
"/{}foo", | ||
"/()foo", | ||
"/{[]}", | ||
"/([])", | ||
"/[)}({]", | ||
"/({[)}]})", | ||
"/\\[", | ||
"/\\{", | ||
"/\\(", | ||
"/[\\[]", | ||
"/(\\[)", | ||
"/{\\[}", | ||
"/[\\(]", | ||
"/(\\()", | ||
"/{\\(}", | ||
"/[\\{]", | ||
"/(\\{)", | ||
"/{\\{}", | ||
"/\\{(\\[\\([{])", | ||
"/\\]", | ||
"/\\}", | ||
"/\\)", | ||
"/[\\]]", | ||
"/(\\])", | ||
"/{\\]}", | ||
"/[\\)]", | ||
"/(\\))", | ||
"/{\\)}", | ||
"/[\\}]", | ||
"/(\\})", | ||
"/{\\}}", | ||
"/({[\\]})]})", | ||
]; | ||
const whiteSpaceSequences = [ | ||
"", | ||
" ", | ||
"\t\v\r\n", | ||
"\u3000\u2028", | ||
]; | ||
it("stops parsing unterminated regexes at correct position", () => { | ||
graphemecluster marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ts.forEach(testCases, testCase => { | ||
ts.forEach(whiteSpaceSequences, whiteSpaces => { | ||
const testCaseWithWhiteSpaces = testCase + whiteSpaces; | ||
const sources = [ | ||
`const regex = ${testCaseWithWhiteSpaces};`, | ||
`(${testCaseWithWhiteSpaces});`, | ||
`([${testCaseWithWhiteSpaces}]);`, | ||
`({prop: ${testCaseWithWhiteSpaces}});`, | ||
`({prop: ([(${testCaseWithWhiteSpaces})])});`, | ||
`({[(${testCaseWithWhiteSpaces}).source]: 42});`, | ||
]; | ||
ts.forEach(sources, 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. |
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.
This is eliminated for readability