You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* add support for custom template tokenizers
* fix types
* Make existing tests pass
* merge tokens into existing tokenizer
* add ast test
* Write documentation for custom template tokenizers
* forward tokenizer controls to custom tokenizer
* document attributes used to control tokenizer
* refactor template text tokenizing into separate method.
* fix mock tokenizer token ranges
* guard against empty text nodes
* don't parse mustaches when template lang isn't html
* test if tokenizer gets unprocessed mustaches
* don't call the custom tokinzer on root text nodes if we are already processing a custom template lang
* don't have empty tokens in custom tokenizer
* add disclaimer for templateTokenizer option
* prevent nested template parsing by checking if template is top level instead of maintaining a flag
Copy file name to clipboardExpand all lines: README.md
+23Lines changed: 23 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -194,6 +194,29 @@ If set to `true`, to parse expressions in `v-bind` CSS functions inside `<style>
194
194
195
195
See also to [here](https://github.com/vuejs/rfcs/blob/master/active-rfcs/0043-sfc-style-variables.md).
196
196
197
+
### parserOptions.templateTokenizer
198
+
199
+
You can use `parserOptions.templateTokenizer` property to specify custom tokenizers to parse `<template lang="...">` tags.
200
+
201
+
For example to enable parsing of pug templates:
202
+
203
+
```jsonc
204
+
{
205
+
"parser":"vue-eslint-parser",
206
+
"parserOptions": {
207
+
"templateTokenizer": {
208
+
// template tokenizer for `<template lang="pug">`
209
+
"pug":"vue-eslint-parser-template-tokenizer-pug",
210
+
}
211
+
}
212
+
}
213
+
```
214
+
215
+
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.
216
+
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.
217
+
218
+
See [implementing-custom-template-tokenizers.md](./docs/implementing-custom-template-tokenizers.md) for information on creating your own template tokenizer.
219
+
197
220
## 🎇 Usage for custom rules / plugins
198
221
199
222
- This parser provides `parserServices` to traverse `<template>`.
A custom template tokenizer needs to create two types of tokens from the text it is given:
4
+
5
+
- Low level [tokens](https://github.com/vuejs/vue-eslint-parser/blob/master/src/ast/tokens.ts), which can be of an [existing HTML type](https://github.com/vuejs/vue-eslint-parser/blob/master/src/html/tokenizer.ts#L59) or even new types.
6
+
- Intermediate tokens, which **must** be of type `StartTag`, `EndTag`, `Text` or `Mustache` (see [IntermediateTokenizer](https://github.com/vuejs/vue-eslint-parser/blob/master/src/html/intermediate-tokenizer.ts#L33)).
7
+
8
+
Token ranges and locations must count from the start of the document. To help with this, custom tokenizers are initialized with a starting line and column.
9
+
10
+
## Interface
11
+
12
+
```ts
13
+
classCustomTokenizer {
14
+
/**
15
+
* The tokenized low level tokens, excluding comments.
16
+
*/
17
+
tokens:Token[]
18
+
/**
19
+
* The tokenized low level comment tokens
20
+
*/
21
+
comments:Token[]
22
+
errors:ParseError[]
23
+
24
+
/**
25
+
* Used to control tokenization of {{ expressions }}. If false, don't produce VExpressionStart/End tokens
26
+
*/
27
+
expressionEnabled:boolean=true
28
+
29
+
/**
30
+
* The current namespace. Set and used by the parser. You probably can ignore this.
31
+
*/
32
+
namespace:string="http://www.w3.org/1999/xhtml"
33
+
34
+
/**
35
+
* The current tokenizer state. Set by the parser. You can probably ignore this.
36
+
*/
37
+
state:string="DATA"
38
+
39
+
/**
40
+
* The complete source code text. Used by the parser and set via the constructor.
41
+
*/
42
+
text:string
43
+
44
+
/**
45
+
* Initialize this tokenizer.
46
+
* @paramtemplateText The contents of the <template> tag.
47
+
* @paramtext The complete source code
48
+
* @param{startingLine, startingColumn}The starting location of the templateText. Your token positions need to include this offset.
49
+
*/
50
+
constructor (templateText:string, text:string, { startingLine: number, startingColumn: number }) {
51
+
this.text=text
52
+
}
53
+
54
+
/**
55
+
* Get the next intermediate token.
56
+
* @returns The intermediate token or null.
57
+
*/
58
+
nextToken ():IntermediateToken|null {
59
+
60
+
}
61
+
}
62
+
```
63
+
64
+
## Behaviour
65
+
66
+
When the html parser encounters a `<template lang="...">` tag that matches a configured custom tokenizer, it will initialize a new instance of this tokenizer with the contents of the template tag. It will then call the `nextToken` method of this tokenizer until it returns `null`. After having consumed all intermediate tokens it will copy the low level tokens, comments and errors from the tokenizer instance.
67
+
68
+
## Examples
69
+
70
+
For a working example, see [vue-eslint-parser-template-tokenizer-pug](https://github.com/rashfael/vue-eslint-parser-template-tokenizer-pug/).
// Referred to the Vue parser. See https://github.com/vuejs/vue-next/blob/cbaa3805064cb581fc2007cf63774c91d39844fe/packages/compiler-sfc/src/parse.ts#L127
0 commit comments