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 partially fixed conditions #176

Closed ElPrudi closed 1 year ago

ElPrudi commented 1 year ago

There is an edge case where this plugin fails:

function test(a: any, b: any) {
    if (typeof a === 'number' && typeof b === 'number') return a + b

    return undefined
}

will turn into this:

const isNumber = (a): a is number => typeof 'number'

function test(a: any, b: any) {
    if (isNumber(a) && isNumber(b)) return a + b

    return undefined
}

But, if I change one condition back:

const isNumber = (a): a is number => typeof 'number'

function test(a: any, b: any) {
    if (isNumber(a) && typeof b === 'number') return a + b // no eslint issues

    return undefined
}

the plugin can not detect typeof b === 'number' as the first part is already fixed. It will only fix it again, if you remove the generated guard.

coderaiser commented 1 year ago

Just fixed 🎉 . Is it works for you?

ElPrudi commented 1 year ago

Yes, it works now. Thank you very much :)