This repository has been archived by the owner on Dec 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
/
instrumenter.js
154 lines (124 loc) · 4.14 KB
/
instrumenter.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
import istanbul from 'istanbul';
import {transform as babelTransform} from 'babel-core';
import esprima from 'esprima';
import escodegen from 'escodegen';
import {SourceMapConsumer} from 'source-map';
const POSITIONS = ['start', 'end'];
export class Instrumenter extends istanbul.Instrumenter {
constructor (options = {}) {
super();
istanbul.Instrumenter.call(this, options);
this.babelOptions = {
sourceMap: true,
...(options && options.babel || {})
};
}
instrumentSync (code, fileName) {
const result = this._r =
babelTransform(code, { ...this.babelOptions, filename: fileName });
this._babelMap = new SourceMapConsumer(result.map);
// PARSE
let program = esprima.parse(result.code, {
loc: true,
range: true,
tokens: this.opts.preserveComments,
comment: true
});
if (this.opts.preserveComments) {
program = escodegen
.attachComments(program, program.comments, program.tokens);
}
return this.instrumentASTSync(program, fileName, code);
}
getPreamble (sourceCode, emitUseStrict) {
[['s', 'statementMap'], ['f', 'fnMap'], ['b', 'branchMap']]
.forEach(([metricName, metricMapName]) => {
let [metrics, metricMap] = [
this.coverState[metricName],
this.coverState[metricMapName]
];
let transformFctName = `_${metricMapName}Transformer`;
let transformedMetricMap = this[transformFctName](metricMap, metrics)
this.coverState[metricMapName] = transformedMetricMap;
})
return super.getPreamble(sourceCode, emitUseStrict);
}
////
_statementMapTransformer (metrics) {
return Object.keys(metrics)
.map((index) => metrics[index])
.map((statementMeta) => {
let [location] = this._getMetricOriginalLocations([statementMeta]);
return location;
})
.reduce(this._arrayToArrayLikeObject, {});
}
_fnMapTransformer (metrics) {
return Object.keys(metrics)
.map((index) => metrics[index])
.map((fnMeta) => {
let [loc] = this._getMetricOriginalLocations([fnMeta.loc]);
// Force remove the last skip key
if (fnMeta.skip === undefined) {
delete fnMeta.skip;
if (loc.skip !== undefined) {
fnMeta.skip = loc.skip;
}
}
return { ...fnMeta, loc };
})
.reduce(this._arrayToArrayLikeObject, {});
}
_branchMapTransformer (metrics) {
return Object.keys(metrics)
.map((index) => metrics[index])
.map((branchMeta) => {
return {
...branchMeta,
...{
locations: this._getMetricOriginalLocations(branchMeta.locations)
}
};
})
.reduce(this._arrayToArrayLikeObject, {});
}
////
_getMetricOriginalLocations (metricLocations = []) {
let o = { line: 0, column: 0 };
return metricLocations
.map((generatedPositions) => {
return [
this._getOriginalPositionsFor(generatedPositions),
generatedPositions
]
})
.map(([{start, end}, generatedPosition]) => {
let postitions = [start.line, start.column, end.line, end.column];
let isValid = postitions.every((n) => n !== null);
// Matches behavior in _fnMapTransformer above.
if (generatedPosition.skip === undefined) {
delete generatedPosition.skip;
}
return isValid
? { ...generatedPosition, start, end }
: { start: o, end: o, skip: true };
})
}
_getOriginalPositionsFor (generatedPositions = { start: {}, end: {} }) {
return POSITIONS
.map((position) => [generatedPositions[position], position])
.reduce((originalPositions, [generatedPosition, position]) => {
let originalPosition = this._babelMap.originalPositionFor(generatedPosition);
// remove extra keys
delete originalPosition.name;
delete originalPosition.source;
originalPositions[position] = originalPosition;
return originalPositions;
}, {});
}
_arrayToArrayLikeObject (arrayLikeObject, item, index) {
arrayLikeObject[index + 1] = item;
return arrayLikeObject;
};
}