putout v19.3.0
--fix cannot be used with Ruler toggler
Just landed additional check of user input when running command to set baseline disabling all found rules --disable-all and other ruler options.
π€·ββοΈ What is Ruler?
When you need to change .putout.json you can do it not only manually editing the file in an editor, but with help of Ruler as well.
Ruler can enable one rule with putout --enable convert-commonjs-to-esm or disable all rules with putout --disable-all. But it should never be used with --fix, because it's not clear what should be done. Actually what is done is changes made to .putout.json but putout doesn't see them and works with older version of config what can be confusing.
Of course this should be documented. But much better way would be to help user with detailed explanation from used application, so there was no need to read the documentation :).
π€·ββοΈ How ruler can be helpful to me?
You may want to convert your CommonJS module into Ecma Script Modules. Right now this is a trend started by @sindresorhus.
Convert CommonJS to ESM
Let's suppose you have a file called index.js:
const unused = 5;
module.exports = function() {
return promise();
}
async function promise(a) {
return Promise.reject(Error('x'));
}You want to convert it to ESM, and everything else keep untouched. You can do this with a Ruler. So you disable all rules that Putout can find right now.
putout index.js --disable-all will find next errors:
1:4 error "unused" is defined but never used remove-unused-variables
7:23 error "a" is defined but never used remove-unused-variables
3:0 error Arrow functions should be used convert-to-arrow-function
1:0 error "use strict" directive should be on top of commonjs file strict-mode/add
8:4 error Reject is useless in async functions, use throw instead promises/convert-reject-to-throw
4:11 error Async functions should be called using await promises/add-missing-await
7:0 error Useless async should be avoided promises/remove-useless-asyncAnd create config file .putout.json with:
{
"rules": {
"remove-unused-variables": "off",
"convert-to-arrow-function": "off",
"strict-mode/add": "off",
"promises/convert-reject-to-throw": "off",
"promises/add-missing-await": "off",
"promises/remove-useless-async": "off"
}
}
Then running putout index.js --enable convert-commonjs-to-esm will update config with:
{
"rules": {
"remove-unused-variables": "off",
"convert-to-arrow-function": "off",
"strict-mode/add": "off",
"promises/convert-reject-to-throw": "off",
"promises/add-missing-await": "off",
- "promises/remove-useless-async": "off"
+ "promises/remove-useless-async": "off",
+ "convert-commonjs-to-esm": "on"
}
}Then putout --fix index.js will do the thing and update index.js with:
const unused = 5;
export default function() {
return promise();
};
async function promise(a) {
return Promise.reject(Error('x'));
}So in case of src directory, it will look like:
putout src --disable-all && putout src --enable convert-commonjs-to-esm && putout src --fixThis command will disable all rules that Putout can find right now and enable single rule. All Putout rules made for good and highly suggested to be used, they all enabled in this and all my other repositories. Anyways you can always disable what you don't need.
Happy coding π!
π fix
- (@putout/plugin-declare-undefined-variables) mockImport: multiple declarations
π₯ feature
- (putout) exit with error when "--fix" used with "--enable(-all)" or "--diable(-all)" (#77)
- (@putout/plugin-regexp) remove-useless-group: allow only test, search match (#74)
- (@putout/plugin-apply-filter-boolean) add
- (eslint-plugin-putout) rm category (https://eslint.org/blog/2021/08/eslint-v8.0.0-beta.1-released)
