What is eslint-plugin-vue?
The eslint-plugin-vue npm package is a plugin for ESLint specifically designed for Vue.js applications. It provides linting rules that are tailored to the Vue.js framework, helping developers to write consistent and error-free code by following best practices and avoiding common mistakes.
What are eslint-plugin-vue's main functionalities?
Style Guide Enforcement
Enforces a style guide by limiting the number of attributes per line in Vue templates. This helps in maintaining readability and consistency in the codebase.
"vue/max-attributes-per-line": ["error", { "singleline": 3, "multiline": { "max": 1, "allowFirstLine": false } }]
Component Naming
Ensures that component names in templates are written in a specific case, such as PascalCase, which is a common convention in Vue.js projects.
"vue/component-name-in-template-casing": ["error", "PascalCase"]
Avoiding Deprecated Features
Prevents the use of deprecated features like the 'slot' attribute, encouraging developers to use the updated syntax and features of Vue.js.
"vue/no-deprecated-slot-attribute": "error"
Prop Validation
Ensures that every prop has a default value, which can prevent bugs and make the components more predictable.
"vue/require-default-prop": "error"
Script Indentation
Enforces consistent indentation in the script section of Vue single-file components, which improves code readability and maintainability.
"vue/script-indent": ["error", 2, { "baseIndent": 1, "switchCase": 1, "ignores": [] }]
Other packages similar to eslint-plugin-vue
eslint-plugin-react
Similar to eslint-plugin-vue, eslint-plugin-react provides linting rules specific to React applications. It helps enforce best practices and conventions for React components, JSX syntax, and more.
eslint-plugin-angular
This plugin is designed for Angular applications and offers linting rules that cater to the Angular framework's structure and coding styles, similar to how eslint-plugin-vue serves Vue.js developers.
eslint-plugin-svelte3
For developers using Svelte, eslint-plugin-svelte3 provides linting rules tailored to the Svelte framework. It helps maintain code quality and consistency in a way that's analogous to eslint-plugin-vue's role for Vue.js.
eslint-plugin-vue
![License](https://img.shields.io/github/license/vuejs/eslint-plugin-vue.svg?style=flat)
Official ESLint plugin for Vue.js
:book: Documentation
Please refer to the official website.
:anchor: Versioning Policy
This plugin follows Semantic Versioning and ESLint's Semantic Versioning Policy.
:newspaper: Releases
This project uses GitHub Releases.
:beers: Contribution Guide
Contributing is welcome! See the ESLint Vue Plugin Developer Guide.
Working With Rules
Be sure to read the official ESLint guide before you start writing a new rule.
To see what an abstract syntax tree (AST) of your code looks like, you may use AST Explorer. After opening AST Explorer, select Vue
as the syntax and vue-eslint-parser
as the parser.
The default JavaScript parser must be replaced because Vue.js single file components are not plain JavaScript, but a custom file format. vue-eslint-parser
is a replacement parser that generates an enhanced AST with nodes that represent specific parts of the template syntax, as well as the contents of the <script>
tag.
To learn more about certain nodes in a produced AST, see the ESTree project page and the vue-eslint-parser AST documentation.
vue-eslint-parser
provides a few useful parser services to help traverse the produced AST and access template tokens:
context.parserServices.defineTemplateBodyVisitor(visitor, scriptVisitor)
context.parserServices.getTemplateBodyTokenStore()
Check out an example rule to see usage of these services.
Be aware that depending on the code samples you write in tests, the RuleTester
parser property must be set accordingly (this can be done on a test by test basis). See an example here.
If you're stuck, remember there are many rules available for reference. If you can't find the right solution, don't hesitate to reach out in issues – we're happy to help!
:lock: License
See the LICENSE file for license rights and limitations (MIT).