File tree Expand file tree Collapse file tree 3 files changed +53
-1
lines changed Expand file tree Collapse file tree 3 files changed +53
-1
lines changed Original file line number Diff line number Diff line change 1
1
import * as path from "path"
2
+ import type { IntermediateToken } from "../html/intermediate-tokenizer"
2
3
import type { VDocumentFragment } from "../ast"
3
4
import { getLang , isScriptElement , isScriptSetupElement } from "./ast-utils"
4
5
6
+ interface TemplateTokenizer {
7
+ nextToken ( ) : IntermediateToken
8
+ }
9
+
10
+ interface TemplateTokenizerConstructor {
11
+ new (
12
+ templateText : string ,
13
+ source : string ,
14
+ options : { startingLine : number ; startingColumn : number } ,
15
+ ) : TemplateTokenizer
16
+ }
17
+
5
18
export interface ParserOptions {
6
19
// vue-eslint-parser options
7
20
parser ?: boolean | string
@@ -37,6 +50,8 @@ export interface ParserOptions {
37
50
38
51
// others
39
52
// [key: string]: any
53
+
54
+ templateTokenizer ?: TemplateTokenizerConstructor
40
55
}
41
56
42
57
export function isSFCFile ( parserOptions : ParserOptions ) {
Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import type {
15
15
VDocumentFragment ,
16
16
VElement ,
17
17
VExpressionContainer ,
18
+ VLiteral ,
18
19
} from "../ast"
19
20
import { NS , ParseError } from "../ast"
20
21
import { debug } from "../common/debug"
@@ -641,6 +642,38 @@ export class Parser {
641
642
debug ( "[html] Text %j" , token )
642
643
643
644
const parent = this . currentNode
645
+ if ( parent . type === "VElement" && parent . name === "template" ) {
646
+ const langAttribute = parent . startTag . attributes . find (
647
+ ( a ) => a . key . name === "lang" ,
648
+ )
649
+ const lang = ( langAttribute ?. value as VLiteral ) . value
650
+ if (
651
+ lang &&
652
+ lang !== "html" &&
653
+ this . baseParserOptions . templateTokenizer [ lang ]
654
+ ) {
655
+ // eslint-disable-next-line @typescript-eslint/no-require-imports
656
+ const TemplateTokenizer = require ( this . baseParserOptions
657
+ . templateTokenizer [ lang ] )
658
+ const templateTokenizer = new TemplateTokenizer (
659
+ token . value ,
660
+ this . text ,
661
+ {
662
+ startingLine : token . loc . start . line ,
663
+ startingColumn : token . loc . start . column ,
664
+ } ,
665
+ )
666
+
667
+ let templateToken : IntermediateToken | null = null
668
+ while (
669
+ ( templateToken = templateTokenizer . nextToken ( ) ) != null
670
+ ) {
671
+ ; ( this as any ) [ templateToken . type ] ( templateToken )
672
+ }
673
+ // TODO integrate templateTokenizer.tokens/errors/comments
674
+ return
675
+ }
676
+ }
644
677
parent . children . push ( {
645
678
type : "VText" ,
646
679
range : token . range ,
Original file line number Diff line number Diff line change @@ -43,6 +43,7 @@ function isVueFile(code: string, options: ParserOptions): boolean {
43
43
* @param parserOptions The parser options.
44
44
* @returns The parsing result.
45
45
*/
46
+ // eslint-disable-next-line complexity
46
47
export function parseForESLint (
47
48
code : string ,
48
49
parserOptions : any ,
@@ -97,13 +98,16 @@ export function parseForESLint(
97
98
const scripts = rootAST . children . filter ( isScriptElement )
98
99
const template = rootAST . children . find ( isTemplateElement )
99
100
const templateLang = getLang ( template ) || "html"
101
+ const hasTemplateTokenizer =
102
+ parserOptions . templateTokenizer [ templateLang ]
100
103
const concreteInfo : AST . HasConcreteInfo = {
101
104
tokens : rootAST . tokens ,
102
105
comments : rootAST . comments ,
103
106
errors : rootAST . errors ,
104
107
}
105
108
const templateBody =
106
- template != null && templateLang === "html"
109
+ template != null &&
110
+ ( templateLang === "html" || hasTemplateTokenizer )
107
111
? Object . assign ( template , concreteInfo )
108
112
: undefined
109
113
You can’t perform that action at this time.
0 commit comments