-
Notifications
You must be signed in to change notification settings - Fork 5
/
ingest.js
136 lines (116 loc) · 3.45 KB
/
ingest.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// vim: set tw=99 ts=2 sw=2 et:
'use strict';
const { iterate } = require('iterare');
const lineColumn = require('line-column');
const sortBy = require('lodash.sortby');
const { VError } = require('verror');
const ingest = (graphs, targets) => {
const entriesMapBySrc = new Map();
const registerEntryBySrc = (src, entry) => {
const entries = entriesMapBySrc.get(src);
if (entries) {
entries.push(entry);
} else {
entriesMapBySrc.set(src, [entry]);
}
};
const traces = iterate(targets)
.map(target => {
const trace = new Map();
registerEntryBySrc(target.src, { trace });
if (target.alts) {
const origSrcIndex = lineColumn(target.src);
for (const alt of target.alts) {
registerEntryBySrc(alt.src, {
trace,
srcMap: alt.srcMap,
srcIndex: lineColumn(alt.src),
origSrcIndex,
});
}
}
return trace;
})
.toArray();
for (const graph of graphs) {
const entriesMapByNodeKey = new Map();
for (const [scriptNodeKey, scriptNodeAttrs] of graph.nodeEntries()) {
if (scriptNodeAttrs['node type'] !== 'script') {
continue;
}
const scriptSrc = scriptNodeAttrs['source'];
if (scriptSrc == null) {
continue;
}
const entries = entriesMapBySrc.get(scriptSrc);
if (!entries) {
continue;
}
entriesMapByNodeKey.set(scriptNodeKey, entries);
}
for (const [bindingNodeKey, bindingNodeAttrs] of graph.nodeEntries()) {
if (bindingNodeAttrs['node type'] !== 'binding') {
continue;
}
const binding = bindingNodeAttrs['binding'];
for (const [
bindingEdgeKey,
bindingEdgeAttrs,
bindingEventNodeKey,
,
bindingEventNodeAttrs,
] of graph.inEdgeEntries(bindingNodeKey)) {
if (bindingEdgeAttrs['edge type'] !== 'binding') {
continue;
}
for (const [
bindingEventEdgeKey,
bindingEventEdgeAttrs,
scriptNodeKey,
,
scriptNodeAttrs,
] of graph.inEdgeEntries(bindingEventNodeKey)) {
const entries = entriesMapByNodeKey.get(scriptNodeKey);
if (!entries) {
continue;
}
const rawPosition = bindingEventEdgeAttrs['script position'];
for (const entry of entries) {
let position = rawPosition;
if (entry.srcMap) {
position = entry.srcIndex.fromIndex(position);
position = srcMap.originalPositionFor({
line: position.line,
column: position.col - 1,
});
if (!position.source) {
// Ignore accesses from within generated code.
continue;
}
position = entry.origSrcIndex.toIndex(position.line, position.column + 1);
}
const bindingSet = entry.trace.get(position);
if (bindingSet) {
bindingSet.add(binding);
} else {
entry.trace.set(position, new Set([binding]));
}
}
break;
}
}
}
}
return traces.map(trace =>
sortBy(
iterate(trace)
.map(([position, bindingSet]) =>
iterate(bindingSet).map(binding => ({ binding, position }))
)
.flatten()
.toArray(),
['position', 'binding']
)
);
};
module.exports = ingest;