-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
71 lines (62 loc) · 1.17 KB
/
index.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
'use strict';
const nodes = require('nunjucks/src/nodes');
const NODE_KEYS = [
'args',
'arr',
'body',
'cond',
'expr',
'else_',
'left',
'name',
'right',
'target',
'template',
'val',
'value',
];
const CHILD_KEYS = [
'children',
'ops',
'targets',
];
const NODE_NAMES = Object.keys(nodes);
const getNodeType = (node) => {
var type;
return node.type || (NODE_NAMES.some((name) => {
if (node.constructor === nodes[name]) {
return type = name;
}
}), type);
};
const normalize = (node) => {
return walk(node, n => {
n.type = getNodeType(n);
});
};
const clean = (node) => {
return walk(node, n => {
delete n.lineno;
delete n.colno;
delete n.parent;
});
};
const walk = (node, func) => {
if (func(node) !== false) {
CHILD_KEYS
.filter(key => Array.isArray(node[key]))
.forEach(key => {
node[key].forEach(child => walk(child, func));
});
NODE_KEYS
.filter(key => node[key] && typeof node[key] === 'object')
.forEach(key => walk(node[key], func));
}
return node;
};
module.exports = {
clean: clean,
normalize: normalize,
getNodeType: getNodeType,
walk: walk
};