提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
8.8 KiB

  1. # vue-eslint-parser
  2. [![npm version](https://img.shields.io/npm/v/vue-eslint-parser.svg)](https://www.npmjs.com/package/vue-eslint-parser)
  3. [![Downloads/month](https://img.shields.io/npm/dm/vue-eslint-parser.svg)](http://www.npmtrends.com/vue-eslint-parser)
  4. [![Build Status](https://github.com/vuejs/vue-eslint-parser/workflows/CI/badge.svg)](https://github.com/vuejs/vue-eslint-parser/actions)
  5. [![Coverage Status](https://codecov.io/gh/vuejs/vue-eslint-parser/branch/master/graph/badge.svg)](https://codecov.io/gh/vuejs/vue-eslint-parser)
  6. [![Dependency Status](https://david-dm.org/vuejs/vue-eslint-parser.svg)](https://david-dm.org/vuejs/vue-eslint-parser)
  7. The ESLint custom parser for `.vue` files.
  8. ## ⤴️ Motivation
  9. This parser allows us to lint the `<template>` of `.vue` files. We can make mistakes easily on `<template>` if we use complex directives and expressions in the template. This parser and the rules of [eslint-plugin-vue](https://github.com/vuejs/eslint-plugin-vue) would catch some of the mistakes.
  10. ## 💿 Installation
  11. ```bash
  12. npm install --save-dev eslint vue-eslint-parser
  13. ```
  14. - Requires Node.js ^12.22.0, ^14.17.0, 16.0.0 or later.
  15. - Requires ESLint 6.0.0 or later.
  16. ## 📖 Usage
  17. 1. Write `parser` option into your `.eslintrc.*` file.
  18. 2. Use glob patterns or `--ext .vue` CLI option.
  19. ```json
  20. {
  21. "extends": "eslint:recommended",
  22. "parser": "vue-eslint-parser"
  23. }
  24. ```
  25. ```console
  26. $ eslint "src/**/*.{js,vue}"
  27. # or
  28. $ eslint src --ext .vue
  29. ```
  30. ## 🔧 Options
  31. `parserOptions` has the same properties as what [espree](https://github.com/eslint/espree#usage), the default parser of ESLint, is supporting.
  32. For example:
  33. ```json
  34. {
  35. "parser": "vue-eslint-parser",
  36. "parserOptions": {
  37. "sourceType": "module",
  38. "ecmaVersion": 2018,
  39. "ecmaFeatures": {
  40. "globalReturn": false,
  41. "impliedStrict": false,
  42. "jsx": false
  43. }
  44. }
  45. }
  46. ```
  47. ### parserOptions.parser
  48. You can use `parserOptions.parser` property to specify a custom parser to parse `<script>` tags.
  49. Other properties than parser would be given to the specified parser.
  50. For example:
  51. ```json
  52. {
  53. "parser": "vue-eslint-parser",
  54. "parserOptions": {
  55. "parser": "@babel/eslint-parser",
  56. "sourceType": "module"
  57. }
  58. }
  59. ```
  60. ```json
  61. {
  62. "parser": "vue-eslint-parser",
  63. "parserOptions": {
  64. "parser": "@typescript-eslint/parser",
  65. "sourceType": "module"
  66. }
  67. }
  68. ```
  69. You can also specify an object and change the parser separately for `<script lang="...">`.
  70. ```jsonc
  71. {
  72. "parser": "vue-eslint-parser",
  73. "parserOptions": {
  74. "parser": {
  75. // Script parser for `<script>`
  76. "js": "espree",
  77. // Script parser for `<script lang="ts">`
  78. "ts": "@typescript-eslint/parser",
  79. // Script parser for vue directives (e.g. `v-if=` or `:attribute=`)
  80. // and vue interpolations (e.g. `{{variable}}`).
  81. // If not specified, the parser determined by `<script lang ="...">` is used.
  82. "<template>": "espree",
  83. }
  84. }
  85. }
  86. ```
  87. If the `parserOptions.parser` is `false`, the `vue-eslint-parser` skips parsing `<script>` tags completely.
  88. This is useful for people who use the language ESLint community doesn't provide custom parser implementation.
  89. ### parserOptions.vueFeatures
  90. You can use `parserOptions.vueFeatures` property to specify how to parse related to Vue features.
  91. For example:
  92. ```json
  93. {
  94. "parser": "vue-eslint-parser",
  95. "parserOptions": {
  96. "vueFeatures": {
  97. "filter": true,
  98. "interpolationAsNonHTML": true,
  99. "styleCSSVariableInjection": true,
  100. }
  101. }
  102. }
  103. ```
  104. ### parserOptions.vueFeatures.filter
  105. You can use `parserOptions.vueFeatures.filter` property to specify whether to parse the Vue2 filter. If you specify `false`, the parser does not parse `|` as a filter.
  106. For example:
  107. ```json
  108. {
  109. "parser": "vue-eslint-parser",
  110. "parserOptions": {
  111. "vueFeatures": {
  112. "filter": false
  113. }
  114. }
  115. }
  116. ```
  117. If you specify `false`, it can be parsed in the same way as Vue 3.
  118. The following template parses as a bitwise operation.
  119. ```vue
  120. <template>
  121. <div>{{ a | b }}</div>
  122. </template>
  123. ```
  124. However, the following template that are valid in Vue 2 cannot be parsed.
  125. ```vue
  126. <template>
  127. <div>{{ a | valid:filter }}</div>
  128. </template>
  129. ```
  130. ### parserOptions.vueFeatures.interpolationAsNonHTML
  131. You can use `parserOptions.vueFeatures.interpolationAsNonHTML` property to specify whether to parse the interpolation as HTML. If you specify `true`, the parser handles the interpolation as non-HTML (However, you can use HTML escaping in the interpolation). Default is `true`.
  132. For example:
  133. ```json
  134. {
  135. "parser": "vue-eslint-parser",
  136. "parserOptions": {
  137. "vueFeatures": {
  138. "interpolationAsNonHTML": true
  139. }
  140. }
  141. }
  142. ```
  143. If you specify `true`, it can be parsed in the same way as Vue 3.
  144. The following template can be parsed well.
  145. ```vue
  146. <template>
  147. <div>{{a<b}}</div>
  148. </template>
  149. ```
  150. But, it cannot be parsed with Vue 2.
  151. ### parserOptions.vueFeatures.styleCSSVariableInjection
  152. If set to `true`, to parse expressions in `v-bind` CSS functions inside `<style>` tags. `v-bind()` is parsed into the `VExpressionContainer` AST node and held in the `VElement` of `<style>`. Default is `true`.
  153. See also to [here](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0043-sfc-style-variables.md).
  154. ## 🎇 Usage for custom rules / plugins
  155. - This parser provides `parserServices` to traverse `<template>`.
  156. - `defineTemplateBodyVisitor(templateVisitor, scriptVisitor, options)` ... returns ESLint visitor to traverse `<template>`.
  157. - `getTemplateBodyTokenStore()` ... returns ESLint `TokenStore` to get the tokens of `<template>`.
  158. - `getDocumentFragment()` ... returns the root `VDocumentFragment`.
  159. - `defineCustomBlocksVisitor(context, customParser, rule, scriptVisitor)` ... returns ESLint visitor that parses and traverses the contents of the custom block.
  160. - `defineDocumentVisitor(documentVisitor, options)` ... returns ESLint visitor to traverses the document.
  161. - [ast.md](./docs/ast.md) is `<template>` AST specification.
  162. - [mustache-interpolation-spacing.js](https://github.com/vuejs/eslint-plugin-vue/blob/b434ff99d37f35570fa351681e43ba2cf5746db3/lib/rules/mustache-interpolation-spacing.js) is an example.
  163. ### `defineTemplateBodyVisitor(templateBodyVisitor, scriptVisitor, options)`
  164. *Arguments*
  165. - `templateBodyVisitor` ... Event handlers for `<template>`.
  166. - `scriptVisitor` ... Event handlers for `<script>` or scripts. (optional)
  167. - `options` ... Options. (optional)
  168. - `templateBodyTriggerSelector` ... Script AST node selector that triggers the templateBodyVisitor. Default is `"Program:exit"`. (optional)
  169. ```ts
  170. import { AST } from "vue-eslint-parser"
  171. export function create(context) {
  172. return context.parserServices.defineTemplateBodyVisitor(
  173. // Event handlers for <template>.
  174. {
  175. VElement(node: AST.VElement): void {
  176. //...
  177. }
  178. },
  179. // Event handlers for <script> or scripts. (optional)
  180. {
  181. Program(node: AST.ESLintProgram): void {
  182. //...
  183. }
  184. },
  185. // Options. (optional)
  186. {
  187. templateBodyTriggerSelector: "Program:exit"
  188. }
  189. )
  190. }
  191. ```
  192. ## ⚠️ Known Limitations
  193. Some rules make warnings due to the outside of `<script>` tags.
  194. Please disable those rules for `.vue` files as necessary.
  195. - [eol-last](http://eslint.org/docs/rules/eol-last)
  196. - [linebreak-style](http://eslint.org/docs/rules/linebreak-style)
  197. - [max-len](http://eslint.org/docs/rules/max-len)
  198. - [max-lines](http://eslint.org/docs/rules/max-lines)
  199. - [no-trailing-spaces](http://eslint.org/docs/rules/no-trailing-spaces)
  200. - [unicode-bom](http://eslint.org/docs/rules/unicode-bom)
  201. - Other rules which are using the source code text instead of AST might be confused as well.
  202. ## 📰 Changelog
  203. - [GitHub Releases](https://github.com/vuejs/vue-eslint-parser/releases)
  204. ## 🍻 Contributing
  205. Welcome contributing!
  206. Please use GitHub's Issues/PRs.
  207. If you want to write code, please execute `npm install && npm run setup` after you cloned this repository.
  208. The `npm install` command installs dependencies.
  209. The `npm run setup` command initializes ESLint as git submodules for tests.
  210. ### Development Tools
  211. - `npm test` runs tests and measures coverage.
  212. - `npm run build` compiles TypeScript source code to `index.js`, `index.js.map`, and `index.d.ts`.
  213. - `npm run coverage` shows the coverage result of `npm test` command with the default browser.
  214. - `npm run clean` removes the temporary files which are created by `npm test` and `npm run build`.
  215. - `npm run lint` runs ESLint.
  216. - `npm run setup` setups submodules to develop.
  217. - `npm run update-fixtures` updates files in `test/fixtures/ast` directory based on `test/fixtures/ast/*/source.vue` files.
  218. - `npm run watch` runs `build`, `update-fixtures`, and tests with `--watch` option.