From ac62b0e69cf058718f97008e3d2c920b03d73768 Mon Sep 17 00:00:00 2001 From: Schmavery Date: Thu, 20 Aug 2015 17:31:36 -0700 Subject: [PATCH] Add `invalid regular expression literal` error #3432 --- src/compiler/diagnosticInformationMap.generated.ts | 1 + src/compiler/diagnosticMessages.json | 4 ++++ src/compiler/scanner.ts | 14 ++++++++++++++ tests/baselines/reference/parser579071.errors.txt | 7 +++++++ tests/baselines/reference/parser579071.symbols | 4 ---- tests/baselines/reference/parser579071.types | 5 ----- ...serRegularExpressionDivideAmbiguity4.errors.txt | 4 ++-- 7 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 tests/baselines/reference/parser579071.errors.txt delete mode 100644 tests/baselines/reference/parser579071.symbols delete mode 100644 tests/baselines/reference/parser579071.types diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 5ccc0a0db9b40..e3d4597355254 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -616,5 +616,6 @@ namespace ts { JSX_attribute_expected: { code: 17003, category: DiagnosticCategory.Error, key: "JSX attribute expected." }, Cannot_use_JSX_unless_the_jsx_flag_is_provided: { code: 17004, category: DiagnosticCategory.Error, key: "Cannot use JSX unless the '--jsx' flag is provided." }, A_constructor_cannot_contain_a_super_call_when_its_class_extends_null: { code: 17005, category: DiagnosticCategory.Error, key: "A constructor cannot contain a 'super' call when its class extends 'null'" }, + Invalid_regular_expression_literal: { code: 17006, category: DiagnosticCategory.Error, key: "Invalid regular expression literal." }, }; } \ No newline at end of file diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index e138e2ddac489..96053243c68f7 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2457,5 +2457,9 @@ "A constructor cannot contain a 'super' call when its class extends 'null'": { "category": "Error", "code": 17005 + }, + "Invalid regular expression literal.": { + "category": "Error", + "code": 17006 } } diff --git a/src/compiler/scanner.ts b/src/compiler/scanner.ts index 199e7e9b63857..d90e7160d2553 100644 --- a/src/compiler/scanner.ts +++ b/src/compiler/scanner.ts @@ -347,6 +347,15 @@ namespace ts { let hasOwnProperty = Object.prototype.hasOwnProperty; + function isValidRegex(r : string): boolean { + try { + RegExp(r); + } catch (e) { + return false; + } + return true; + } + export function isWhiteSpace(ch: number): boolean { // Note: nextLine is in the Zs space, and should be considered to be a whitespace. // It is explicitly not a line-break as it isn't in the exact set specified by EcmaScript. @@ -1536,6 +1545,11 @@ namespace ts { } pos = p; tokenValue = text.substring(tokenPos, pos); + + if (inCharacterClass || !isValidRegex(tokenValue)) { + error(Diagnostics.Invalid_regular_expression_literal); + } + token = SyntaxKind.RegularExpressionLiteral; } return token; diff --git a/tests/baselines/reference/parser579071.errors.txt b/tests/baselines/reference/parser579071.errors.txt new file mode 100644 index 0000000000000..231940748eca8 --- /dev/null +++ b/tests/baselines/reference/parser579071.errors.txt @@ -0,0 +1,7 @@ +tests/cases/conformance/parser/ecmascript5/RegressionTests/parser579071.ts(1,15): error TS17006: Invalid regular expression literal. + + +==== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser579071.ts (1 errors) ==== + var x = /fo(o/; + +!!! error TS17006: Invalid regular expression literal. \ No newline at end of file diff --git a/tests/baselines/reference/parser579071.symbols b/tests/baselines/reference/parser579071.symbols deleted file mode 100644 index 5c14f161641a1..0000000000000 --- a/tests/baselines/reference/parser579071.symbols +++ /dev/null @@ -1,4 +0,0 @@ -=== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser579071.ts === -var x = /fo(o/; ->x : Symbol(x, Decl(parser579071.ts, 0, 3)) - diff --git a/tests/baselines/reference/parser579071.types b/tests/baselines/reference/parser579071.types deleted file mode 100644 index bfdece6f7879e..0000000000000 --- a/tests/baselines/reference/parser579071.types +++ /dev/null @@ -1,5 +0,0 @@ -=== tests/cases/conformance/parser/ecmascript5/RegressionTests/parser579071.ts === -var x = /fo(o/; ->x : RegExp ->/fo(o/ : RegExp - diff --git a/tests/baselines/reference/parserRegularExpressionDivideAmbiguity4.errors.txt b/tests/baselines/reference/parserRegularExpressionDivideAmbiguity4.errors.txt index f545f0f98086f..d56a6878a39ae 100644 --- a/tests/baselines/reference/parserRegularExpressionDivideAmbiguity4.errors.txt +++ b/tests/baselines/reference/parserRegularExpressionDivideAmbiguity4.errors.txt @@ -1,6 +1,6 @@ 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,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(1,17): error TS17006: Invalid regular expression literal. ==== tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpressionDivideAmbiguity4.ts (3 errors) ==== @@ -10,4 +10,4 @@ tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpre !!! error TS1161: Unterminated regular expression literal. -!!! error TS1005: ')' expected. \ No newline at end of file +!!! error TS17006: Invalid regular expression literal. \ No newline at end of file