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

How to disable pretty printer #180

Closed edi9999 closed 1 year ago

edi9999 commented 1 year ago

Hi, I came to this library thanks to this article : https://javascript.plainenglish.io/declarative-codemods-f9958a99e85e

I have a project where I'd like to remove console.log quickly, and that's how I got to putout. (I could use regex but it would not catch multiline console.log).

Now I have achieved to remove the console.logs, using following .putout.json

{
    "rules": {
        "rm-console": "on",
        "remove-unused-variables": "off",
        "convert-to-arrow-function": "off",
        "strict-mode/add-missing": "off",
        "promises/convert-reject-to-throw": "off",
        "promises/add-missing-await": "off",
        "promises/remove-useless-async": "off",
        "apply-destructuring/array": "off",
        "remove-console": "off",
        "remove-useless-operand": "off",
        "merge-if-statements": "off",
        "nodejs/declare-after-require": "off",
        "new/remove-useless": "off",
        "conditions/apply-comparison-order": "off",
        "conditions/evaluate": "off",
        "conditions/remove-boolean": "off",
        "conditions/remove-zero": "off",
        "for-of/for-each": "off"
    }
}

And following plugin (~/.putout/rm-console)

'use strict';

module.exports.report = () => 'Do not use console.log';

module.exports.replace = () => ({
    'console.__a(__args)': '',
});

However, now that I run :

putout --fix file.js, it will also reformat the whole file, whereas I just wanted to remove console.log and have the formatting of the file kept as is.

Is there an option for that ?

Potentially, it could be : putout --no-reformat --fix file.js

edi9999 commented 1 year ago

Oh I think changing to the "recast" printer works better.

How can I have the printer respect my ".editorconfig" ?

I'd like to use tabs instead of spaces.

coderaiser commented 1 year ago

Hi, look there is a couple types of printers supported by 🐊Putout.

☝️ TLDR: Most likely you need to configure ESLint rule indent.

Printer

In the eyes of mercy, no one should have hateful thoughts. Feel pity for the man who is even more at fault. The area and size of mercy is limitless.

(c) Yamamoto Tsunetomo "Hagakure"

You have also ability to define printer of your choose, it can be:

@putout/printer used by default, if you want to set any other update .putout.json with:

{
    "printer": "recast"
}

@putout/printer:

recast:

babel:

You can choose any of them, but preferred is default printer.

edi9999 commented 1 year ago

Is there a way to have support for respecting .editorconfig ?

coderaiser commented 1 year ago

Is there a way to have support for respecting .editorconfig ?

No there is no reason to support this old format in code transformer. You have Pritter, ESLint and other tools for this purpose.

Anyways you can set formatting options in .putout.json. Each tool has it's own formatting options.

Here is @putout/printer example:

{
    "printer": ["putout", {
        "format": {
            "indent": "[tab-character-here]"
        }
    }]
}

And recast example:

{
    "printer": ["recast", {
        "useTabs": true
    }]
}

Reinstall 🐊Putout to get things working. Is it works for you?

edi9999 commented 1 year ago

The second one gives :

🐊 .putout.json: printer: must be string
edi9999 commented 1 year ago

The first option, when I put this in .putout.json :

{
    "printer": ["putout", {
        "format": {
            "indent": "[tab-character-here]"
        }
    }]
}

also prints :

.putout.json: printer: must be string
coderaiser commented 1 year ago

Just landed fix 🎉 ! Please re-install 🐊Putout. Is it works for you?

coderaiser commented 1 year ago

Oh I think changing to the "recast" printer works better.

Could you please provide me an example where recast printer works better?

edi9999 commented 1 year ago

The following code is transformed when I run putout --fix :

function getResolvedId(part, options) {
    return (
        options.filePath +
        "@" +
        part.lIndex.toString() +
        "-" +
        options.scopeManager.scopePathItem.join("-")
    );
}

With the "putout" printer :

function getResolvedId(part, options) {
    return (options.filePath + "@" + part.lIndex.toString() +
    "-" + options.scopeManager.scopePathItem.join("-"));
}

and with the "recast" printer :

function getResolvedId(part, options) {
    return (options.filePath + '@' + part.lIndex.toString() + '-' + options.scopeManager.scopePathItem.join('-'));
}

What I explicilty would like is to keep the same formatting (just remove the console.log, not do anything else to the code).

coderaiser commented 1 year ago

What I explicilty would like is to keep the same formatting (just remove the console.log, not do anything else to the code).

Yes, recast tries to guess the correct formatting and sometimes it is right (that's why it is still supported), for other cases there is ESLint and Prettier.

edi9999 commented 1 year ago

I'm coming back to this article :

https://javascript.plainenglish.io/declarative-codemods-f9958a99e85e

And I find that the content is quite misleading.

From what you read, you think that putout is new "codemod" tool, which you can use for a single task.

Actually, it is more of a framework and you can't use it like jscodeshift, just for one purpose without reformatting your own file.

Maybe I'll migrate from eslint to putout one day if the project continues to be maintained.

For the moment, for my task of removing console.log, I will be using this jscodeshift script :

jscodeshift index.js --transform ~/.codeshifts/remove-console.js

~/.codeshifts/remove-console.js

export default (fileInfo, api) => {
  const j = api.jscodeshift;

  const root = j(fileInfo.source)

  const callExpressions = root.find(j.CallExpression, {
      callee: {
        type: 'MemberExpression',
        object: { type: 'Identifier', name: 'console' },
      },
    }
  );

  callExpressions.remove();

  return root.toSource();
};
coderaiser commented 1 year ago

You can use whatever tool you want, jscodeshift also uses recast inside of itself (and it's not fun at all maintaining its fork).

For you case of removing console.log you can use:

putout --disable-all .
putout --enable remove-console
putout --fix .

With printer set to recast you will achieve the same results as with jscodeshift (which is not really developed anymore).

And you don't have to switch to 🐊Putout from ESLint, since the rules (mostly) not overlap. Just configure ESLint for appearance and use both tools, that's it.