eslint-plugin-unicorn
eslint-plugin-unicorn copied to clipboard
Rule proposal: `no-instanceof-function`
Description
similar to https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/no-instanceof-array.md
Fail
foo instanceof Function
Pass
typeof foo === 'function'
Additional Info
No response
another case but to forbid instanceof Object
checks in favor truthy check + typeof
Fail
let foo = {}
foo instanceof Object // true
let bar = null
bar instanceof Object // false
Pass
Boolean(foo) && typeof foo === 'object'
Boolean(bar) && typeof bar === 'object'
Would make it a generic no-instanceof
with default config like ["Array", "Function"]
.
Would make it a generic no-instanceof with default config like ["Array", "Function"].
👍
Accepted.
@dimaMachina From my point of view, foo installceof Object
is better than Boolean(foo) && typeof foo === 'object'
. People usually forget to add Boolean(foo)
. Moreover, non-object judgement will be verbose, like if (!(Boolean(foo) && typeof foo === 'object')){}
vs if (!(foo instanceof Object)){}
. Obviously, the instanceof
wins.
instanceof
should be avoided for built-ins as it does not work across realms (iframes, Node.js VM, etc).
instanceof
should be avoided for built-ins as it does not work across realms (iframes, Node.js VM, etc).
Thanks! instanceof
seems not safe in some edge case. Personally,I seldom use instanceof
. I think we should add a new rule no-instanceof
. The default option is all
. User can config its options to ['Array','Function','Object']
manually.