-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecursive.js
85 lines (68 loc) · 2.11 KB
/
recursive.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
const walk = require("acorn-walk");
module.exports = function recursiveWalk(tree) {
var functions = {
FunctionExpression: funkedUp,
FunctionDeclaration: funkedUp,
MemberExpression: expressive,
CallExpression: expressive,
Identifier: ender
};
var state = {
keys: [],
keyExpressions: []
};
walk.recursive(tree, state, functions);
return state.keyExpressions;
// expressions.length ?
// expressions.forEach(key => console.log(`${key}`)):
// console.log('No expressions found');;
function funkedUp(node, state, c) {
var newState = Object.create(state);
newState.keys = Object.create(state.keys);
node.params
.filter(p => p.type == 'Identifier')
.forEach(i => newState.keys.push(i.name));
// if(newState.keys.length) {
// console.log(`(${newState.keys.join(', ')}) => {}`);
// }
c(node.body, newState);
}
function expressive(node, state, c){
if(!state.keys.length) { return next(node, state, c); }
var newState = Object.create(state);
newState.currentKeys = newState.currentKeys || [];
var newNode = null;
var keyToPush = '';
switch (node.type) {
case 'CallExpression':
newNode = node.callee;
keyToPush = '()';
break;
default:
newNode = node.object;
keyToPush = node.property.name ?
node.property.name:
`[${node.property.raw}]`;
break;
}
c(newNode, newState);
if(!state.keys.length) { return; }
newState.currentKeys
.push(keyToPush);
//If we are at the top of an expression chain.
var foundKeys = newState.currentKeys;
if(!state.currentKeys && foundKeys.length && state.keys.includes(foundKeys[0])) {
var newKeyExpression = newState.currentKeys.join('.');
state.keyExpressions
.push(newState.currentKeys.join('.').replace(/\.([\[\(])/g, '$1'));
}
}
function ender(node, state, c) {
if(!state.keys.length || !state.currentKeys) { return; }
state.currentKeys
.push(node.name);
}
function next(node, state, c) {
walk.base[node.type](node, state, c);
}
}