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

Fix path.evaluate for runtime errors in constant-folding #246

Merged
merged 2 commits into from
Nov 3, 2016
Merged
Changes from all 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
4 changes: 4 additions & 0 deletions packages/babel-helper-evaluate-path/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src
__tests__
node_modules
*.log
9 changes: 9 additions & 0 deletions packages/babel-helper-evaluate-path/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# # babel-helper-evaluate-path

`path.evaluate` wrapped in a try catch

## Installation

```sh
$ npm install babel-helper-evaluate-path
```
17 changes: 17 additions & 0 deletions packages/babel-helper-evaluate-path/package.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
10 changes: 10 additions & 0 deletions packages/babel-helper-evaluate-path/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = function evaluate(path) {
try {
return path.evaluate();
} catch (e) {
return {
confident: false,
error: e
};
}
};
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@
"keywords": [
"babel-plugin"
],
"dependencies": {},
"dependencies": {
"babel-helper-evaluate-path": "^0.0.1"
},
"devDependencies": {}
}
Original file line number Diff line number Diff line change
@@ -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?