Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RegExp literal fixes #264

Merged
merged 3 commits into from
Nov 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,30 @@ function transform(code) {
}

describe("transform-regexp-constructors-plugin", () => {
it("should not duplicate forward-slash escapes", () => {
const source = String.raw`var x = new RegExp('\\/');`;
const expected = String.raw`var x = /\//;`;
expect(transform(source)).toBe(expected);
});

it("should transform newlines fine", () => {
const source = String.raw`var x = new RegExp('\\n');`;
const expected = String.raw`var x = /\n/;`;
expect(transform(source)).toBe(expected);
});

it("should transform RegExp constructors with string literals", () => {
const source = "var x = new RegExp('ab+c');";
const expected = "var x = /ab+c/;";
expect(transform(source)).toBe(expected);
});

it("should transform RegExp calls with string literals", () => {
const source = "var x = RegExp('ab+c');";
const expected = "var x = /ab+c/;";
expect(transform(source)).toBe(expected);
});

it("should transform RegExp constructors with flags", () => {
const source = "var x = new RegExp('ab+c', 'gimuy');";
const expected = "var x = /ab+c/gimuy;";
Expand Down Expand Up @@ -60,9 +78,15 @@ const ret = /ab+c\\wd/g;`;
expect(transform(source)).toBe(expected);
});

it("should escape invalid chars", () => {
const source = "var x = new RegExp('\\r\\n\\n/x/')";
const expected = "var x = /\\r\\n\\n\\/x\\//;";
it("should prettify special whitespaces", () => {
const source = String.raw`var x = new RegExp('\b\f\v\t\r\n\n');`;
const expected = String.raw`var x = /[\b]\f\v\t\r\n\n/;`;
expect(transform(source)).toBe(expected);
});

it("should escape forward slashes", () => {
const source = String.raw`var x = new RegExp('/x/');`;
const expected = String.raw`var x = /\/x\//;`;
expect(transform(source)).toBe(expected);
});
});
65 changes: 41 additions & 24 deletions packages/babel-plugin-transform-regexp-constructors/src/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,51 @@
"use strict";

function createRegExpLiteral(args, prettify, t) {
const evaluatedArgs = args.map((a) => a.evaluate());
if (!evaluatedArgs.every((a) => a.confident === true &&
typeof a.value === "string")) {
return;
}
let pattern = (evaluatedArgs.length >= 1 &&
evaluatedArgs[0].value !== "") ?
evaluatedArgs[0].value :
"(?:)";
const flags = evaluatedArgs.length >= 2 ?
evaluatedArgs[1].value :
"";

pattern = new RegExp(pattern).source;
if (prettyify) {
pattern = pattern.replace(/\n/g, "\\n")
.replace(/[\b]/g, "[\\b]")
.replace(/\v/g, "\\v")
.replace(/\f/g, "\\f")
.replace(/\r/g, "\\r");
}
return t.regExpLiteral(pattern, flags);
}

function maybeReplaceWithRegExpLiteral(path, t) {
if (!t.isIdentifier(path.node.callee, {name: "RegExp"})) {
return;
}
const regExpLiteral = createRegExpLiteral(path.get("arguments"), true, t);
if (regExpLiteral) {
path.replaceWith(regExpLiteral);
}
}

module.exports = function({ types: t }) {
return {
name: "transform-regexp-constructors",
visitor: {
NewExpression(path) {
if (!t.isIdentifier(path.node.callee, {name: "RegExp"})) {
return;
}
const evaluatedArgs = path.get("arguments")
.map((a) => a.evaluate());
if (!evaluatedArgs.every((a) => a.confident === true &&
typeof a.value === "string")) {
return;
}
let pattern = (evaluatedArgs.length >= 1 &&
evaluatedArgs[0].value !== "") ?
evaluatedArgs[0].value :
"(?:)";
const flags = evaluatedArgs.length >= 2 ?
evaluatedArgs[1].value :
"";

pattern = pattern
.replace(/\n/g, "\\n")
.replace(/\r/g, "\\r")
.replace(/\//g, "\\/");

path.replaceWith(t.regExpLiteral(pattern, flags));
}
maybeReplaceWithRegExpLiteral(path, t);
},
CallExpression(path) {
// equivalent to `new RegExp()` according to §21.2.3
maybeReplaceWithRegExpLiteral(path, t);
},
},
};
};