-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdescend-visitor.js
102 lines (80 loc) · 1.85 KB
/
descend-visitor.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
'use strict';
const list = {};
const single = {};
module.exports = traverseNode => {
const descendProperties = properties => (node, parent, ast, visitor) => {
const propertiesNames = Object.keys(properties);
if (propertiesNames.every(name => !node[name])) {
return node;
}
const traverse = traverseNode(node, ast, visitor);
const traversedProperties = propertiesNames.map(name => {
try {
const kind = properties[name];
const value = kind === list ?
((node[name] && node[name].map(traverse)) || []) :
traverse(node[name]);
return {[name]: value};
} catch (err) {
throw new Error(
`While traversing property ${name}:${err.message}`
);
}
});
return Object.assign.apply(null, [{}, node].concat(traversedProperties));
};
return {
defaultMethod(node) {
return node;
},
AssignmentWord: descendProperties({expansion: list}),
Script: descendProperties({commands: list}),
CompoundList: descendProperties({commands: list}),
Word: descendProperties({expansion: list}),
Function: descendProperties({
body: single,
redirections: list
}),
Redirect: descendProperties({file: single}),
LogicalExpression: descendProperties({
left: single,
right: single
}),
Case: descendProperties({
cases: list,
clause: single
}),
CaseItem: descendProperties({
pattern: list,
body: single
}),
If: descendProperties({
clause: single,
else: single,
then: single
}),
While: descendProperties({
clause: single,
do: single
}),
Until: descendProperties({
clause: single,
do: single
}),
Command: descendProperties({
name: single,
prefix: list,
suffix: list
}),
For: descendProperties({
wordlist: list,
do: single
}),
Pipeline: descendProperties({
commands: list
}),
Subshell: descendProperties({
list: single
})
};
};