Skip to content

Commit

Permalink
Fix: Don't allow regex y flag in ES5
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Dec 10, 2015
1 parent 3155ced commit 73a079c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
24 changes: 23 additions & 1 deletion espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ function isValidToken(parser) {
case tt.jsxTagEnd:
return ecma.jsx;

// https://github.com/ternjs/acorn/issues/363
case tt.regexp:
if (extra.ecmaVersion < 6 && parser.value.flags && parser.value.flags.indexOf("y") > -1) {
return false;
}

return true;

default:
return true;
}
Expand Down Expand Up @@ -427,7 +435,7 @@ function tokenize(code, options) {
options = options || {};

var acornOptions = {
ecmaVersion: 6
ecmaVersion: 5
};

resetExtra();
Expand All @@ -453,6 +461,20 @@ function tokenize(code, options) {

extra.tolerant = typeof options.tolerant === "boolean" && options.tolerant;

if (typeof options.ecmaVersion === "number") {
switch (options.ecmaVersion) {
case 3:
case 5:
case 6:
acornOptions.ecmaVersion = options.ecmaVersion;
extra.ecmaVersion = options.ecmaVersion;
break;

default:
throw new Error("ecmaVersion must be 3, 5, or 6.");
}
}

// apply parsing flags
if (options.ecmaFeatures && typeof options.ecmaFeatures === "object") {
extra.ecmaFeatures = options.ecmaFeatures;
Expand Down
18 changes: 18 additions & 0 deletions tests/lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ describe("parse()", function() {

});

describe("ES5", function() {

it("should throw an error when using the y regex flag", function() {

assert.throws(function() {
espree.parse("/./y");
});
});

it("should throw an error when using the u regex flag", function() {

assert.throws(function() {
espree.parse("/./u");
});
});

});

describe("general", function() {
it("should output tokens, comments, locs, and ranges when called with those options", function() {
var ast = espree.parse("let foo = bar;", {
Expand Down

0 comments on commit 73a079c

Please sign in to comment.