ESLint

Assembles JavaScript and JSX inspection tools

ESLint

The installation

Prerequisites: Node.js (>=6.14), NPM Version 3+.

  • NPM install ESLint:
$ npm install eslint --save-dev
Copy the code
  • Set up a configuration file
$ ./node_modules/.bin/eslint --init
Copy the code
  • Run ESLint on any file or directory
$ ./node_modules/.bin/eslint yourfile.js
Copy the code

configuration

Configuration mode

Configuration Comments Configures Comments

Use JavaScript comments to embed configuration information directly into a code source file.

Configuration Files Configuration Files

Use JavaScript, JSON, or YAML files to specify configuration information for the entire directory (handling your home directory) and its subdirectories.

  • You can configure a separate.eslintrc.* file

  • Json file, and ESLint will look for and automatically read them.

  • Again, you can specify an arbitrary configuration file at command line runtime. (- c – config)

eslint -c ~/my-eslint.json file.js
Copy the code

This example uses ~/my-eslint.json as the configuration file. If.eslintrc.* and/or package.json files are also used for configuration (e.g. –no-eslintrc is not specified), the configuration will be merged. The options in this configuration file take precedence over those in the.eslintrc.* and package.json files.

Eslint configuration

Eslint configuration

Rule Rule

Eslint rules

The command line

Eslint command line

ignore

  • Using the configuration file. Eslintignore specifies the file or directory to ignore

  • Specified by command line arguments –ignore-path, –no-ignore, –ignore-pattern

    • --ignore-path

      Allows you to specify a file as.eslintignore. By default, ESLint looks for.eslintignore in the current working directory. You can change this behavior by providing a path to another file.

      eslint --ignore-path tmp/.eslintignore file.js
      eslint --ignore-path .gitignore file.js
      Copy the code
    • --no-ignore

      Disallow the exclusion of files specified in.eslintignore, –ignore-path, and –ignore-pattern files.

    • --ignore-pattern

      Allows you to specify file modes to ignore (in addition to those in.eslintignore). You can repeat this option to provide multiple modes. The syntax supported is the same as in the.eslintignore file, that is, using the same schema as the.gitignore specification.

    eslint --ignore-pattern '/lib/' --ignore-pattern '/src/vendor/*' .
    Copy the code
  • Use the –ext command line option to specify a comma-separated list of extension names to ignore other files. Note that this flag is only valid when used with directories (the argument after the eslint command is directories) and will be ignored if filename or glob mode is used.