forked from ember-template-lint/ember-template-lint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast-node-info.js
209 lines (173 loc) · 4.38 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
'use strict';
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 isTextNode(node) {
return node.type === 'TextNode';
}
function isAttrNode(node) {
return node.type === 'AttrNode';
}
function isCommentStatement(node) {
return node.type === 'CommentStatement';
}
function isConcatStatement(node) {
return node.type === 'ConcatStatement';
}
function isMustacheCommentStatement(node) {
return node.type === 'MustacheCommentStatement';
}
function isPathExpression(node) {
return node.type === 'PathExpression';
}
function isSubExpression(node) {
return node.type === 'SubExpression';
}
function isElementNode(node) {
return node && node.type === 'ElementNode';
}
function isComponentNode(node) {
return node && node.type === 'ComponentNode';
}
function isMustacheStatement(node) {
return node.type === 'MustacheStatement';
}
function isBlockStatement(node) {
return node.type === 'BlockStatement';
}
function isStringLiteral(node) {
return node.type === 'StringLiteral';
}
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 hasAttributeValue(node, attributeName, nodeAttributeValue) {
const attributeNode = findAttribute(node, attributeName);
if (!attributeNode) {
return false;
}
return attributeValue(attributeNode) === nodeAttributeValue;
}
function elementAttributeValue(node, attributeName) {
return attributeValue(findAttribute(node, attributeName));
}
function hasAnyAttribute(node, attributeNames) {
return attributeNames.map((name) => hasAttribute(node, name)).some((exists) => exists === true);
}
function attributeValue(attrNode) {
if (!attrNode) {
return;
}
if (attrNode.value && attrNode.value.type === 'TextNode') {
return attrNode.value.chars;
} else {
return attrNode.value;
}
}
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 isInputElement(node) {
return node.tag === 'input';
}
function isImgElement(node) {
return node.tag === 'img';
}
function isLinkElement(node) {
return node.tag === 'a';
}
function isObjectElement(node) {
return node.tag === 'object';
}
function isAreaElement(node) {
return node.tag === 'area';
}
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.concat(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);
}
module.exports = {
childrenFor,
findAttribute,
hasAttribute,
hasChildren,
hasAnyAttribute,
hasAttributeValue,
attributeValue,
elementAttributeValue,
isBlockStatement,
isStringLiteral,
isIf,
isUnless,
isEach,
isEachIn,
isLet,
isWith,
isControlFlowHelper,
isCommentStatement,
isConcatStatement,
isMustacheCommentStatement,
isPathExpression,
isSubExpression,
isComponentNode,
isConfigurationHtmlComment,
isElementNode,
isInputElement,
isObjectElement,
isAreaElement,
isImgElement,
isLinkElement,
isMustacheStatement,
isNonConfigurationHtmlComment,
isTextNode,
isAttrNode,
};