Fix: Convert RegExpLieteral value to RegExp object (fixes babel/babel-eslint#477) (babel/babel-eslint#478)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
@@ -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]}`");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user