-
-
Notifications
You must be signed in to change notification settings - Fork 77
Custom template tokenizers #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
9720b51
add support for custom template tokenizers
rashfael b2ae37f
fix types
rashfael 2143d85
Make existing tests pass
rashfael afae11c
Merge remote-tracking branch 'upstream/master' into custom-template-t…
rashfael 3b947fd
merge tokens into existing tokenizer
rashfael 9053d67
add ast test
rashfael bd0dc08
Write documentation for custom template tokenizers
rashfael e35d98b
forward tokenizer controls to custom tokenizer
rashfael e740416
document attributes used to control tokenizer
rashfael f290737
refactor template text tokenizing into separate method.
rashfael d86b569
fix mock tokenizer token ranges
rashfael 92eaa9f
guard against empty text nodes
rashfael 90238ed
don't parse mustaches when template lang isn't html
rashfael 3188731
test if tokenizer gets unprocessed mustaches
rashfael 3567b12
don't call the custom tokinzer on root text nodes if we are already p…
rashfael ac34a9d
don't have empty tokens in custom tokenizer
rashfael 6a60aae
add disclaimer for templateTokenizer option
rashfael 8a85f69
prevent nested template parsing by checking if template is top level …
rashfael File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Implementing custom template tokenizers | ||
|
||
A custom template tokenizer needs to create two types of tokens from the text it is given: | ||
|
||
- 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. | ||
- 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)). | ||
|
||
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. | ||
|
||
## Interface | ||
|
||
```ts | ||
class CustomTokenizer { | ||
/** | ||
* The tokenized low level tokens, excluding comments. | ||
*/ | ||
tokens: Token[] | ||
/** | ||
* The tokenized low level comment tokens | ||
*/ | ||
comments: Token[] | ||
errors: ParseError[] | ||
|
||
/** | ||
* Used to control tokenization of {{ expressions }}. If false, don't produce VExpressionStart/End tokens | ||
*/ | ||
expressionEnabled: boolean = true | ||
|
||
/** | ||
* The current namespace. Set and used by the parser. You probably can ignore this. | ||
*/ | ||
namespace: string = "http://www.w3.org/1999/xhtml" | ||
|
||
/** | ||
* The current tokenizer state. Set by the parser. You can probably ignore this. | ||
*/ | ||
state: string = "DATA" | ||
|
||
/** | ||
* The complete source code text. Used by the parser and set via the constructor. | ||
*/ | ||
text: string | ||
|
||
/** | ||
* Initialize this tokenizer. | ||
* @param templateText The contents of the <template> tag. | ||
* @param text The complete source code | ||
* @param {startingLine, startingColumn} The starting location of the templateText. Your token positions need to include this offset. | ||
*/ | ||
constructor (templateText: string, text: string, { startingLine: number, startingColumn: number }) { | ||
this.text = text | ||
} | ||
|
||
/** | ||
* Get the next intermediate token. | ||
* @returns The intermediate token or null. | ||
*/ | ||
nextToken (): IntermediateToken | null { | ||
|
||
} | ||
} | ||
``` | ||
|
||
## Behaviour | ||
|
||
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. | ||
|
||
## Examples | ||
|
||
For a working example, see [vue-eslint-parser-template-tokenizer-pug](https://github.com/rashfael/vue-eslint-parser-template-tokenizer-pug/). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.