• Stars
    star
    269
  • Rank 152,662 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 4 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The CLI tool to fix huge number of ESLint errors

Cover image

The CLI tool to fix huge number of ESLint errors

Quick fixes, per rule.
Run eslint --fix, disable per line, apply suggestions, and more! Screenshot

Motivation

The default ESLint output contains a lot of useful messages for developers, such as the source of the error and hints for fixing it. While this works for many use cases, it does not work well in situations where many messages are reported. For example, when introducing ESLint into a project, or when making big changes to the .eslintrc of a project. In these situations, the output of ESLint can be quite large, making it difficult for developers to analyze the output. It is also difficult for the developer to fix messages mechanically, because messages of many rules are mixed up in the output.

In such the above situation, I think two things are important:

  • Show a summary of all problems (called "warnings" or "errors" in ESLint) so that the whole picture can be easily understood
    • Showing the details of each problem will confuse developers.
  • Provide an efficient way to fix many problems
    • eslint --fix is one of the best ways to fix problems efficiently, but it auto-fixes all rule problems at once.
    • Depending on the rule, auto-fix may affect the behavior of the code, so auto-fix should be done with care.
    • Therefore, it is desirable to provide a way to auto-fix in smaller units than eslint --fix.

So, I created a tool called eslint-interactive which wraps ESLint. This tool groups all problems by rule and outputs formatted number of problems per rule. In addition to the breakdown of problems per rule, it also outputs the number of fixable problems and other hints to help developers fix problems.

It also supports the following actions in addition to eslint --fix. All actions can be applied for each rule:

  • Display details of lint results
  • Run eslint --fix
  • Disable per line (by // eslint-disable-next-line <rule-name>)
  • Disable per file (/* eslint-disable <rule-name> */)
  • Convert error to warning per file (/* eslint <rule-name>: 1 */)
  • Apply suggestions
  • Make forcibly fixable and run eslint --fix

Installation

📝 NOTE: The globally installed eslint-interactive is not officially supported. It is recommended to install eslint-interactive locally. See FAQ.

$ # For npm
$ npm i -D eslint-interactive
$ npx eslint-interactive --help

$ # For yarn
$ yarn add -D eslint-interactive
$ yarn eslint-interactive --help

$ # For pnpm
$ pnpm add -D eslint-interactive
$ pnpm exec eslint-interactive --help

Usage

The interface of eslint-interactive is partially compatible with eslint. So, in most cases, simply replacing eslint with eslint-interactive in the command to lint (e.g. eslint ./src --ext .ts,.tsx) will work. However, eslint-interactive is installed locally, so you need to add npx.

$ # Show help
$ npx eslint-interactive --help
eslint-interactive [file.js] [dir]

Options:
      --help                         Show help                                                                                     [boolean]
      --version                      Show version number                                                                           [boolean]
      --eslintrc                     Enable use of configuration from .eslintrc.*                                  [boolean] [default: true]
  -c, --config                       Use this configuration, overriding .eslintrc.* config options if present                       [string]
      --resolve-plugins-relative-to  A folder where plugins should be resolved from, CWD by default                                 [string]
      --ext                          Specify JavaScript file extensions                                                              [array]
      --rulesdir                     Use additional rules from this directory                                                        [array]
      --ignore-path                  Specify path of ignore file                                                                    [string]
      --format                       Specify the format to be used for the `Display problem messages` action [string] [default: "codeframe"]
      --quiet                        Report errors only                                                           [boolean] [default: false]
      --cache                        Only check changed files                                                      [boolean] [default: true]
      --cache-location               Path to the cache file or directory
                                                            [string] [default: "node_modules/.cache/eslint-interactive/10.6.0/.eslintcache"]

Examples:
  eslint-interactive ./src                                           Lint ./src/ directory
  eslint-interactive ./src ./test                                    Lint multiple directories
  eslint-interactive './src/**/*.{ts,tsx,vue}'                       Lint with glob pattern
  eslint-interactive ./src --ext .ts,.tsx,.vue                       Lint with custom extensions
  eslint-interactive ./src --rulesdir ./rules                        Lint with custom rules
  eslint-interactive ./src --no-eslintrc --config ./.eslintrc.ci.js  Lint with custom config

Available actions

Actions can be executed per rule.

  • Display details of lint results
    • Displays ESLint error reports.
  • Run eslint --fix
    • Apply fixable problems.
  • Disable per line
    • Add disable comments (// eslint-disable-next-line <rule-name>) per line.
  • Disable per file
    • Add disable comments (// eslint-disable <rule-name>) per file.
  • Convert error to warning per file
    • Add inline config comments (/* eslint <rule-name>: 1 */) per file.
  • Apply suggestions (experimental, for experts)
    • Select one of the applicable suggestions and apply it. (What's suggestions?)
    • Users can write JavaScript code to programmatically select the suggestion to be applied.
    • The tool will tell you how to use the feature in detail. Please follow the instructions of the tool.
    • This feature is experimental and may change significantly.
  • Make forcibly fixable and run eslint --fix (experimental, for experts)
    • This feature make forcibly un-fixable problems fixable, and apply them.
    • The tool will tell you how to use the feature in detail. Please follow the instructions of the tool.
    • This feature is experimental and may change significantly.

Programmable API

See Programmable API documentation.

FAQ

What's suggestions?

Excerpt a quote from official ESLint documentation.

In some cases fixes aren't appropriate to be automatically applied, for example, if a fix potentially changes functionality or if there are multiple valid ways to fix a rule depending on the implementation intent. In such a case, the ESLint rule provides candidates for a fix. These are called suggestions.

Suggestion can be one or more. The user has to manually decide if that fix should be applied or which fix should be applied because automatically applying of suggestion is inappropriate. For this reason, ESLint has stated that it will not officially provide a way to automatically apply suggestion (ref). Instead, tools such as vscode-eslint allow users to apply suggestions manually (ref).

Why is global installation not officially supported?

eslint is installed locally in most projects. In such a case, if eslint-interactive is installed globally, it will be installed in a different space than eslint. The space separation makes some resources of eslint-interactive inaccessible from eslint and may break them (ref: #77). Therefore, global installation of eslint-interactive is not officially supported.

Is global installation prohibited?

No. eslint-interactive does not prohibit global installation. Global installation is allowed because it has the advantage of saving installation effort. However, it is not officially supported and users must use it at their own risk.

If the global installation does not work, you can send a patch by pull request. However, whether the patch will be accepted depends on the case.

What's the difference from eslint-nibble?

A tool similar to eslint-interactive is eslint-nibble. Both tools solve the same problem, but eslint-interactive has some features that eslint-nibble does not have. For example, eslint-interactive prints the number of fixable problems per rule, while eslint-nibble does not. Also, eslint-interactive has various tricks to speed up the cycle of auto-fixing per-rule, but eslint-nibble auto-fixes once and terminates the process every time, so it is not as fast as eslint-interactive.

I think these features are very important to solve the aforementioned problem. At first, I thought of implementing these features in eslint-nibble, but it required a major rewrite of the code, so I implemented it as a new tool eslint-interactive. Although eslint-interactive is a tool independent of eslint-nibble, it is influenced by the ideas of eslint-nibble and inherits some of its code. That's why you can find the names of @IanVS and others in the license of eslint-interactive.

Thanks, @IanVS.

What's the difference from suppress-eslint-errors?

suppress-eslint-errors is an excellent tool to add comments for disable mechanically. Just like eslint-interactive, it allows you to add disable comments for each rule and leave the purpose of disable as a comment. There is no functional difference between the two, but there is a difference in the API used to insert the comments.

suppress-eslint-errors uses jscodeshift to insert comments. jscodeshift modifies the file in parallel, so suppress-eslint-errors has the advantage of being able to insert comments faster. However, jscodeshift cannot reuse the AST of ESLint, so you need to reparse the code in jscodeshift. This means that you have to pass jscodeshift the information it needs to parse your code (parser type, parser options). In fact, suppress-eslint-errors requires --extensions and --parser command line option. Normally, users specify the parsing options in .eslintrc, so passing these options may seem cumbersome. Also, due to the difference in the way ESLint and jscodeshift parse, it may not be possible to insert comments correctly.

On the other hand, eslint-interactive uses ESLint.outputFixes to insert comments. It uses ESLint's API to do everything from parsing the code to inserting the comments, so it works as expected in many cases. Also, eslint-interactive will parse the code using the parsing options specified in .eslintrc. Therefore, comments can be inserted without any additional command line options. By the way, comment insertion is slower than suppress-eslint-errors because, unlike suppress-eslint-errors, it cannot modify files in parallel. However, this limitation may be improved when ESLint supports parallel processing in the near future.

Benchmark

More Repositories

1

happy-css-modules

Typed, definition jumpable CSS Modules. Moreover, easy!
TypeScript
141
star
2

wasm-dev-book

Rust を用いた WebAssembly の開発環境を構築する手法を紹介する本.
JavaScript
40
star
3

typescript-plugin-asset

TypeScript language service plugin supporting for importing assets.
TypeScript
32
star
4

eslint-plugin-layout-shift

ESLint plugin to force responsive media elements to set the width/height attributes
JavaScript
21
star
5

graphql-codegen-typescript-fabbrica

GraphQL Code Generator Plugin to define mock data factory.
TypeScript
12
star
6

dotfiles

Dotfiles for @mizdra
Shell
10
star
7

scrapbox-userscript-icon-suggestion

Scrapbox's UserScript to suggest and insert icons
TypeScript
9
star
8

now-playing-for-google-play-music

#NowPlaying for YouTube Music
TypeScript
9
star
9

npm-package-template

TypeScript
8
star
10

volar-demo

TypeScript
8
star
11

inline-fixture-files

A utility to write filesystem fixtures inline.
TypeScript
6
star
12

img-layout-shift-detector

The browser extension that detects `<img>` tags that cause Layout Shift
TypeScript
5
star
13

eslint-config-mizdra

ESLint config for @mizdra
JavaScript
3
star
14

strictly-typed-event-target

This is a strictly-typed version of `EventTarget`
TypeScript
3
star
15

rocketimer

[experimental] 🚀Blazing fast cascade timer
TypeScript
3
star
16

shiki

Shiki is a rust-like programming language.
Rust
2
star
17

rust-tinymt

The Rust implements of TinyMT for Pokémon RNG.
Rust
2
star
18

webpack-ts-skeleton

TypeScriptの素振り環境
JavaScript
2
star
19

react-component-definition-jump-test

Created with CodeSandbox
TypeScript
2
star
20

webpack-wasm-skeleton

WebAssemblyの素振り環境
JavaScript
1
star
21

happy-birthday

Happy Birthday, mizdra 🎉
JavaScript
1
star
22

pokemon-swsh-scripts

Python
1
star
23

yukari-slot

『ゆゆ式』の日向縁さんの誕生日をお祝いして作成したスロットゲーム
HTML
1
star
24

parcel-frontend-skeleton

Boilerplate for Web frontend.
JavaScript
1
star
25

stylelint-happy-css-modules

TypeScript
1
star
26

prettier-config-mizdra

Shareable Prettier Config for @mizdra
JavaScript
1
star
27

Perl-Tidy-LanguageServer

Language Server for Perltidy.
Perl
1
star
28

renovate-config-mizdra

Shareable Renovate Config for @mizdra
1
star
29

nextjs-get-server-side-props-util-example

CSS
1
star
30

wasm-dev-book-webpack

Webpackを使ったWebAssemblyの開発環境.
Rust
1
star
31

wasm-dev-book-hello-wasm

WebAssemblyの入門.
HTML
1
star