Skip to content

Commit 4b264ad

Browse files
committed
Fix: don't create templateBody node from PUG
PUG has been not supported yet.
1 parent 45ce600 commit 4b264ad

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed

src/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ function isScriptElement(node: AST.VNode): node is AST.VElement {
4141
return node.type === "VElement" && node.name === "script"
4242
}
4343

44+
/**
45+
* Check whether the attribute node is a `lang` attribute.
46+
* @param attribute The attribute node to check.
47+
* @returns `true` if the attribute node is a `lang` attribute.
48+
*/
49+
function isLang(attribute: AST.VAttribute | AST.VDirective): attribute is AST.VAttribute {
50+
return attribute.directive === false && attribute.key.name === "lang"
51+
}
52+
4453
/**
4554
* Parse the given source code.
4655
* @param code The source code to parse.
@@ -66,6 +75,8 @@ export function parseForESLint(code: string, options: any): AST.ESLintExtendedPr
6675
const locationCalcurator = new LocationCalculator(tokenizer.gaps, tokenizer.lineTerminators)
6776
const script = rootAST.children.find(isScriptElement) as AST.VElement | undefined // https://github.com/Microsoft/TypeScript/issues/7657
6877
const template = rootAST.children.find(isTemplateElement) as AST.VElement | undefined
78+
const templateLangAttr = template && template.startTag.attributes.find(isLang) as AST.VAttribute | undefined
79+
const templateLang = (templateLangAttr && templateLangAttr.value && templateLangAttr.value.value) || "html"
6980
const concreteInfo: AST.HasConcreteInfo = {
7081
tokens: rootAST.tokens,
7182
comments: rootAST.comments,
@@ -75,7 +86,7 @@ export function parseForESLint(code: string, options: any): AST.ESLintExtendedPr
7586
result = (script != null)
7687
? parseScriptElement(script, locationCalcurator, options)
7788
: parseScript("", options)
78-
result.ast.templateBody = (template != null)
89+
result.ast.templateBody = (template != null && templateLang === "html")
7990
? Object.assign(template, concreteInfo)
8091
: undefined
8192
}

test/fixtures/ast/pug/ast.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"type": "Program",
3+
"start": 0,
4+
"end": 0,
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 1,
12+
"column": 0
13+
}
14+
},
15+
"range": [
16+
0,
17+
0
18+
],
19+
"body": [],
20+
"sourceType": "script",
21+
"comments": [],
22+
"tokens": []
23+
}

test/fixtures/ast/pug/source.vue

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template lang="pug">
2+
Hello!
3+
</template>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

test/fixtures/ast/pug/tree.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

0 commit comments

Comments
 (0)