eslint-plugin-putout

2019-06-24 admin

eslint-plugin-putout是什么

什么是eslint-plugin-putout,eslint plagin for putout

eslint-plugin-putout使用教程帮助文档

Putout NPM version Dependency Status Build Status Coverage Status

Putout is a pluggable and configurable code transformer. It work’s in a similar but more powerful than eslint way. While eslint can tell you about unused variable, debugger statement or redundant console.log, it fixes mostly whitespaces. Putout can fix all this things and much more.

putout

Why?

The main difference of putout is saving code transformation results directly in a source code in a day-to-day baisis.

Install

npm i putout -g

Usage

Usage: putout [options] [path]
Options:
  -h, --help              display this help and exit
  -v, --version           output version information and exit
  -f, --format            use a specific output format - default: dump
  --fix                   aply found fixes of errors to code
  --fix-count             count of fixes rounds (defaults to 10)

To find possible transform places:

putout lib test

To apply transforms:

putout lib test --fix

Built-in transforms

<details><summary>remove unused variables</summary>

  function show() {
-     const message = 'hello';
      console.log('hello world');
  }
```</details> <details><summary>remove unused private fields</summary>

```source-diff
  class Hello {
    #a = 5;
-   #b = 3;
    get() {
        return this.#a;
    };
}
```</details> <details><summary>remove unused expressions</summary>

```source-diff
  function show(error) {
-     showError;
  }
```</details> <details><summary>remove useless variables</summary>

```source-diff
-   function hi(a) {
-       const b = a;
    };
+   function hi(b) {
    };
```</details> <details><summary>remove `debugger` statement</summary>

```source-diff
- debugger;
```</details> <details><summary>remove `boolean` from `logical expressions`</summary>

```source-diff
-const t = true && false;
-const t = false;
```</details> <details><summary>replace `test.only` to `test` calls</summary>

```source-diff
-test.only('some test here', (t) => {
+test('some test here', (t) => {
    t.end();
});
```</details> <details><summary>replace `test.skip` to `test` calls</summary>

```source-diff
-test.skip('some test here', (t) => {
+test('some test here', (t) => {
    t.end();
});
```</details> <details><summary>remove `process.exit` call</summary>

```source-diff
-process.exit();
```</details> <details><summary>split variable declarations</summary>

```source-diff
-let a, b;
+let a;
+let b;
```</details> <details><summary>remove `console.log` calls</summary>

```source-diff
-console.log('hello');
```</details> <details><summary>remove empty block statements</summary>

```source-diff
-if (x > 0) {
-}
```</details> <details><summary>remove empty patterns</summary>

```source-diff
-const {} = process;
```</details> <details><summary>remove strict mode directive from esm</summary>

```source-diff
-'use strict';
-
import * from fs;
```</details> <details><summary>if absent `strict mode` directive in `commonjs` add it</summary>

```source-diff
+'use strict';
+
const fs = require('fs');
```</details> <details><summary>remove `constant conditions`</summary>

```source-diff
function hi(a) {
-   if (2 < 3) {
-       console.log('hello');
-       console.log('world');
-   }
+   console.log('hello');
+   console.log('world');
};

function world(a) {
-   if (false) {
-       console.log('hello');
-       console.log('world');
-   }
};
```</details> <details><summary>convert `esm` to `commonjs`</summary>

```source-diff
-import hello from 'world';
+const hello = require('world');
```</details> <details><summary>convert `commonjs` to `esm`</summary>

```source-diff
-const hello = require('world');
+import hello from 'world';
```</details> <details><summary>apply destructuring</summary>

```source-diff
-const hello = world.hello;
-const a = b[0];
+const {hello} = world; 
+const [a] = b;
```</details> <details><summary>merge destructuring properties</summary>

```source-diff
-const {one} = require('numbers'):
-const {two} = require('numbers');
+ const {
+   one,
+   two
+} = require('numbers');
```</details> <details><summary>convert `Math.pow` to `exponentiation operator`</summary>

```source-diff
-Math.pow(2, 4);
+2 ** 4;
```</details> <details><summary>extract sequence expressions</summary>

```source-diff
-module.exports.x = 1,
-module.exports.y = 2;
+module.exports.x = 1;
+module.exports.y = 2;
```</details> <details><summary>extract object properties into variable</summary>

```source-diff
-const {replace} = putout.operate;
-const {isIdentifier} = putout.types;
+const {operate, types} = putout;
+const {replace} = operate;
+const {isIdentifier} = types;
```</details> <details><summary>convert `apply` to `spread`</summary>

```source-diff
-console.log.apply(console, arguments);
+console.log(...arguments);
```</details> <details><summary>convert `arguments` to `rest`</summary>

```source-diff
-function hello() {
-    console.log(arguments);
+function hello(...args) {
+    console.log(args);
}
```</details> <details><summary>convert `Object.assign` to `merge spread`</summary>

```source-diff
function merge(a) {
-   return Object.assign({}, a, {
-       hello: 'world'
-   });
+   return {
+       ...a,
+       hello: 'world'
+   };
};
```</details> <details><summary>convert `binary expression` to `boolean`</summary>

```source-diff
-   const a = b === b;
+   const a = true;
```</details> 

## Plugins

The `putout` repo is comprised of many npm packages. It is a [lerna](https://github.com/lerna/lerna) monorepo similar to [babel](https://github.com/babel/babel).

| Package | Version | Dependencies |
| --- | --- | --- |
| [`@putout/plugin-remove-unused-variables`](https://github.com/coderaiser/putout/blob/master/packages/plugin-remove-unused-variables) | [![npm](https://camo.githubusercontent.com/b95bf790962d0f5e69a82fa2297f397017c9ecde/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d72656d6f76652d756e757365642d7661726961626c65732e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-remove-unused-variables) | [![Dependency Status](https://camo.githubusercontent.com/d52a96ffff9506e768dd8c3b3a59c5b51eb34a8c/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d72656d6f76652d756e757365642d7661726961626c6573)](https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-unused-variables) |
| [`@putout/plugin-remove-unused-expressions`](https://github.com/coderaiser/putout/blob/master/packages/plugin-remove-unused-expressions) | [![npm](https://camo.githubusercontent.com/2cb8c693ff7e923809b9dd8b756695b70a449f5b/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d72656d6f76652d756e757365642d65787072657373696f6e732e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-remove-unused-expressions) | [![Dependency Status](https://camo.githubusercontent.com/c0daf11e28d3aa0bdb5d515ea5b104e88da4eee3/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d72656d6f76652d756e757365642d65787072657373696f6e73)](https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-unused-expressions) |
| [`@putout/plugin-remove-unused-private-fields`](https://github.com/coderaiser/putout/blob/master/packages/plugin-remove-unused-private-fields) | [![npm](https://camo.githubusercontent.com/025973b85bcb5f01a9ea9d13b0af0e2b4a4d75ac/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d72656d6f76652d756e757365642d707269766174652d6669656c64732e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-remove-unused-private-fields) | [![Dependency Status](https://camo.githubusercontent.com/1e3b2d23645c1edbdce55cffe7971ae815ef2b54/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d72656d6f76652d756e757365642d707269766174652d6669656c6473)](https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-unused-private-fields) |
| [`@putout/plugin-remove-useless-variables`](https://github.com/coderaiser/putout/blob/master/packages/plugin-remove-useless-variables) | [![npm](https://camo.githubusercontent.com/7d9eace7f9db3ab49f6533a0b31d54e383f80031/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d72656d6f76652d7573656c6573732d7661726961626c65732e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-remove-useless-variables) | [![Dependency Status](https://camo.githubusercontent.com/ced69e7cc722bb6c687adaa1cdc9d498862f0741/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d72656d6f76652d7573656c6573732d7661726961626c6573)](https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-useless-variables) |
| [`@putout/plugin-remove-process-exit`](https://github.com/coderaiser/putout/blob/master/packages/plugin-remove-process-exit) | [![npm](https://camo.githubusercontent.com/d2725ffefce84d19267554c5d1610002f3f2c5d9/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d72656d6f76652d70726f636573732d657869742e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-remove-process-exit) | [![Dependency Status](https://camo.githubusercontent.com/0bbd751ff881ee717fa744a3895546c89e992fe0/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d72656d6f76652d70726f636573732d65786974)](https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-process-exit) |
| [`@putout/plugin-remove-debugger`](https://github.com/coderaiser/putout/blob/master/packages/plugin-remove-debugger) | [![npm](https://camo.githubusercontent.com/7a301f9c8977ee948d942f8b2612f363c57b2cb4/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d72656d6f76652d64656275676765722e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-remove-debugger) | [![Dependency Status](https://camo.githubusercontent.com/4364c18e93b9d4fc85c1cd2d67cedcaf2a8e0a3a/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d72656d6f76652d6465627567676572)](https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-debugger) |
| [`@putout/plugin-remove-only`](https://github.com/coderaiser/putout/blob/master/packages/plugin-remove-only) | [![npm](https://camo.githubusercontent.com/56b8ddfaa62d3af40f79320b3dfb429f64329fbe/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d72656d6f76652d6f6e6c792e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-remove-only) | [![Dependency Status](https://camo.githubusercontent.com/ae348c70d13b3d3789e12af99478e15486a7dca1/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d72656d6f76652d6f6e6c79)](https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-only) |
| [`@putout/plugin-remove-skip`](https://github.com/coderaiser/putout/blob/master/packages/plugin-remove-skip) | [![npm](https://camo.githubusercontent.com/28eef0fb014777c6f99b1de436c394596e6e4a8a/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d72656d6f76652d736b69702e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-remove-skip) | [![Dependency Status](https://camo.githubusercontent.com/e17738c4685e6a476255e3eaf15c2ff603a06dd6/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d72656d6f76652d736b6970)](https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-skip) |
| [`@putout/plugin-split-variable-declarations`](https://github.com/coderaiser/putout/blob/master/packages/plugin-split-variable-declarations) | [![npm](https://camo.githubusercontent.com/6b848f7d16f1ac4554c35531f80617f5fbc163eb/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d73706c69742d7661726961626c652d6465636c61726174696f6e732e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-split-variable-declarations) | [![Dependency Status](https://camo.githubusercontent.com/68ad919dcfe1c3163af28851b6de169020c83078/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d73706c69742d7661726961626c652d6465636c61726174696f6e73)](https://david-dm.org/coderaiser/putout?path=packages/plugin-split-variable-declarations) |
| [`@putout/plugin-remove-console`](https://github.com/coderaiser/putout/blob/master/packages/plugin-remove-console) | [![npm](https://camo.githubusercontent.com/dedd7fd4e1aabbc4a6d51a5413f8c789bc10596f/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d72656d6f76652d636f6e736f6c652e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-remove-console) | [![Dependency Status](https://camo.githubusercontent.com/c508c4315d1003cd338dcceade03795da897b320/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d72656d6f76652d636f6e736f6c65)](https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-console) |
| [`@putout/plugin-remove-empty`](https://github.com/coderaiser/putout/blob/master/packages/plugin-remove-empty) | [![npm](https://camo.githubusercontent.com/ee2063063bfc43faa621b8923b99226816e7d54a/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d72656d6f76652d656d7074792e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-remove-empty) | [![Dependency Status](https://camo.githubusercontent.com/8b37572782c16c900cb62795d3382ac519924ea2/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d72656d6f76652d656d707479)](https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-empty) |
| [`@putout/plugin-remove-empty-pattern`](https://github.com/coderaiser/putout/blob/master/packages/plugin-remove-empty-pattern) | [![npm](https://camo.githubusercontent.com/d3f30e06717b29f6067faf0691847c8cc77144d7/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d72656d6f76652d656d7074792d7061747465726e2e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-remove-empty-pattern) | [![Dependency Status](https://camo.githubusercontent.com/468787a09715d1e876d4ee3f84460a5ca917a330/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d72656d6f76652d656d7074792d7061747465726e)](https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-empty-pattern) |
| [`@putout/plugin-remove-constant-conditions`](https://github.com/coderaiser/putout/blob/master/packages/plugin-remove-constant-conditions) | [![npm](https://camo.githubusercontent.com/1925a20cbe3b7bde7f32ee0508264af49b73854e/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d72656d6f76652d636f6e7374616e742d636f6e646974696f6e732e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-remove-constant-conditions) | [![Dependency Status](https://camo.githubusercontent.com/cbf44bad778ac7b8182029083b13578fe3db6208/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d72656d6f76652d636f6e7374616e742d636f6e646974696f6e73)](https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-constant-conditions) |
| [`@putout/plugin-remove-boolean-from-logical-expressions`](https://github.com/coderaiser/putout/blob/master/packages/plugin-remove-boolean-from-logical-expressions) | [![npm](https://camo.githubusercontent.com/e3fe0aba501f6e6b09884893a18214ee6a997509/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d72656d6f76652d626f6f6c65616e2d66726f6d2d6c6f676963616c2d65787072657373696f6e732e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-remove-boolean-from-logical-expressions) | [![Dependency Status](https://camo.githubusercontent.com/237fa73f7d20e6ae17030a47ba8c68ac671c4752/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d72656d6f76652d626f6f6c65616e2d66726f6d2d6c6f676963616c2d65787072657373696f6e73)](https://david-dm.org/coderaiser/putout?path=packages/plugin-remove-boolean-from-logical-expressions) |
| [`@putout/plugin-convert-esm-to-commonjs`](https://github.com/coderaiser/putout/blob/master/packages/plugin-convert-esm-to-commonjs) | [![npm](https://camo.githubusercontent.com/a7dc3f50ff86c2a321be4ca8e6d8eb53351531dc/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d636f6e766572742d65736d2d746f2d636f6d6d6f6e6a732e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-convert-esm-to-commonjs) | [![Dependency Status](https://camo.githubusercontent.com/14618dcadf2bfe6117ef70aa8026e2fbbe916f07/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d636f6e766572742d65736d2d746f2d636f6d6d6f6e6a73)](https://david-dm.org/coderaiser/putout?path=packages/plugin-convert-esm-to-commonjs) |
| [`@putout/plugin-convert-commonjs-to-esm`](https://github.com/coderaiser/putout/blob/master/packages/plugin-convert-commonjs-to-esm) | [![npm](https://camo.githubusercontent.com/53ca616349c13e7b793d5c6522893a8c7e6226c7/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d636f6e766572742d636f6d6d6f6e6a732d746f2d65736d2e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-convert-commonjs-to-esm) | [![Dependency Status](https://camo.githubusercontent.com/22783968f624db654f76b8f80b6bfb13abbb8db6/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d636f6e766572742d636f6d6d6f6e6a732d746f2d65736d)](https://david-dm.org/coderaiser/putout?path=packages/plugin-convert-commonjs-to-esm) |
| [`@putout/plugin-apply-destructuring`](https://github.com/coderaiser/putout/blob/master/packages/plugin-apply-destructuring) | [![npm](https://camo.githubusercontent.com/c45697ccc2aff974d69208cb4b7baadfd9bda5c3/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d6170706c792d64657374727563747572696e672e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-apply-destructuring) | [![Dependency Status](https://camo.githubusercontent.com/824936abfeb381680c0647d639c13142ae3ae90e/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d6170706c792d64657374727563747572696e67)](https://david-dm.org/coderaiser/putout?path=packages/plugin-apply-destructuring) |
| [`@putout/plugin-merge-destructuring-properties`](https://github.com/coderaiser/putout/blob/master/packages/plugin-merge-destructuring-properties) | [![npm](https://camo.githubusercontent.com/58d89f6b3b7cfd2133d2e96ea97f45a0b5bac88e/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d6d657267652d64657374727563747572696e672d70726f706572746965732e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-merge-destructuring-properties) | [![Dependency Status](https://camo.githubusercontent.com/76f21ae8644d251cf893d67193d472dc40586eff/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d6d657267652d64657374727563747572696e672d70726f70657274696573)](https://david-dm.org/coderaiser/putout?path=packages/plugin-merge-destructuring-properties) |
| [`@putout/plugin-react-hooks`](https://github.com/coderaiser/putout/blob/master/packages/plugin-react-hooks) | [![npm](https://camo.githubusercontent.com/938d6c669da8d1f67a61f752ff5fa5cf83cf820d/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d72656163742d686f6f6b732e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-react-hooks) | [![Dependency Status](https://camo.githubusercontent.com/d2c96fd83db3ca3dba6448616d7946844614667a/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d72656163742d686f6f6b73)](https://david-dm.org/coderaiser/putout?path=packages/plugin-react-hooks) |
| [`@putout/plugin-madrun`](https://github.com/coderaiser/putout/blob/master/packages/plugin-madrun) | [![npm](https://camo.githubusercontent.com/251507ddc3b4a2550404bc8b17018e695351c8cd/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d6d616472756e2e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-madrun) | [![Dependency Status](https://camo.githubusercontent.com/d7e6a52897fd6596bcc1f46009e64d13521580d3/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d6d616472756e)](https://david-dm.org/coderaiser/putout?path=packages/plugin-madrun) |
| [`@putout/plugin-strict-mode`](https://github.com/coderaiser/putout/blob/master/packages/plugin-strict-mode) | [![npm](https://camo.githubusercontent.com/7684f7cac7b0e16344fc661c9fee057b5bea02eb/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d7374726963742d6d6f64652e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-strict-mode) | [![Dependency Status](https://camo.githubusercontent.com/f9c73b7c9fb0e9b804010c9b856361801b06ca7c/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d7374726963742d6d6f6465)](https://david-dm.org/coderaiser/putout?path=packages/plugin-strict-mode) |
| [`@putout/plugin-convert-math-pow`](https://github.com/coderaiser/putout/blob/master/packages/plugin-convert-math-pow) | [![npm](https://camo.githubusercontent.com/d557af5e2f847a34d188c63dfaa980f7c613e833/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d636f6e766572742d6d6174682d706f772e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-convert-math-pow) | [![Dependency Status](https://camo.githubusercontent.com/397cc9fb7e8efcd054d80b3a393c18776d68bab5/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d636f6e766572742d6d6174682d706f77)](https://david-dm.org/coderaiser/putout?path=packages/plugin-convert-math-pow) |
| [`@putout/plugin-extract-sequence-expressions`](https://github.com/coderaiser/putout/blob/master/packages/plugin-extract-sequence-expressions) | [![npm](https://camo.githubusercontent.com/80ee50777a4c8353ac94fb637a1b0008df567d5c/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d657874726163742d73657175656e63652d65787072657373696f6e732e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-extract-sequence-expressions) | [![Dependency Status](https://camo.githubusercontent.com/a8bfb0b34b794d6b0c55d3bd832fc31320b6c427/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d657874726163742d73657175656e63652d65787072657373696f6e73)](https://david-dm.org/coderaiser/putout?path=packages/plugin-extract-sequence-expressions) |
| [`@putout/plugin-extract-object-properties`](https://github.com/coderaiser/putout/blob/master/packages/plugin-extract-object-properties) | [![npm](https://camo.githubusercontent.com/53121931025ccf2a443035ebcdf057948b5bf3f7/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d657874726163742d6f626a6563742d70726f706572746965732e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-extract-object-properties) | [![Dependency Status](https://camo.githubusercontent.com/caeb6d9cf3557064db9d5621a4c7cf4aaab22764/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d657874726163742d6f626a6563742d70726f70657274696573)](https://david-dm.org/coderaiser/putout?path=packages/plugin-extract-object-properties) |
| [`@putout/plugin-convert-apply-to-spread`](https://github.com/coderaiser/putout/blob/master/packages/plugin-convert-apply-to-spread) | [![npm](https://camo.githubusercontent.com/b9fcd557ed119c14b67b3348a94f01c77d372a21/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d636f6e766572742d6170706c792d746f2d7370726561642e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-convert-apply-to-spread) | [![Dependency Status](https://camo.githubusercontent.com/858c29a8e4cf76fe1eac1c461a889db0fc524c04/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d636f6e766572742d6170706c792d746f2d737072656164)](https://david-dm.org/coderaiser/putout?path=packages/plugin-convert-apply-to-spread) |
| [`@putout/plugin-convert-arguments-to-rest`](https://github.com/coderaiser/putout/blob/master/packages/plugin-convert-arguments-to-rest) | [![npm](https://camo.githubusercontent.com/35bdccac22e3f4ee3e04edb35439c056f5d9da88/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d636f6e766572742d617267756d656e74732d746f2d726573742e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-convert-arguments-to-rest) | [![Dependency Status](https://camo.githubusercontent.com/8e02be52c4d3259993b8e2ee00fecbae263e1d9e/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d636f6e766572742d617267756d656e74732d746f2d72657374)](https://david-dm.org/coderaiser/putout?path=packages/plugin-convert-arguments-to-rest) |
| [`@putout/plugin-convert-object-assign-to-merge-spread`](https://github.com/coderaiser/putout/blob/master/packages/plugin-convert-object-assign-to-merge-spread) | [![npm](https://camo.githubusercontent.com/0c854bb9d8ff00c74e2c97fd0c8f0aa0b8ed469a/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d636f6e766572742d6f626a6563742d61737369676e2d746f2d6d657267652d7370726561642e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-convert-object-assign-to-merge-spread) | [![Dependency Status](https://camo.githubusercontent.com/755ca7d66a3f18d316698c175dfcb045b86edba9/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d636f6e766572742d6f626a6563742d61737369676e2d746f2d6d657267652d737072656164)](https://david-dm.org/coderaiser/putout?path=packages/plugin-convert-object-assign-to-merge-spread) |
| [`@putout/plugin-convert-binary-expression-to-boolean`](https://github.com/coderaiser/putout/blob/master/packages/plugin-convert-binary-expression-to-boolean) | [![npm](https://camo.githubusercontent.com/f46adaead46a86259abead4c8198abbfa325adf2/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d636f6e766572742d62696e6172792d65787072657373696f6e2d746f2d626f6f6c65616e2e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-convert-binary-expression-to-boolean) | [![Dependency Status](https://camo.githubusercontent.com/8b0fda1328981bb997913bd1836c4385729dc790/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d636f6e766572742d62696e6172792d65787072657373696f6e2d746f2d626f6f6c65616e)](https://david-dm.org/coderaiser/putout?path=packages/plugin-convert-binary-expression-to-boolean) |
| [`@putout/plugin-putout`](https://github.com/coderaiser/putout/blob/master/packages/plugin-putout) | [![npm](https://camo.githubusercontent.com/bdf925f35a2ebf3fed38c6122639de1552ababd5/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f706c7567696e2d7075746f75742e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/plugin-putout) | [![Dependency Status](https://camo.githubusercontent.com/7ab8d28cba7dbd8a29ca567f4a016ad888eba053/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f706c7567696e2d7075746f7574)](https://david-dm.org/coderaiser/putout?path=packages/plugin-putout) |

## Formatters

`putout` use formatters similar to [eslint's formatters](https://eslint.org/docs/user-guide/formatters/). You can specify a formatter using the `--format` or `-f` flag on the command line. For example, `--format codeframe` uses the `codeframe` formatter.

The built-in formatter options are:

*   `dump`
*   `stream`
*   `json`
*   `codeframe`
*   `progress`

| Package | Version | Dependencies |
| --- | --- | --- |
| [`@putout/formatter-dump`](https://github.com/coderaiser/putout/blob/master/packages/formatter-dump) | [![npm](https://camo.githubusercontent.com/2869d9ef40703b35e68c1199ee25240e396efbb7/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f666f726d61747465722d64756d702e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/formatter-dump) | [![Dependency Status](https://camo.githubusercontent.com/89bf563588d00bec3ac8201eb11373a427253ca7/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f666f726d61747465722d64756d70)](https://david-dm.org/coderaiser/putout?path=packages/formatter-dump) |
| [`@putout/formatter-stream`](https://github.com/coderaiser/putout/blob/master/packages/formatter-stream) | [![npm](https://camo.githubusercontent.com/f4519b3be989ca0620be2101e6a4faf17eee7cf2/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f666f726d61747465722d73747265616d2e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/formatter-stream) | [![Dependency Status](https://camo.githubusercontent.com/d86ca3739cd8cab08b3138f6fb5edf84a605f410/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f666f726d61747465722d73747265616d)](https://david-dm.org/coderaiser/putout?path=packages/formatter-stream) |
| [`@putout/formatter-progress`](https://github.com/coderaiser/putout/blob/master/packages/formatter-progress) | [![npm](https://camo.githubusercontent.com/5405ffd01a63c64e56790e33f45d9a8c87ce8b78/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f666f726d61747465722d70726f67726573732e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/formatter-progress) | [![Dependency Status](https://camo.githubusercontent.com/8d9429285886db098be6027add4f86db0db57d1a/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f666f726d61747465722d70726f6772657373)](https://david-dm.org/coderaiser/putout?path=packages/formatter-progress) |
| [`@putout/formatter-json`](https://github.com/coderaiser/putout/blob/master/packages/formatter-json) | [![npm](https://camo.githubusercontent.com/7653b11d79bbfcc3cf6b2329e97d309cd71c25c9/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f666f726d61747465722d6a736f6e2e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/formatter-json) | [![Dependency Status](https://camo.githubusercontent.com/c8f339f0ea80572a64f6b8d0ab5e5abe2cd2b5ad/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f666f726d61747465722d6a736f6e)](https://david-dm.org/coderaiser/putout?path=packages/formatter-json) |
| [`@putout/formatter-codeframe`](https://github.com/coderaiser/putout/blob/master/packages/formatter-codeframe) | [![npm](https://camo.githubusercontent.com/48fba2e0ba647e83e890db6ada19f38bdf42a2e5/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f666f726d61747465722d636f64656672616d652e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/formatter-codeframe) | [![Dependency Status](https://camo.githubusercontent.com/7a639fefc50998c319e2da6092496342f8b25542/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f666f726d61747465722d636f64656672616d65)](https://david-dm.org/coderaiser/putout?path=packages/formatter-codeframe) |
| [`@putout/formatter-eslint`](https://github.com/coderaiser/putout/blob/master/packages/formatter-eslint) | [![npm](https://camo.githubusercontent.com/89f900edbac73f2a20f98fbfee3a457b16d6ef42/68747470733a2f2f696d672e736869656c64732e696f2f6e706d2f762f407075746f75742f666f726d61747465722d65736c696e742e7376673f6d61784167653d3836343030)](https://www.npmjs.com/package/@putout/formatter-eslint) | [![Dependency Status](https://camo.githubusercontent.com/51ffa6e1af7bb6bb841bc5767a8de8e065ee8797/68747470733a2f2f64617669642d646d2e6f72672f636f64657261697365722f7075746f75742e7376673f706174683d7061636b616765732f666f726d61747465722d65736c696e74)](https://david-dm.org/coderaiser/putout?path=packages/formatter-eslint) |

### Custom Formatter

Formatter function executes on every processed file, it should return `output string`.

module.exports = ({name, source, places, index, count, filesCount, errorsCount}) => { return ‘’; };


Here is list of options:

*   `name` - name of processed file
*   `source` - source code of processed file
*   `index` - current index
*   `count` - processing files count
*   `filesCount` - count of files with errors
*   `errorsCount` count of errors

You can avoid any of this and use only what you nead. To make possible using with `putout` add prefix `putout-formatter-` to your `npm package`, and add tags `putput`, `formatter`, `putout-formatter`.

### Eslint Formatters

`eslint formatters` can be used as well with help of `@putout/formatter-eslint` this way:

Install:

npm i putout @putout/formatter-eslint eslint-formatter-pretty -D


Run:

```source-shell
ESLINT_FORMATTER=pretty putout -f eslint lib

Configuration

To configure putout add section putout to your package.json file or create .putout.json file and override any option:

{
    "formatter": "dump",
    "match": {
        "madrun.js": {
            "madrun/*": true
        },
        "bin": {
            "remove-process-exit": false,
            "remove-console": false
        },
        "test|.spec.js": {
            "remove-only": true,
            "remove-skip": true,
            "putout": true
        }
    },
    "ignore": [
        "node_modules"
    ],
    "rules": {
        "madrun/*": false,
        "convert-esm-to-commonjs": false,
        "convert-commonjs-to-esm": false,
        "remove-only": false,
        "remove-skip": false
    },
    "plugins": [
        "apply-destructuring",
        "madrun",
        "remove-debugger",
        "remove-only",
        "remove-skip",
        "remove-process-exit",
        "remove-console",
        "remove-empty",
        "remove-unused-variables",
        "remove-unused-private-fields",
        "remove-unused-expressions",
        "remove-useless-variables",
        "remove-constant-conditions",
        "remove-boolean-from-logical-expressions",
        "split-variable-declarations",
        "convert-esm-to-commonjs",
        "convert-commonjs-to-esm",
        "convert-apply-to-spread",
        "convert-arguments-to-rest",
        "convert-object-assign-to-merge-spread",
        "convert-math-pow",
        "convert-binary-expression-to-boolean",
        "extract-sequence-expressions",
        "extract-object-properties",
        "merge-destructuring-properties",
        "strict-mode",
        "putout"
    ]
}

Match

When you need to match paths to rules you can use match section for this purpose in .putout.json:

{
    "match": {
        "server": {
            "remove-process-exit": true
        }
    }
}

Ignore

When you need to ignore some routes no metter what, you can use ignore section in .putout.json:

{
    "ignore": [
        "test/fixture"
    ]
}

Plugins

Putout supports plugins, there is to types: with prefix official @putout/plugin- and user plugins with prefix putout-plugin-. To use your plugin create plugin as npm package with keywords putout, putout-plugin and add it to .putout.json.

For example if you need to remove-something create putout plugin with name putout-plugin-remove-something and add it to .putout.json:

{
    "plugins": [
        "remove-something"
    ]
}

Add putout as a peerDependency to your packages.json and set keywords: putout, putout-plugin so other users can find it <g-emoji class=“g-emoji” alias=“slightly_smiling_face” fallback-src=“https://github.githubassets.com/images/icons/emoji/unicode/1f642.png”>🙂</g-emoji>.

Plugins API

Every putout plugin should contain 3 functions:

  • report(path) - report error message to putout cli;
  • traverse({push}) - find errors and push them;
  • fix(path) - fixes paths using places array received using find function;

context of find function contains @babel/traverse and @babel/types. Also there is template. All of this can be get from putout:

const {
    types,
    template,
} = require('putout');

Most information you can find in Babel Plugin Handbook is relevant to putout plugins. To understand how things works from the inside take a look at Super Tiny Compiler.

Example

Let’s consider simplest possible plugin for removing `debugger statements @putout/plugin-remove-debugger:

// this is a message to show in putout cli
module.exports.report = () => 'Unexpected "debugger" statement';

// lets find all "debugger" statements
module.exports.traverse = ({push}) => {
    return {
        DebuggerStatement(path) {
            push(path);
        }
    }
};

// when user calls "putout --fix" node will be removed with
module.exports.fix = (path) => {
    path.remove();
};

Testing

That was the simplest module to remove debugger statements in your code. Let’s look how to test it using @putout/test:

const removeDebugger = require('..');
const test = require('@putout/test')(__dirname, {
    'remove-debugger': removeDebugger,
});

// this is how we test that messages is correct
test('remove debugger: report', (t) => {
    t.reportCode('debugger', 'Unexpected "debugger" statement');
    t.end();
});

// stetement should be removed so result is empty
test('remove debugger: transformCode', (t) => {
    t.transformCode('debugger', '');
    t.end();
});

As you see test runner it is little bit modifed tape. To see more sophisticated example look at @putout/remove-console.

Codemods

putout supports codemodes in the similar to plugins way, just create a directory ~/.putout and put your plugins there. Here is example: convert-tape-to-supertape and this is example of work.

ESLint Support

If you see that putout brokes formatting of your code, use eslint plugin eslint-plugin-putout.

Install eslint-plugin-putout with:

npm i eslint eslint-plugin-putout -D

Then create eslintrc.json:

{
    "extends": [
        "plugin:putout/recommended"
    ],
    "plugins": [
        "putout"
    ]
}

And use with putout this way:

putout --fix lib && eslint --fix lib

API

To use in the browser use require(“putout/dist/putout.js”)

putout(source)

const {readFileSync} = require('fs');
const putout = require('putout');

const source = readFileSync('./1.js', 'utf8');
console.log(source);
// outputs
`
const t = 'hello';
const m = t + '!';
`

const result = putout(source);
// returns
`
const t = 'hello';
`

const result2 = putout(result);
// returns
``

License

MIT

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-68083.html

文章标题:eslint-plugin-putout

回到顶部