-
Notifications
You must be signed in to change notification settings - Fork 153
/
tree-traversing.js
87 lines (66 loc) · 1.8 KB
/
tree-traversing.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
class TreeTraversing {
statementNotContains(node, type) {
const statement = this.findParentStatement(node)
if (!statement) {
return false
}
const itemOfType = this.findDownType(statement, type)
return itemOfType !== null
}
findParentStatement(node) {
while (node.parent != null && !node.parent.type.includes('Statement')) {
node = node.parent
}
return node.parent
}
findParentType(node, type) {
while (node.parent !== undefined && node.parent.type !== type) {
node = node.parent
}
return node.parent || null
}
findDownType(node, type) {
if (!node || node.type === type) {
return node
} else {
return null
}
}
findTypeInChildren(ctx, type) {
if (ctx.children) {
const items = ctx.children.filter(i => i.constructor.name === type)
return (items.length > 0 && items[0]) || null
} else {
return null
}
}
*findIdentifier(ctx) {
const children = ctx.children
for (let i = 0; i < children.length; i += 1) {
if (children[i].constructor.name === 'IdentifierContext') {
yield children[i]
}
}
return null
}
}
TreeTraversing.typeOf = function typeOf(ctx) {
if (!ctx) {
return ''
}
const className = ctx.constructor.name
const typeName = className.replace('Context', '')
return typeName[0].toLowerCase() + typeName.substring(1)
}
TreeTraversing.hasMethodCalls = function hasMethodCalls(node, methodNames) {
const text = node.memberName
return methodNames.includes(text)
}
TreeTraversing.findPropertyInParents = function findPropertyInParents(node, property) {
let curNode = node
while (curNode !== undefined && !curNode[property]) {
curNode = curNode.parent
}
return curNode && curNode[property]
}
module.exports = TreeTraversing