Skip to content

Commit

Permalink
Fix path.evaluate for runtime errors in constant-folding (close #245)
Browse files Browse the repository at this point in the history
  • Loading branch information
boopathi committed Nov 2, 2016
1 parent eb9df7a commit bda4c1d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
13 changes: 12 additions & 1 deletion packages/babel-plugin-minify-constant-folding/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -118,3 +118,14 @@ module.exports = ({ types: t, traverse }) => {
},
};
};

function evaluate(path) {
try {
return path.evaluate();
} catch (e) {
return {
confident: false,
error: e
};
}
}

0 comments on commit bda4c1d

Please sign in to comment.