Official Chinese document: cn.eslint.org/docs/user-g…

Code style detection tool, develop coding specifications, can avoid low-level bugs in the project, the output of uniform style of code.

ESLint has the following features:

  • The default rules contain all existing rules in JSLint, JSHint, and are easy to migrate
  • High configurability of rules: can set “warning “,” error “two error registration, or directly disable
  • Contains rules for code style detection
  • Support plug-in extension, custom rules

extends

Configure rules that extend rules that someone else has already written (“extends”: “ESLint :recommended”)

env

Env Specifies the environment in which the script is run. All environments have a set of predefined global variables (for example, if “browser”: true is configured, the code can directly use browser-specific attributes such as window, document, and so on, without warning).

Js uses comments

Use comments in JavaScript to specify the environment in the following format:

/* eslint-env node, mocha */
Copy the code

Used in specific plug-ins

{
    "plugins": ["example"]."env": {
        "example/custom": true}}Copy the code

globals

globals

Additional global variables accessed by the script during execution

D The no-undef rule will warn when accessing an undefined variable in the current source file. If globals is configured, no prompt is displayed

Global variables are specified using annotations in js

/* global var1, var2 */
/* global var1:writeable, var2: readonly */
Copy the code

Disable with off

{
    "env": {
        "es6": true
    },
    "globals": {
        "custom": "custom value"
        "Promise": "off"}}Copy the code

rules

Enabled rules and their respective error levels (matched files need to conform to the configuration rules here)

{
    "rules": {
        "semi": ["error"."always"]."qutoes": ["error"."double"]}}Copy the code

The first value of the rule configuration is the error level. Optional values:

  • "off"or0Close the rules
  • "warn"or1Treat the rule as a warning (does not affect exit codes)
  • "error"or2Treating the rule as an error (exit code 1)

The second value refers to the value of the correct writing of the rule or the logic to follow

plugins

plugins

When configuring a rule defined in a plugin, you must use the plugin name/rule ID. Such as:

{
    "plugins": [
        "plugin1"]."rules": {
        "quotes": ["error"."double"]."plugin1/rule1": "error"}}Copy the code

Third-party plug-ins are supported. Before using the plug-in, you need to install it using NPM.

You can omit the eslint-plugin- prefix during configuration

{
    "plugins": [
        "plugin1"."eslint-plugin-plugin2"]}Copy the code

parser

parser

ESLint uses Espree as its parser by default and can be configured to specify a different parser

parserOptions

parserOptions

Specify the JavaScript language options you want to support. The default “ECMAScript5”.

Note that support for JSX syntax is not the same as support for React. React applies specific semantics to ESlint’s inability to recognize JSX syntax. If you are using React semantics and want React semantics support, it is recommended to use eslint-plugin-react.

Similarly, support for ES6 syntax does not mean that colleagues support new ES6 global variables or types (such as new types like Set). For ES6 syntax, use {“parserOptions”: {“ecmaVersion”: 6}}; For new ES6 global variables, use {“env”: {” ES6 “: true}}. This configuration automatically enables ES6 syntax, but the former does not.

.eslintignore

Tells ESLint which files or directories to ignore

Configuration items that are not commonly used

root

Eslint will look for configuration files in the parent directory of the same directory according to the nearest rule. If both parent directories have configuration items in the same directory, merge the configuration items and apply them to files in the current directory. If the same directory has the same name, the current directory will prevail (object.assign).

By default, esLint will look for configuration files in all parent directories, up to the root directory, and will stop looking for parent directories when “root”:true is configured in a configuration.

processor

processor

Plug-ins can provide processors. The processor can extract JavaScript code from another file and then have ESLint examine the JavaScript code. Or the processor can transform JavaScript code in preprocessing.

To specify a processor in a configuration file, use the Processor key and a concatenated string of the plug-in name and processor name followed by a slash. For example, the following option enables the processor A-processor provided by plug-in A-plugin:

{
    "plugins": ["a-plugin"]."processor": "a-plugin/a-processor"
}
Copy the code

To specify a processor for a particular type of file, use a combination of the overrides key and the Processor key. For example, the processor a-plugin/markdown is used for *.md files.

setting

setting

Add a shared configuration item that will be provided for each rule executed