-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathast-utils.ts
54 lines (49 loc) · 1.71 KB
/
ast-utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import type { VAttribute, VDirective, VElement, VNode } from "../ast"
/**
* Check whether the node is a `<script>` element.
* @param node The node to check.
* @returns `true` if the node is a `<script>` element.
*/
export function isScriptElement(node: VNode): node is VElement {
return node.type === "VElement" && node.name === "script"
}
/**
* Checks whether the given script element is `<script setup>`.
*/
export function isScriptSetupElement(script: VElement): boolean {
return (
isScriptElement(script) &&
script.startTag.attributes.some(
(attr) => !attr.directive && attr.key.name === "setup",
)
)
}
/**
* Check whether the node is a `<template>` element.
* @param node The node to check.
* @returns `true` if the node is a `<template>` element.
*/
export function isTemplateElement(node: VNode): node is VElement {
return node.type === "VElement" && node.name === "template"
}
/**
* Check whether the attribute node is a `lang` attribute.
* @param attribute The attribute node to check.
* @returns `true` if the attribute node is a `lang` attribute.
*/
export function isLang(
attribute: VAttribute | VDirective,
): attribute is VAttribute {
return attribute.directive === false && attribute.key.name === "lang"
}
/**
* Get the `lang` attribute value from a given element.
* @param element The element to get.
* @param defaultLang The default value of the `lang` attribute.
* @returns The `lang` attribute value.
*/
export function getLang(element: VElement | undefined): string | null {
const langAttr = element && element.startTag.attributes.find(isLang)
const lang = langAttr && langAttr.value && langAttr.value.value
return lang || null
}