coderaiser / putout

🐊 Pluggable and configurable JavaScript Linter, code transformer and formatter, drop-in ESLint superpower replacement πŸ’ͺ with built-in support for js, jsx, typescript, flow, markdown, yaml and json. Write declarative codemods in a simplest possible way 😏
https://putout.cloudcmd.io/
MIT License
698 stars 40 forks source link

[types/convert-typeof-to-is-type]: Does not fix `typeof a === 'object'` #178

Closed ElPrudi closed 1 year ago

ElPrudi commented 1 year ago

It seems you forgot another typeof type check to fix in this rule. This can be easily fixed in JS:

const isObj = (a) => typeof a === 'object'

and TS:

const isObj = (a): a is Record<any, any> => typeof a === 'object'
coderaiser commented 1 year ago

That’s a good idea for a PR :)

coderaiser commented 1 year ago

This is absolutely same as in https://github.com/coderaiser/putout/issues/158: c23fa1e, dfc5954, 2540ecf

coderaiser commented 1 year ago

@ElPrudi how do you think checking of type should look like?

https://github.com/coderaiser/putout/blob/9461e44d04fb7ec2f0e9cc7162301c64b7c6b9af/packages/plugin-types/lib/declare/index.js#L9

types/declare already supports isObject(), but we should remember that typeof null === 'object', so check should be more sophisticated, and I’m not sure a lot people checks object type in a similar way, what do you think?

And same question about !isObject() to what code is should be applied?

Maybe !a || type of !== 'object', I’m not sure it will be useful transformation. How do you check that variable is object?

For example, I just use isObject() and IDE (or putout β€”-fix) will write implementation for me (same with 🐊Putout Editor, by the way, it will just declare it, and it will be used in transformation code)

coderaiser commented 1 year ago

Closed due to a long time of inactivity 🏝 Feel free to reopen when you have new ideas.