About eslint-js

ESLint is an open source JavaScript linting utility and used to verify the js code in js and html.

eslint-js Download

Error Highlight

As shown in the figure below, when an error is detected, a red wavy line will appear.

About eslint-js - 图1

ESLint Plugin Configuration

Click the menu [Tools] [Settings -> Plugin] [eslint-js], you can see the eslint-js configuration.

About eslint-js - 图2

Real-time verification and automatic modification

Supported from HBuilderX 2.6.8+ version

  1. To use this function, you must install the eslint-js plugin
  2. The vue-cli project needs to install the eslint library and configure the eslint rules.
  3. If the above conditions are met, when the code is written and saved, if there is an error in the code, it will be automatically repaired;
  4. Real-time verification function, not enabled by default, you have to manually enable this function

HBuilderX eslint-js configuration files

The configuration file of eslint-js is .eslintrc.js.

Click on the menu Tools -> Plugins -> eslint-js -> .eslintrc.js to open the .eslintrc.js file.

The configuration file is as follows:

  1. module.exports = {
  2. "plugins": [],
  3. "env": {
  4. "browser": true,
  5. "node": true
  6. },
  7. "parser": "esprima",
  8. "parserOptions": {},
  9. "rules": {}
  10. }

More configuration instructions can refer to options

How to add rules?

rules list: https://cn.eslint.org/docs/rules/

Rule Settings:

  • “off” or 0 - turn the rule off
  • “warn” or 1 - turn the rule on as a warning (doesn’t affect the program to exit)
  • “error” or 2 - turn the rule on as an error (the program will exit when triggered)

Example:

  1. "rules": {
  2. "camelcase": 2, //enforce camelcase naming convention,
  3. "indent": [2, 4], //enforce consistent indentation
  4. "id-match": 0, //require identifiers to match a specified regular expression
  5. "init-declarations": 1, //require or disallow initialization in variable declarations
  6. "no-undef": 1, //disallow the use of undeclared variables unless mentioned in /*global */ comments
  7. }

Exampl:Web Project

Use eslint to check extra spaces and fix them automatically.

About eslint-js - 图3

  1. module.exports = {
  2. "plugins": [
  3. "html"
  4. ],
  5. "parserOptions": {
  6. "ecmaVersion": 2018,
  7. "sourceType": "module",
  8. "ecmaFeatures": {
  9. "jsx": true
  10. },
  11. "allowImportExportEverywhere": false
  12. },
  13. rules: {
  14. "no-alert": 0,
  15. "semi": [2, "always"],
  16. "no-multi-spaces": "error",
  17. "quotes": ["error", "single"]
  18. }
  19. }

Example:uni-app project

  • You have to install eslint-vue plugin, plugin address to verify vue syntax.
  • To configure validation rules, you must config rules in the eslint-vue plugin.
  • Menu [Tools] [Settings] [Plugins] [eslint-vue] [.eslintrc.js], edit the .eslintrc.js file

Example: eslint automatically fixes double quotes to single quotes

About eslint-js - 图4

Example:vue-cli project

For the vue-cli project, if you use the configuration rules under the project, you need to install the relevant libraries and create the .eslintrc.js file in the project root directory

Note:

  1. The eslint rules of a project will override the rules in the HBuilderX editor eslint plugin
  2. You have to install eslint-vue plugin, plugin address to verify vue syntax.
  1. npm install --save eslint eslint-plugin-vue eslint-plugin-html eslint-config-standard eslint-plugin-import eslint-plugin-node eslint-plugin-promise eslint-plugin-standard babel-eslint

.eslintrc.js configuration files

  1. module.exports = {
  2. extends: [
  3. 'plugin:vue/recommended'
  4. ],
  5. parserOptions: {
  6. 'ecmaVersion': 2018,
  7. 'sourceType': 'module',
  8. 'ecmaFeatures': {
  9. 'jsx': true
  10. },
  11. 'allowImportExportEverywhere': false
  12. },
  13. rules: {
  14. "no-alert": 0,
  15. "no-multi-spaces": "error", // disallow multiple spaces
  16. "semi": [2, "always"] , // equire or disallow semicolons instead of ASI
  17. "quotes": ["error", "single"] // enforce the consistent use of either backticks, double, or single quotes
  18. }
  19. }

Example: Use eslint to automatically add semicolons

About eslint-js - 图5