es-tooling / module-replacements-codemods

MIT License
184 stars 24 forks source link

feat: add codemod for `is-plain-object` #55

Closed babs20 closed 1 month ago

babs20 commented 1 month ago

Codemod for is-plain-object using a slightly modified replacement from the module-replacements repo. I left an issue over there to address an edge case I encountered while creating this codemod. I am 99% confident in my variation, which matches the package's original behavior.

babs20 commented 1 month ago

Updated this to:

  1. Use an IIFE so we can use a cached version of the value we are checking.
  2. Use the updated suggested replacement from the module-replacements repo

Also, it seems that there is a bit of different behavior from the is-plain-object package compared to the suggested replacement. For example:

const { isPlainObject } = require('is-plain-object')

const arg = Object.create({});
console.log(isPlainObject(arg));
//=> true
console.log(arg && typeof arg === "object" && (Object.getPrototypeOf(arg) === null || Object.getPrototypeOf(arg) === Object.prototype))
//=> false

It appears to me that the suggested replacement from the module-replacements repo creates the "correct" output, so maybe down the road as this package matures we will need to be clear that this codemod (and possibly some others) could change the behavior of the code they are replacing.

thepassle commented 1 month ago

This LGTM, @zloirock what do you think?

thepassle commented 1 month ago

Nice work and great collaboration guys!