coderaiser/putout

How to disable pretty printer

edi9999 opened this issue ยท 12 comments

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

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.

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:

  • โœ… much simpler in support then recast;
  • โœ… opinionated and has good defaults;
  • โœ… produces code like it was processed by ESLint;
  • โœ… twice faster then recast;

recast:

  • โœ… tryies to preserve formatting, and in 70% succeeded;
  • โŒ for other cases you need eslint-plugin-putout;
  • โŒ slowest printer: involves additional parser + sophisticated printer;

babel:

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

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?

The second one gives :

๐ŸŠ .putout.json: printer: must be string

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

Just landed fix ๐ŸŽ‰ ! Please re-install ๐ŸŠPutout. Is it works for you?

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

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

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

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.

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();
};

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.