版博士V2.0程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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