Skip to content

Commit f10d801

Browse files
committed
Chore: small refactoring
1 parent 6906fb1 commit f10d801

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

Diff for: src/index.ts

+22-11
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ function isLang(
5252
return attribute.directive === false && attribute.key.name === "lang"
5353
}
5454

55+
/**
56+
* Get the `lang` attribute value from a given element.
57+
* @param element The element to get.
58+
* @param defaultLang The default value of the `lang` attribute.
59+
* @returns The `lang` attribute value.
60+
*/
61+
function getLang(
62+
element: AST.VElement | undefined,
63+
defaultLang: string,
64+
): string {
65+
const langAttr = element && element.startTag.attributes.find(isLang)
66+
const lang = langAttr && langAttr.value && langAttr.value.value
67+
return lang || defaultLang
68+
}
69+
5570
/**
5671
* Parse the given source code.
5772
* @param code The source code to parse.
@@ -86,27 +101,23 @@ export function parseForESLint(
86101
)
87102
const script = rootAST.children.find(isScriptElement)
88103
const template = rootAST.children.find(isTemplateElement)
89-
const templateLangAttr =
90-
template && template.startTag.attributes.find(isLang)
91-
const templateLang =
92-
(templateLangAttr &&
93-
templateLangAttr.value &&
94-
templateLangAttr.value.value) ||
95-
"html"
104+
const templateLang = getLang(template, "html")
96105
const concreteInfo: AST.HasConcreteInfo = {
97106
tokens: rootAST.tokens,
98107
comments: rootAST.comments,
99108
errors: rootAST.errors,
100109
}
110+
const templateBody =
111+
template != null && templateLang === "html"
112+
? Object.assign(template, concreteInfo)
113+
: undefined
101114

102115
result =
103116
script != null
104117
? parseScriptElement(script, locationCalcurator, options)
105118
: parseScript("", options)
106-
result.ast.templateBody =
107-
template != null && templateLang === "html"
108-
? Object.assign(template, concreteInfo)
109-
: undefined
119+
120+
result.ast.templateBody = templateBody
110121
}
111122

112123
result.services = Object.assign(

0 commit comments

Comments
 (0)