21c1b1a41a1b644a77cb60138048031f92e8e4b59df7e9af27bfe341fbf2fa556aa0d5baaa35859f364cbe29036b1068a6611769a3cb1feb950611620cadbb 8.9 KB

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