Skip to content

Commit

Permalink
Fix: Convert RegExpLieteral value to RegExp object (fixes babel#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
soda0289 committed May 28, 2017
1 parent 94bb5a1 commit 523f127
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion babylon-to-espree/toAST.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ var astTransformVisitor = {
if (path.isRegExpLiteral()) {
node.type = "Literal";
node.raw = node.extra.raw;
node.value = {};
try {
node.value = new RegExp(node.pattern, node.flags);
} catch (err) {
node.value = null;
}
node.regex = {
pattern: node.pattern,
flags: node.flags
Expand Down
14 changes: 14 additions & 0 deletions test/babel-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function assertImplementsAST(target, source, path) {
var typeB = source === null ? "null" : typeof source;
if (typeA !== typeB) {
error(`have different types (${typeA} !== ${typeB}) (${target} !== ${source})`);
} else if (typeA === "object" && ["RegExp"].indexOf(target.constructor.name) !== -1 && target.constructor.name !== source.constructor.name) {
error(`object have different constructors (${target.constructor.name} !== ${source.constructor.name}`);
} else if (typeA === "object") {
var keysTarget = Object.keys(target);
for (var i in keysTarget) {
Expand Down Expand Up @@ -305,6 +307,18 @@ describe("babylon-to-esprima", () => {
parseAndAssertSame("/affix-top|affix-bottom|affix|[a-z]/");
});

it("regexp", () => {
parseAndAssertSame("const foo = /foo/;");
});

it("regexp y flag", () => {
parseAndAssertSame("const foo = /foo/y;");
});

it("regexp u flag", () => {
parseAndAssertSame("const foo = /foo/u;");
});

it("regexp in a template string", () => {
parseAndAssertSame("`${/\\d/.exec(\"1\")[0]}`");
});
Expand Down

0 comments on commit 523f127

Please sign in to comment.