From ef45664980928b2614315ddd7202af2a9740570f Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Tue, 4 Apr 2017 00:34:35 +0200 Subject: [PATCH 1/2] Make jsesc isScriptContext optional + Adds option isScriptContext to constant folding plugin + Fix #440, fix #413 + Related #384, #382 --- .../__tests__/constant-folding-test.js | 22 +++++++++++++++---- .../src/index.js | 8 +++---- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js b/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js index c074df7e5..3991e16c4 100644 --- a/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js +++ b/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js @@ -3,9 +3,9 @@ jest.autoMockOff(); const babel = require("babel-core"); const unpad = require("../../../utils/unpad"); -function transform(code) { +function transform(code, opts = {}) { return babel.transform(code, { - plugins: [require("../src/index")] + plugins: [[require("../src/index"), opts]] }).code; } @@ -83,7 +83,7 @@ describe("constant-folding-plugin", () => { "<\\\\/script"; ` ); - expect(transform(source)).toBe(expected); + expect(transform(source, { isScriptContext: true })).toBe(expected); }); it("should handle style escape", () => { @@ -98,7 +98,7 @@ describe("constant-folding-plugin", () => { "<\\\\/style"; ` ); - expect(transform(source)).toBe(expected); + expect(transform(source, { isScriptContext: true })).toBe(expected); }); it("should handle html comment escape", () => { @@ -113,6 +113,20 @@ describe("constant-folding-plugin", () => { "\\\\x3C!--"; ` ); + expect(transform(source, { isScriptContext: true })).toBe(expected); + }); + + it("should fix #440", () => { + const source = unpad( + ` + var x = "'cool'" + "test"; + ` + ); + const expected = unpad( + ` + var x = "'cool'test"; + ` + ); expect(transform(source)).toBe(expected); }); }); diff --git a/packages/babel-plugin-minify-constant-folding/src/index.js b/packages/babel-plugin-minify-constant-folding/src/index.js index 76bf3f149..3594fae1c 100644 --- a/packages/babel-plugin-minify-constant-folding/src/index.js +++ b/packages/babel-plugin-minify-constant-folding/src/index.js @@ -55,7 +55,7 @@ module.exports = ({ types: t, traverse }) => { }, // TODO: look into evaluating binding too (could result in more code, but gzip?) - Expression(path) { + Expression(path, { opts: { isScriptContext = false } }) { const { node } = path; if (node[seen]) { @@ -122,10 +122,8 @@ module.exports = ({ types: t, traverse }) => { } // https://github.com/babel/babili/issues/382 - if (typeof res.value === "string") { - res.value = jsesc(res.value, { - isScriptContext: true - }); + if (typeof res.value === "string" && isScriptContext) { + res.value = jsesc(res.value, { isScriptContext }); } const node = t.valueToNode(res.value); From 58d3571158145f4c92424e167dbb3e2759bf7b9d Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Tue, 4 Apr 2017 14:06:05 +0200 Subject: [PATCH 2/2] Remove jsesc --- .../__tests__/constant-folding-test.js | 16 ++++++++-------- .../package.json | 6 ++---- .../src/index.js | 8 +------- 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js b/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js index 3991e16c4..7b0eb37ec 100644 --- a/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js +++ b/packages/babel-plugin-minify-constant-folding/__tests__/constant-folding-test.js @@ -3,9 +3,9 @@ jest.autoMockOff(); const babel = require("babel-core"); const unpad = require("../../../utils/unpad"); -function transform(code, opts = {}) { +function transform(code) { return babel.transform(code, { - plugins: [[require("../src/index"), opts]] + plugins: [require("../src/index")] }).code; } @@ -71,7 +71,7 @@ describe("constant-folding-plugin", () => { expect(transform(source)).toBe(source); }); - it("should handle script escape", () => { + xit("should handle script escape", () => { const source = unpad( ` " { "<\\\\/script"; ` ); - expect(transform(source, { isScriptContext: true })).toBe(expected); + expect(transform(source)).toBe(expected); }); - it("should handle style escape", () => { + xit("should handle style escape", () => { const source = unpad( ` " { "<\\\\/style"; ` ); - expect(transform(source, { isScriptContext: true })).toBe(expected); + expect(transform(source)).toBe(expected); }); - it("should handle html comment escape", () => { + xit("should handle html comment escape", () => { const source = unpad( ` " { "\\\\x3C!--"; ` ); - expect(transform(source, { isScriptContext: true })).toBe(expected); + expect(transform(source)).toBe(expected); }); it("should fix #440", () => { diff --git a/packages/babel-plugin-minify-constant-folding/package.json b/packages/babel-plugin-minify-constant-folding/package.json index 0a4581985..f7e1d0328 100644 --- a/packages/babel-plugin-minify-constant-folding/package.json +++ b/packages/babel-plugin-minify-constant-folding/package.json @@ -12,8 +12,6 @@ "babel-plugin" ], "dependencies": { - "babel-helper-evaluate-path": "^0.0.3", - "jsesc": "^2.4.0" - }, - "devDependencies": {} + "babel-helper-evaluate-path": "^0.0.3" + } } diff --git a/packages/babel-plugin-minify-constant-folding/src/index.js b/packages/babel-plugin-minify-constant-folding/src/index.js index 3594fae1c..f9826e5c7 100644 --- a/packages/babel-plugin-minify-constant-folding/src/index.js +++ b/packages/babel-plugin-minify-constant-folding/src/index.js @@ -1,7 +1,6 @@ "use strict"; const evaluate = require("babel-helper-evaluate-path"); -const jsesc = require("jsesc"); module.exports = ({ types: t, traverse }) => { const seen = Symbol("seen"); @@ -55,7 +54,7 @@ module.exports = ({ types: t, traverse }) => { }, // TODO: look into evaluating binding too (could result in more code, but gzip?) - Expression(path, { opts: { isScriptContext = false } }) { + Expression(path) { const { node } = path; if (node[seen]) { @@ -121,11 +120,6 @@ module.exports = ({ types: t, traverse }) => { } } - // https://github.com/babel/babili/issues/382 - if (typeof res.value === "string" && isScriptContext) { - res.value = jsesc(res.value, { isScriptContext }); - } - const node = t.valueToNode(res.value); node[seen] = true; path.replaceWith(node);