-
Notifications
You must be signed in to change notification settings - Fork 1
/
label.js
69 lines (60 loc) · 1.43 KB
/
label.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
var morphic = require("morphic");
var matchArray = require("./matchers.js").matchArray;
var List = require("immutable").List;
var generateLabels = new morphic();
generateLabels.with(
morphic.Number("position"),
morphic.Object("list"),
{
"type": morphic.String("type")
}
).then(
(r, _1, _2, input) => {
var flatCopy = {};
var pos = r.position + 1;
var nodeList = r.list;
Object.keys(input).forEach(key => {
var out = generateLabels(pos, nodeList, input[key]);
pos = out.pos;
flatCopy[key] = out.copy;
nodeList = out.nodeList;
});
nodeList = nodeList.set(r.position, flatCopy);
return {
pos: pos,
copy: r.position,
nodeList: nodeList
};
}
);
generateLabels.with(
morphic.Number("position"),
morphic.Object("list"),
matchArray("array")
).then(
r => {
var pos = r.position;
var nodeList = r.list;
var nodes = r.array.map(element => {
var out = generateLabels(pos, nodeList, element);
pos = out.pos;
nodeList = out.nodeList;
return out.copy;
});
return {
pos: pos,
copy: nodes,
nodeList: nodeList
};
}
);
// Return the node directly
generateLabels.otherwise().then((_, position, nodeList, node) => ({
pos: position,
copy: node,
nodeList: nodeList
})
);
module.exports = function label(ast) {
return generateLabels(0, new List(), ast).nodeList;
};