forked from SugarCoatJS/sugarcoat
-
Notifications
You must be signed in to change notification settings - Fork 2
/
report.js
46 lines (40 loc) · 1.43 KB
/
report.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
// vim: set tw=99 ts=2 sw=2 et:
'use strict';
const path = require('path');
const fs = require('fs-extra');
const Handlebars = require('handlebars');
const { iterate } = require('iterare');
const lineColumn = require('line-column');
const groupBy = require('lodash.groupby');
const sortBy = require('lodash.sortby');
const { VError } = require('verror');
const contextLength = 24;
const report = (reportTemplate, targets) =>
reportTemplate({
targets: iterate(targets).map(({ name, src, trace }) => {
const srcIndex = lineColumn(src);
return {
name,
bindings: sortBy(Object.entries(groupBy(trace, 'binding')), 0).map(([name, accesses]) => ({
name,
accesses: accesses.map(bindingAccess => ({
accessPosition: srcIndex.fromIndex(bindingAccess.position),
accessPrefix: src.substring(
Math.max(0, bindingAccess.position - 1 - contextLength),
bindingAccess.position - 1
),
accessSuffix: src.substring(
bindingAccess.position - 1,
Math.min(src.length, bindingAccess.position - 1 + contextLength)
),
})),
})),
};
}),
});
module.exports = report;
const loadReportTemplate = async reportTemplate =>
Handlebars.compile(
await fs.readFile(path.join(__dirname, 'reportTemplate.handlebars'), { encoding: 'utf8' })
);
report.loadReportTemplate = loadReportTemplate;