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 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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);
});
});
67 changes: 43 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,53 @@
"use strict";

function createRegExpLiteral(args, 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;
// This step is for prettification -- technically we can insert whitespace
// literals into a regExpLiteral just fine.
// e.g. '\t'.replace(/ /, '') === '\t'.replace(/\t/, '') === ''.
pattern = pattern.replace(/\n/g, "\\n")
.replace(/\t/g, "\\t")
.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"), 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);
},
},
};
};