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
705 stars 40 forks source link

[types/convert-typeof-to-is-type]: Function insertion doubles the class comment. #164

Closed ElPrudi closed 1 year ago

ElPrudi commented 1 year ago

Every time this rule is triggered and inserts a function into the file where a class is, the class comment gets duplicated.

For example: X.ts:

/**
 * This is class X
 */
export default class X {

   public a(b): string {
          if (typeof b === 'number') return 'doStuff'
          return 'stuff'
   }
}

gets transformed into


/**
 * This is class X
 */
const isNumber = (x) => typeof x === 'number'

/**
 * This is class X
 */
export default class X {

   public a(b): string {
          if (isNumber(x)) return 'doStuff'
          return 'stuff'
   }
}
coderaiser commented 1 year ago

Could you please show me your .putout.json?

ElPrudi commented 1 year ago

I use eslint-plugin-putout and this is the putout part of it:

{
    "putout/putout": [
        2,
        {
            "plugins": ["apply-shorthand-properties", "conditions", "simplify-ternary", "types"],
            "rules": {
                "apply-shorthand-properties": [
                    "on",
                    {
                        "ignore": []
                    }
                ],
                "split-variable-declarations": "off",
                "new/remove-useless": "off",
                "remove-unused-variables": "off",
                "remove-console": "off",
                "convert-template-to-string": "off",
                "remove-useless-array-constructor": "off",
                "types/remove-double-negations": "off",
                "promises/remove-useless-async": "off",
                "convert-object-assign-to-merge-spread": "off",
                "apply-destructuring/array": "off",
                "apply-destructuring/object": "off",
                "promises/add-missing-await": "off",
                "typescript/remove-useless-parens": "off",
                "remove-useless-arguments/method": "off",
                "math/apply-exponentiation": [
                    "on",
                    {
                        "exclude": ["__a * __a", "__a * __a * __a", "__a * __a * __a * __a", "__a * __a * __a * __a * __a"]
                    }
                ]
            }
        }
    ],
    "putout/align-spaces": 0,
    "putout/remove-newline-from-empty-object": 0,
    "putout/add-newline-after-function-call": 0,
    "putout/add-newline-before-function-call": 0,
    "putout/add-newline-before-return": 0,
    "putout/add-newlines-between-specifiers": 0,
    "putout/add-newlines-between-types-in-union": 0,
    "putout/array-element-newline": 0,
    "putout/function-declaration-paren-newline": 0,
    "putout/keyword-spacing": 0,
    "putout/long-properties-destructuring": 0,
    "putout/multiple-properties-destructuring": 0,
    "putout/newline-function-call-arguments": 0,
    "putout/nonblock-statement-body-newline": 0,
    "putout/nonblock-statement-body-position": 0,
    "putout/object-property-newline": 0
}
coderaiser commented 1 year ago

I can't reproduce this with 1.ts:

/**
 * This is class X
 */
export default class X {

   public a(b): string {
          if (typeof b === 'number') return 'doStuff'
          return 'stuff'
   }
}
putout 1.ts --fix

Produces this result:

const isNumber = (a) => typeof a === 'number';

/**
 * This is class X
 */
export default class X {
    public a(b): string {
        if (isNumber(b))
            return 'doStuff';

        return 'stuff';
    }
}
ElPrudi commented 1 year ago

Found it. It only duplicates it, if you also have imports before that. If the inserted function becomes the first line in the file, it doesn't duplicate it.

coderaiser commented 1 year ago

Fixed in v30.6.0. Please re-install 🐊.

Is it works for you?

ElPrudi commented 1 year ago

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