forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodeAttributes.js
40 lines (39 loc) · 1.01 KB
/
nodeAttributes.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
// Return attributes and values of a node in a convenient way:
/* example:
<ExampleElement attr1="15" attr2>
{ attr1: {
hasValue: true,
value: 15
},
attr2: {
hasValue: false
}
Inclusion of hasValue is in case an eslint rule cares about boolean values
explicitely assigned to attribute vs the attribute being used as a flag
*/
class NodeAttributes {
constructor(ASTnode) {
this.attributes = {}
ASTnode.attributes.forEach((attribute) => {
this.attributes[attribute.name.name] = {
hasValue: !!attribute.value,
}
if (attribute.value) {
this.attributes[attribute.name.name].value = attribute.value.value
}
})
}
has(attrName) {
return !!this.attributes[attrName]
}
hasValue(attrName) {
return !!this.attributes[attrName].hasValue
}
value(attrName) {
if (!this.attributes[attrName]) {
return true
}
return this.attributes[attrName].value
}
}
module.exports = NodeAttributes