Skip to content
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
1 change: 1 addition & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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." },
};
}
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
14 changes: 14 additions & 0 deletions src/compiler/scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,15 @@ namespace ts {

let hasOwnProperty = Object.prototype.hasOwnProperty;

function isValidRegex(r : string): boolean {
try {
RegExp(r);
} catch (e) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch on the next line

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.
Expand Down Expand Up @@ -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;
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,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.
4 changes: 0 additions & 4 deletions tests/baselines/reference/parser579071.symbols

This file was deleted.

5 changes: 0 additions & 5 deletions tests/baselines/reference/parser579071.types

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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) ====
Expand All @@ -10,4 +10,4 @@ tests/cases/conformance/parser/ecmascript5/RegularExpressions/parserRegularExpre

!!! error TS1161: Unterminated regular expression literal.

!!! error TS1005: ')' expected.
!!! error TS17006: Invalid regular expression literal.