About eslint-vue

This plugin is used for Vue syntax verification. eslint-vue installation

It needs to be used with eslint-js, eslint-js installation

Error Highlight

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

About eslint-vue - 图1

Plugin Configuration

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

About eslint-vue - 图2

**Real-time verification and automatic modification

Only supports HBuilderX 2.6.8+ version

  1. To use this function, you must install eslint-js and eslint-vue 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-vue configuration files

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

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

The configuration file is as follows:

  1. module.exports = {
  2. "extends": "plugin:vue/essential",
  3. "parserOptions": {},
  4. "rules": {}
  5. };

More configuration instructions can refer to options

How to add rules?

rules list

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)

Modify the .eslintrc.js file and add rules, such as:

  1. {
  2. //Disable asynchronous actions in computed properties
  3. 'vue/no-async-in-computed-properties': 'error',
  4. //no duplicate keys
  5. 'vue/no-dupe-keys': 'error',
  6. //no duplicate attributes
  7. 'vue/no-duplicate-attributes': 'error',
  8. //在 <template> Parsing errors are not allowed under the label
  9. 'vue/no-parsing-error': ['error',{
  10. 'x-invalid-end-tag': false,
  11. }],
  12. //Overriding reserved keywords is not allowed
  13. 'vue/no-reserved-keys': 'error',
  14. //Mandatory data must be a function with a return value
  15. // 'vue/no-shared-component-data': 'error',
  16. //Side effects are not allowed in computed properties.
  17. 'vue/no-side-effects-in-computed-properties': 'error',
  18. //<template>No key attributes
  19. 'vue/no-template-key': 'error',
  20. //在 <textarea> not mustaches
  21. 'vue/no-textarea-mustache': 'error',
  22. //Unused variable definitions are not allowed in v-for or attributes in the scope
  23. 'vue/no-unused-vars': 'error',
  24. //<component>Label required v-bind:is
  25. 'vue/require-component-is': 'error',
  26. // The render function must have a return value
  27. 'vue/require-render-return': 'error',
  28. // Ensure that v-bind:key and v-for instructions appear in pairs
  29. 'vue/require-v-for-key': 'error',
  30. // Check whether the default prop value is valid
  31. 'vue/require-valid-default-prop': 'error',
  32. // Ensure that there is a return statement in the computed attribute
  33. 'vue/return-in-computed-property': 'error',
  34. // Mandatory verification of template root node
  35. 'vue/valid-template-root': 'error',
  36. // Mandatory verification of v-bind command
  37. 'vue/valid-v-bind': 'error',
  38. // 指令Mandatory verification of v-cloak command
  39. 'vue/valid-v-cloak': 'error',
  40. // Mandatory verification of v-else-if command
  41. 'vue/valid-v-else-if': 'error',
  42. // Mandatory verification of v-else command
  43. 'vue/valid-v-else': 'error',
  44. // Mandatory verification of v-for command
  45. 'vue/valid-v-for': 'error',
  46. // Mandatory verification of v-html commands
  47. 'vue/valid-v-html': 'error',
  48. // Mandatory verification of v-if commands
  49. 'vue/valid-v-if': 'error',
  50. // Mandatory verification of v-model commands
  51. 'vue/valid-v-model': 'error',
  52. // Mandatory verification of v-on commands
  53. 'vue/valid-v-on': 'error',
  54. // Mandatory verification of v-once commands
  55. 'vue/valid-v-once': 'error',
  56. // Mandatory verification of v-pre commands
  57. 'vue/valid-v-pre': 'error',
  58. // Mandatory verification of v-show commands
  59. 'vue/valid-v-show': 'error',
  60. // Mandatory verification of v-text commands
  61. 'vue/valid-v-text': 'error'
  62. }

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-vue - 图3

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",
  16. "semi": [2, "always"] ,
  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-vue - 图4