From 747afaa59e20b57a561404306cf4dc83dd1c7798 Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Fri, 4 Nov 2016 14:40:26 -0700 Subject: [PATCH 1/2] Escape newline in regex optimization --- .../__tests__/transform-regexp-constructors-test.js | 6 ++++++ .../src/index.js | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/babel-plugin-transform-regexp-constructors/__tests__/transform-regexp-constructors-test.js b/packages/babel-plugin-transform-regexp-constructors/__tests__/transform-regexp-constructors-test.js index 9f8a64b81..e3f35382e 100644 --- a/packages/babel-plugin-transform-regexp-constructors/__tests__/transform-regexp-constructors-test.js +++ b/packages/babel-plugin-transform-regexp-constructors/__tests__/transform-regexp-constructors-test.js @@ -59,4 +59,10 @@ const flags = "g"; const ret = /ab+c\\wd/g;`; expect(transform(source)).toBe(expected); }); + + it("should work with newlines", () => { + const source = "var x = new RegExp('\\r\\n\\n')"; + const expected = "var x = /\\r\\n\\n/;"; + expect(transform(source)).toBe(expected); + }); }); diff --git a/packages/babel-plugin-transform-regexp-constructors/src/index.js b/packages/babel-plugin-transform-regexp-constructors/src/index.js index 469918adf..6cd66dc8d 100644 --- a/packages/babel-plugin-transform-regexp-constructors/src/index.js +++ b/packages/babel-plugin-transform-regexp-constructors/src/index.js @@ -14,13 +14,18 @@ module.exports = function({ types: t }) { typeof a.value === "string")) { return; } - const pattern = (evaluatedArgs.length >= 1 && + 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'); + path.replaceWith(t.regExpLiteral(pattern, flags)); } }, From 9e63cce129468a440a717e95e7c46728b54895ce Mon Sep 17 00:00:00 2001 From: Juriy Zaytsev Date: Fri, 4 Nov 2016 15:22:55 -0700 Subject: [PATCH 2/2] Add slash --- .../__tests__/transform-regexp-constructors-test.js | 6 +++--- .../babel-plugin-transform-regexp-constructors/src/index.js | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/babel-plugin-transform-regexp-constructors/__tests__/transform-regexp-constructors-test.js b/packages/babel-plugin-transform-regexp-constructors/__tests__/transform-regexp-constructors-test.js index e3f35382e..eb0744723 100644 --- a/packages/babel-plugin-transform-regexp-constructors/__tests__/transform-regexp-constructors-test.js +++ b/packages/babel-plugin-transform-regexp-constructors/__tests__/transform-regexp-constructors-test.js @@ -60,9 +60,9 @@ const ret = /ab+c\\wd/g;`; expect(transform(source)).toBe(expected); }); - it("should work with newlines", () => { - const source = "var x = new RegExp('\\r\\n\\n')"; - const expected = "var x = /\\r\\n\\n/;"; + it("should escape invalid chars", () => { + const source = "var x = new RegExp('\\r\\n\\n/x/')"; + const expected = "var x = /\\r\\n\\n\\/x\\//;"; expect(transform(source)).toBe(expected); }); }); diff --git a/packages/babel-plugin-transform-regexp-constructors/src/index.js b/packages/babel-plugin-transform-regexp-constructors/src/index.js index 6cd66dc8d..29e62714a 100644 --- a/packages/babel-plugin-transform-regexp-constructors/src/index.js +++ b/packages/babel-plugin-transform-regexp-constructors/src/index.js @@ -24,7 +24,8 @@ module.exports = function({ types: t }) { pattern = pattern .replace(/\n/g, '\\n') - .replace(/\r/g, '\\r'); + .replace(/\r/g, '\\r') + .replace(/\//g, '\\/'); path.replaceWith(t.regExpLiteral(pattern, flags)); }