SoFunction
Updated on 2025-02-28

Detailed explanation of the three general rules of eslint

Install

It can be installed globally or under the project. The following is an example of installation in the project. You only need to add the following configuration to it and install it: >"eslint": "^4.11.0"

Configuration

There are two configuration methods, but it is recommended to use file configuration, which is relatively independent and easy to maintain. How to use file configuration: In the root directory of the project, create a new file named .eslintrc and add some check rules to this file.

File configuration method

env: In what environment will your script run in

Environment can preset global variables of other environments, such as browser, node environment variable, es6 environment variable, mocha environment variable, etc.

'env': {
  'browser': true,
  'commonjs': true,
  'es6': true
 },

globals: extra global variables

globals: {
  vue: true,
  wx: true
 },

rules: Turn on rules and levels reported when an error occurs

There are three error levels for the rule:

  • 0 or 'off': Close the rule.
  • 1 or 'warn': Turn on the rule and act as a warning (and does not cause the check to fail).
  • 2 or 'error': Open the rule and as an error (exit code is 1, check fails).

Parameter description:

  • Parameter 1: Error level
  • Parameter 2: Processing method

Configure code comment method

Sometimes we may want to ignore some kind of eslint checks in the code, or add some kind of specific checks. At this time, we can use the following method:

Example:

Ignore no-undef check
/* eslint-disable no-undef */

Ignore no-new check
/* eslint-disable no-new */

Settings Check
/*eslint eqeqeq: off*/ /*eslint eqeqeq: 0*/

eslint check command

Check and repair
eslint * --fix

Check the specified file
eslint --fix

eslint officially provides 3 pre-installed packages:

1、eslint-config-google

Google Standard

Perform the installation:

npm install eslint eslint-config-google -g

2、eslint-config-airbnb

Airbnb standard, it relies on plugins such as eslint, eslint-plugin-import, eslint-plugin-react, and eslint-plugin-jsx-a11y, and has requirements for the versions of each plug-in.

You can execute the following command to view the versions you depend on:

npm info "eslint-config-airbnb@latest" peerDependencies

You will see the following output information, including the version requirements for each plugins

{ eslint: '^3.15.0',
 'eslint-plugin-jsx-a11y': '^3.0.2 || ^4.0.0',
 'eslint-plugin-import': '^2.2.0',
 'eslint-plugin-react': '^6.9.0' }

After knowing the version requirements of each plugins, substitute the following command to execute the installation and use it:

npm install eslint-config-airbnb eslint@^#.#.# eslint-plugin-jsx-a11y@^#.#.# eslint-plugin-import@^#.#.# eslint-plugin-react@^#.#.# -g

3、eslint-config-standard

Standard, it is a standard customized by some front-end engineers.

Perform the installation:

npm install eslint-config-standard eslint-plugin-standard eslint-plugin-promise -g

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.