forked from OKEAMAH/prettier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
53 lines (45 loc) · 1.4 KB
/
utils.js
File metadata and controls
53 lines (45 loc) · 1.4 KB
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
import htmlVoidElements from "./html-void-elements.evaluate.js";
function isUppercase(string) {
return string.toUpperCase() === string;
}
function isGlimmerComponent(node) {
return (
node.type === "ElementNode" &&
typeof node.tag === "string" &&
!node.tag.startsWith(":") &&
(isUppercase(node.tag[0]) || node.tag.includes("."))
);
}
// https://github.com/glimmerjs/glimmer-vm/blob/ec5648f3895b9ab8d085523be001553746221449/packages/%40glimmer/syntax/lib/generation/printer.ts#L44-L46
function isVoidTag(tag) {
return htmlVoidElements.has(tag.toLowerCase()) && !isUppercase(tag[0]);
}
function isVoidElement(node) {
return (
node.selfClosing === true ||
isVoidTag(node.tag) ||
(isGlimmerComponent(node) &&
node.children.every((node) => isWhitespaceNode(node)))
);
}
function isWhitespaceNode(node) {
return node.type === "TextNode" && !/\S/u.test(node.chars);
}
function isPrettierIgnoreNode(node) {
return (
node?.type === "MustacheCommentStatement" &&
typeof node.value === "string" &&
node.value.trim() === "prettier-ignore"
);
}
function hasPrettierIgnore(path) {
return (
isPrettierIgnoreNode(path.node) ||
(path.isInArray &&
(path.key === "children" ||
path.key === "body" ||
path.key === "parts") &&
isPrettierIgnoreNode(path.siblings[path.index - 2]))
);
}
export { hasPrettierIgnore, isVoidElement, isWhitespaceNode };