From bda4c1d1846a484f3f4af714cbef0ac9ef2a174c Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Thu, 3 Nov 2016 00:30:17 +0100 Subject: [PATCH 1/2] Fix path.evaluate for runtime errors in constant-folding (close #245) --- .../__tests__/constant-folding-test.js | 11 +++++++++++ .../src/index.js | 13 ++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) 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 9d54194b1..30fe72afc 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 @@ -49,4 +49,15 @@ describe("constant-folding-plugin", () => { `); expect(transform(source)).toBe(expected); }); + + it("should handle runtime errors", () => { + const source = unpad(` + try { + x({ + toString: 0 + } + ''); + } catch (e) {} + `); + expect(transform(source)).toBe(source); + }); }); diff --git a/packages/babel-plugin-minify-constant-folding/src/index.js b/packages/babel-plugin-minify-constant-folding/src/index.js index da4865a08..4462037bc 100644 --- a/packages/babel-plugin-minify-constant-folding/src/index.js +++ b/packages/babel-plugin-minify-constant-folding/src/index.js @@ -92,7 +92,7 @@ module.exports = ({ types: t, traverse }) => { return; } - const res = path.evaluate(); + const res = evaluate(path); if (res.confident) { // Avoid fractions because they can be longer than the original expression. // There is also issues with number percision? @@ -118,3 +118,14 @@ module.exports = ({ types: t, traverse }) => { }, }; }; + +function evaluate(path) { + try { + return path.evaluate(); + } catch (e) { + return { + confident: false, + error: e + }; + } +} From de198a9d44bd6c2106548c870f6ca8aa4b736ed9 Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Thu, 3 Nov 2016 01:01:38 +0100 Subject: [PATCH 2/2] Move path.evaluate to separate helper --- packages/babel-helper-evaluate-path/.npmignore | 4 ++++ packages/babel-helper-evaluate-path/README.md | 9 +++++++++ .../babel-helper-evaluate-path/package.json | 17 +++++++++++++++++ .../babel-helper-evaluate-path/src/index.js | 10 ++++++++++ .../package.json | 4 +++- .../src/index.js | 13 ++----------- 6 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 packages/babel-helper-evaluate-path/.npmignore create mode 100644 packages/babel-helper-evaluate-path/README.md create mode 100644 packages/babel-helper-evaluate-path/package.json create mode 100644 packages/babel-helper-evaluate-path/src/index.js diff --git a/packages/babel-helper-evaluate-path/.npmignore b/packages/babel-helper-evaluate-path/.npmignore new file mode 100644 index 000000000..22250660e --- /dev/null +++ b/packages/babel-helper-evaluate-path/.npmignore @@ -0,0 +1,4 @@ +src +__tests__ +node_modules +*.log diff --git a/packages/babel-helper-evaluate-path/README.md b/packages/babel-helper-evaluate-path/README.md new file mode 100644 index 000000000..4d1cef9ca --- /dev/null +++ b/packages/babel-helper-evaluate-path/README.md @@ -0,0 +1,9 @@ +# # babel-helper-evaluate-path + +`path.evaluate` wrapped in a try catch + +## Installation + +```sh +$ npm install babel-helper-evaluate-path +``` diff --git a/packages/babel-helper-evaluate-path/package.json b/packages/babel-helper-evaluate-path/package.json new file mode 100644 index 000000000..f561078f1 --- /dev/null +++ b/packages/babel-helper-evaluate-path/package.json @@ -0,0 +1,17 @@ +{ + "name": "babel-helper-evaluate-path", + "version": "0.0.1", + "description": "path.evaluate wrapped in a try catch", + "homepage": "https://github.com/babel/babili#readme", + "repository": "https://github.com/babel/babili/tree/master/packages/babel-helper-evaluate-path", + "bugs": "https://github.com/babel/babili/issues", + "author": "boopathi", + "license": "MIT", + "main": "lib/index.js", + "keywords": [ + "babel-plugin", + "babili" + ], + "dependencies": {}, + "devDependencies": {} +} diff --git a/packages/babel-helper-evaluate-path/src/index.js b/packages/babel-helper-evaluate-path/src/index.js new file mode 100644 index 000000000..55bfb892a --- /dev/null +++ b/packages/babel-helper-evaluate-path/src/index.js @@ -0,0 +1,10 @@ +module.exports = function evaluate(path) { + try { + return path.evaluate(); + } catch (e) { + return { + confident: false, + error: e + }; + } +}; diff --git a/packages/babel-plugin-minify-constant-folding/package.json b/packages/babel-plugin-minify-constant-folding/package.json index 79f2db9c0..369d79c49 100644 --- a/packages/babel-plugin-minify-constant-folding/package.json +++ b/packages/babel-plugin-minify-constant-folding/package.json @@ -11,6 +11,8 @@ "keywords": [ "babel-plugin" ], - "dependencies": {}, + "dependencies": { + "babel-helper-evaluate-path": "^0.0.1" + }, "devDependencies": {} } diff --git a/packages/babel-plugin-minify-constant-folding/src/index.js b/packages/babel-plugin-minify-constant-folding/src/index.js index 4462037bc..a9ea903e6 100644 --- a/packages/babel-plugin-minify-constant-folding/src/index.js +++ b/packages/babel-plugin-minify-constant-folding/src/index.js @@ -1,5 +1,7 @@ "use strict"; +const evaluate = require("babel-helper-evaluate-path"); + module.exports = ({ types: t, traverse }) => { const seen = Symbol("seen"); @@ -118,14 +120,3 @@ module.exports = ({ types: t, traverse }) => { }, }; }; - -function evaluate(path) { - try { - return path.evaluate(); - } catch (e) { - return { - confident: false, - error: e - }; - } -}