Skip to content

Commit

Permalink
Fix #576 (#587)
Browse files Browse the repository at this point in the history
* Should fix #576

People were calling `"foo".toString() or ["foo", "bar"].toString()`, which returned the real JS string `"[object Object]"`, the stringified version of the replacements object.

* Prettier code means prettier green checkmarks

* Testing testing 123

* Forgot the parens around the object literal

* Semicolons!

* npm run format
  • Loading branch information
j-f1 authored and vigneshshanmugam committed Jun 19, 2017
1 parent ba29bc5 commit 38b7740
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,18 @@ describe("constant-folding-plugin", () => {
);
expect(transform(source)).toBe(expected);
});

it("shouldn’t crash on toString() calls or accesses", () => {
const source = unpad(
`
"foo".toString();
["foo", "bar"].toString();
({}).toString();
"foo".toString;
["foo", "bar"].toString;
({}).toString;
`
);
expect(transform(source)).toBe(source);
});
});
5 changes: 4 additions & 1 deletion packages/babel-plugin-minify-constant-folding/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ function swap(path, member, handlers, ...args) {
const key = getName(member);
if (key === undefined) return;
let handler = handlers[key];
if (typeof handler !== "function") {
if (
typeof handler !== "function" ||
!Object.hasOwnProperty.call(handlers, key)
) {
if (typeof handlers[FALLBACK_HANDLER] === "function") {
handler = handlers[FALLBACK_HANDLER].bind(member.object, key);
} else {
Expand Down

0 comments on commit 38b7740

Please # to comment.