Repository: babel/babili (js)

Issue:
Don't use path.replaceWith() with a source string, use path.replaceWithSourceString()
ERROR in unknown: Don't use `path.replaceWith()` with a source string, use `path.replaceWithSourceString()`

Seems to be the result of this change : 
https://github.com/babel/babili/pull/543/files

Pull request description:
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. Fixes #576.

---

I just noticed this:
```js
[foo, bar].toString() // is longer than
[foo, bar].join(',')
```

Patch:
diff --git a/packages/babel-plugin-minify-constant-folding/src/index.js b/packages/babel-plugin-minify-constant-folding/src/index.js
index 782d76b..da5b16e 100644
--- a/packages/babel-plugin-minify-constant-folding/src/index.js
+++ b/packages/babel-plugin-minify-constant-folding/src/index.js
@@ -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 {


Test patch:
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 ca2e633..352027f 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
@@ -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);
+  });
 });