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/__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/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 da4865a08..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"); @@ -92,7 +94,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?