Skip to content

Commit

Permalink
[[FIX]] Allow more escapes with RegExp u flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike authored and rwaldron committed Jun 24, 2019
1 parent 0a60c9e commit 5ac5c46
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/lex.js
Expand Up @@ -1431,6 +1431,18 @@ Lexer.prototype = {
checks,
function() { return true; }
);
} else if (char === "0" && reg.decimalDigit.test(this.peek(index + 1))) {
this.triggerAsync(
"error",
{
code: "E016",
line: this.line,
character: this.char,
data: [ "Invalid decimal escape sequence" ]
},
checks,
hasUFlag
);
}

index += 1;
Expand Down Expand Up @@ -1707,6 +1719,8 @@ Lexer.prototype = {
return !escapedChars.split("").every(function(escapedChar) {
return escapedChar === "u" ||
escapedChar === "/" ||
escapedChar === "0" ||
reg.regexpControlEscapes.test(escapedChar) ||
reg.regexpCharClasses.test(escapedChar) ||
reg.regexpSyntaxChars.test(escapedChar);
});
Expand Down
2 changes: 2 additions & 0 deletions src/reg.js
Expand Up @@ -49,6 +49,8 @@ exports.regexpSyntaxChars = /[\^$\\.*+?()[\]{}|]/;

exports.regexpQuantifiers = /[*+?{]/;

exports.regexpControlEscapes = /[fnrtv]/;

exports.regexpCharClasses = /[dDsSwW]/;

// Identifies the "dot" atom in regular expressions
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/parser.js
Expand Up @@ -700,6 +700,20 @@ exports.regexp.basic = function (test) {
.addError(1, 9, "Invalid regular expression.")
.test("var a = /.*/ii;");

TestRun(test, "Invalid Decimal Escape Sequence tolerated without `u` flag")
.test([
"void /\\00/;",
"void /\\01/;",
"void /\\02/;",
"void /\\03/;",
"void /\\04/;",
"void /\\05/;",
"void /\\06/;",
"void /\\07/;",
"void /\\08/;",
"void /\\09/;"
]);

test.done();
};

Expand Down Expand Up @@ -816,6 +830,29 @@ exports.regexp.uFlag = function (test) {
TestRun(test)
.test('void /[\\s0-1\\s2-3\\s]/u;', { esversion: 6 });

TestRun(test, "Null CharacterEscape")
.test([
"void /\\0/u;",
"void /\\0a/u;"
], { esversion: 6 });

TestRun(test)
.addError(1, 6, "Invalid regular expression.")
.test("void /\\00/u;", { esversion: 6 });

TestRun(test)
.addError(1, 6, "Invalid regular expression.")
.test("void /\\01/u;", { esversion: 6 });

TestRun(test, "ControlEscape")
.test([
"void /\\f/u;",
"void /\\n/u;",
"void /\\r/u;",
"void /\\t/u;",
"void /\\v/u;"
], { esversion: 6 });

test.done();
};

Expand Down

0 comments on commit 5ac5c46

Please sign in to comment.