Skip to content

Commit

Permalink
[[FIX]] Support y RegExp flag in ES2015 code (#2999)
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike authored and rwaldron committed Aug 15, 2016
1 parent 17b71b5 commit a801433
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/lex.js
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ Lexer.prototype = {
var flags = [];
var malformed = false;
var isCharSet = false;
var terminated;
var terminated, malformedDesc;

var scanUnexpectedChars = function() {
// Unexpected control character
Expand Down Expand Up @@ -1394,10 +1394,24 @@ Lexer.prototype = {

while (index < length) {
char = this.peek(index);
if (!/[gim]/.test(char)) {
if (!/[gimy]/.test(char)) {
break;
}
flags.push(char);
if (char === "y") {
if (!state.inES6(true)) {
this.trigger("warning", {
code: "W119",
line: this.line,
character: this.char,
data: [ "Sticky RegExp flag", "6" ]
});
}
if (value.indexOf("y") > -1) {
malformedDesc = "Duplicate RegExp flag";
}
} else {
flags.push(char);
}
value += char;
index += 1;
}
Expand All @@ -1407,12 +1421,21 @@ Lexer.prototype = {
try {
new RegExp(body, flags.join(""));
} catch (err) {
/**
* Because JSHint relies on the current engine's RegExp parser to
* validate RegExp literals, the description (exposed as the "data"
* property on the error object) is platform dependent.
*/
malformedDesc = err.message;
}

if (malformedDesc) {
malformed = true;
this.trigger("error", {
code: "E016",
line: this.line,
character: this.char,
data: [ err.message ] // Platform dependent!
data: [ malformedDesc ]
});
}

Expand Down
28 changes: 28 additions & 0 deletions tests/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,34 @@ exports.testRegexRegressions = function (test) {
test.done();
};

exports.regexpSticky = function (test) {
TestRun(test)
.addError(1, "'Sticky RegExp flag' is only available in ES6 (use 'esversion: 6').")
.test("var exp = /./y;", { esversion: 5 });

TestRun(test).test("var exp = /./y;", { esversion: 6 });
TestRun(test).test("var exp = /./gy;", { esversion: 6 });
TestRun(test).test("var exp = /./yg;", { esversion: 6 });

TestRun(test, "Invalid due to repetition")
.addError(1, "Invalid regular expression.")
.addError(2, "Invalid regular expression.")
.test([
"var exp = /./yy;",
"var exp = /./ygy;"
], { esversion: 6 });

TestRun(test, "Invalid due to other conditions")
.addError(1, "Invalid regular expression.")
.addError(2, "Invalid regular expression.")
.test([
"var exp = /./gyg;",
"var exp = /?/y;"
] , { esversion: 6 });

test.done();
};

exports.strings = function (test) {
var code = [
"var a = '\u0012\\r';",
Expand Down

0 comments on commit a801433

Please sign in to comment.