-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve Recovery of Unterminated Regular Expressions (#58289)
Co-authored-by: Ron Buckton <ron.buckton@microsoft.com>
- Loading branch information
1 parent
842cf17
commit d0ef028
Showing
20 changed files
with
223 additions
and
125 deletions.
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