diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index f7949496bfee2..4f90045cbf6ba 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -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 diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 2287c41431616..4d4b74b238bf5 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2370,6 +2370,12 @@ namespace ts { const tokenText = scanner.getTokenText(); (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()) { @@ -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 { diff --git a/tests/baselines/reference/parser579071.errors.txt b/tests/baselines/reference/parser579071.errors.txt new file mode 100644 index 0000000000000..50d0c037a7efd --- /dev/null +++ b/tests/baselines/reference/parser579071.errors.txt @@ -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/'. \ No newline at end of file diff --git a/tests/baselines/reference/parserRegularExpressionDivideAmbiguity4.errors.txt b/tests/baselines/reference/parserRegularExpressionDivideAmbiguity4.errors.txt index f545f0f98086f..293c7533351c3 100644 --- a/tests/baselines/reference/parserRegularExpressionDivideAmbiguity4.errors.txt +++ b/tests/baselines/reference/parserRegularExpressionDivideAmbiguity4.errors.txt @@ -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.