Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: no-instanceof-function #2157

Open
dimaMachina opened this issue Jun 21, 2023 · 8 comments
Open

Rule proposal: no-instanceof-function #2157

dimaMachina opened this issue Jun 21, 2023 · 8 comments

Comments

@dimaMachina
Copy link
Contributor

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

@dimaMachina
Copy link
Contributor Author

dimaMachina commented Jun 21, 2023

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'

@silverwind
Copy link
Contributor

Would make it a generic no-instanceof with default config like ["Array", "Function"].

@sindresorhus
Copy link
Owner

Would make it a generic no-instanceof with default config like ["Array", "Function"].

👍

@sindresorhus
Copy link
Owner

Accepted.

@zanminkian
Copy link
Contributor

zanminkian commented Mar 9, 2024

@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.

@sindresorhus
Copy link
Owner

instanceof should be avoided for built-ins as it does not work across realms (iframes, Node.js VM, etc).

@zanminkian
Copy link
Contributor

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.

@zanminkian
Copy link
Contributor

This rule can be solved by #2452

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants