-
Notifications
You must be signed in to change notification settings - Fork 235
/
ast-node-info.js
106 lines (90 loc) · 2.34 KB
/
ast-node-info.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
function isConfigurationHtmlComment(node) {
return node.type === 'CommentStatement' && node.value.trim().indexOf('template-lint ') === 0;
}
function isNonConfigurationHtmlComment(node) {
return node.type === 'CommentStatement' && node.value.trim().indexOf('template-lint ') !== 0;
}
function isIf(node) {
return node.path && node.path.original === 'if';
}
function isUnless(node) {
return node.path && node.path.original === 'unless';
}
function isEach(node) {
return node.path && node.path.original === 'each';
}
function isEachIn(node) {
return node.path && node.path.original === 'each-in';
}
function isLet(node) {
return node.path && node.path.original === 'let';
}
function isWith(node) {
return node.path && node.path.original === 'with';
}
function isControlFlowHelper(node) {
return (
isIf(node) || isUnless(node) || isEach(node) || isEachIn(node) || isLet(node) || isWith(node)
);
}
function hasAttribute(node, attributeName) {
let attribute = findAttribute(node, attributeName);
return Boolean(attribute);
}
function hasAnyAttribute(node, attributeNames) {
return attributeNames.map((name) => hasAttribute(node, name)).includes(true);
}
function findAttribute(node, attributeName) {
if (!node.attributes || !node.attributes.length) {
return;
}
for (let i = 0; i < node.attributes.length; i++) {
let attribute = node.attributes[i];
if (attribute.name === attributeName) {
return attribute;
}
}
}
function childrenFor(node) {
if (node.type === 'Program' || node.type === 'Block' || node.type === 'Template') {
return node.body;
}
if (node.type === 'BlockStatement') {
if (node.inverse) {
return [...node.program.body, ...node.inverse.body];
}
return node.program.body;
}
if (node.type === 'ElementNode') {
return node.children;
}
}
function hasChildren(node) {
let children = childrenFor(node);
return Boolean(children && children.length);
}
function attributeTextValue(node) {
if (!node) {
return;
}
if (node.value && node.value.type === 'TextNode') {
return node.value.chars;
}
}
export default {
attributeTextValue,
childrenFor,
findAttribute,
hasAttribute,
hasChildren,
hasAnyAttribute,
isIf,
isUnless,
isEach,
isEachIn,
isLet,
isWith,
isControlFlowHelper,
isConfigurationHtmlComment,
isNonConfigurationHtmlComment,
};