Skip to content

Added validation for regex literals via RegExp constructor #35957

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,10 @@
"category": "Error",
"code": 1261
},
"Invalid regular expression literal: '{0}'.": {
"category": "Error",
"code": 1262
},
"'with' statements are not allowed in an async function block.": {
"category": "Error",
"code": 1300
Expand Down
16 changes: 16 additions & 0 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,12 @@ namespace ts {
const tokenText = scanner.getTokenText();
(<TemplateLiteralLikeNode>node).rawText = tokenText.substring(1, tokenText.length - (scanner.isUnterminated() ? 0 : isLast ? 1 : 2));
break;

case SyntaxKind.RegularExpressionLiteral:
if (!isValidRegularExpressionLiteral(node.text)) {
parseErrorAtCurrentToken(Diagnostics.Invalid_regular_expression_literal_Colon_0, node.text);
}
break;
}

if (scanner.hasExtendedUnicodeEscape()) {
Expand All @@ -2396,6 +2402,16 @@ namespace ts {
return node;
}

function isValidRegularExpressionLiteral(text: string) {
try {
new RegExp(text);
return true;
}
catch {
return false;
}
}

// TYPES

function parseTypeReference(): TypeReferenceNode {
Expand Down
7 changes: 7 additions & 0 deletions tests/baselines/reference/parser579071.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests/cases/conformance/parser/ecmascript5/RegressionTests/parser579071.ts(1,9): error TS1262: Invalid regular expression literal: '/fo(o/'.


==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser579071.ts (1 errors) ====
var x = /fo(o/;
~~~~~~
!!! error TS1262: Invalid regular expression literal: '/fo(o/'.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpressionDivideAmbiguity4.ts(1,1): error TS2304: Cannot find name 'foo'.
tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpressionDivideAmbiguity4.ts(1,5): error TS1262: Invalid regular expression literal: '/notregexp);'.
tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpressionDivideAmbiguity4.ts(1,6): error TS1161: Unterminated regular expression literal.
tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpressionDivideAmbiguity4.ts(1,17): error TS1005: ')' expected.


==== tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpressionDivideAmbiguity4.ts (3 errors) ====
==== tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpressionDivideAmbiguity4.ts (4 errors) ====
foo(/notregexp);
~~~
!!! error TS2304: Cannot find name 'foo'.
~~~~~~~~~~~~
!!! error TS1262: Invalid regular expression literal: '/notregexp);'.

!!! error TS1161: Unterminated regular expression literal.

Expand Down