1 | /*
|
---|
2 | Copyright 2015, Yahoo Inc.
|
---|
3 | Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
|
---|
4 | */
|
---|
5 | 'use strict';
|
---|
6 |
|
---|
7 | const debug = require('debug')('istanbuljs');
|
---|
8 | const libCoverage = require('istanbul-lib-coverage');
|
---|
9 | const { MappedCoverage } = require('./mapped');
|
---|
10 | const getMapping = require('./get-mapping');
|
---|
11 | const { getUniqueKey, getOutput } = require('./transform-utils');
|
---|
12 |
|
---|
13 | class SourceMapTransformer {
|
---|
14 | constructor(finder, opts = {}) {
|
---|
15 | this.finder = finder;
|
---|
16 | this.baseDir = opts.baseDir || process.cwd();
|
---|
17 | }
|
---|
18 |
|
---|
19 | processFile(fc, sourceMap, coverageMapper) {
|
---|
20 | let changes = 0;
|
---|
21 |
|
---|
22 | Object.entries(fc.statementMap).forEach(([s, loc]) => {
|
---|
23 | const hits = fc.s[s];
|
---|
24 | const mapping = getMapping(sourceMap, loc, fc.path);
|
---|
25 |
|
---|
26 | if (mapping) {
|
---|
27 | changes += 1;
|
---|
28 | const mappedCoverage = coverageMapper(mapping.source);
|
---|
29 | mappedCoverage.addStatement(mapping.loc, hits);
|
---|
30 | }
|
---|
31 | });
|
---|
32 |
|
---|
33 | Object.entries(fc.fnMap).forEach(([f, fnMeta]) => {
|
---|
34 | const hits = fc.f[f];
|
---|
35 | const mapping = getMapping(sourceMap, fnMeta.decl, fc.path);
|
---|
36 | const spanMapping = getMapping(sourceMap, fnMeta.loc, fc.path);
|
---|
37 |
|
---|
38 | if (
|
---|
39 | mapping &&
|
---|
40 | spanMapping &&
|
---|
41 | mapping.source === spanMapping.source
|
---|
42 | ) {
|
---|
43 | changes += 1;
|
---|
44 | const mappedCoverage = coverageMapper(mapping.source);
|
---|
45 | mappedCoverage.addFunction(
|
---|
46 | fnMeta.name,
|
---|
47 | mapping.loc,
|
---|
48 | spanMapping.loc,
|
---|
49 | hits
|
---|
50 | );
|
---|
51 | }
|
---|
52 | });
|
---|
53 |
|
---|
54 | Object.entries(fc.branchMap).forEach(([b, branchMeta]) => {
|
---|
55 | const hits = fc.b[b];
|
---|
56 | const locs = [];
|
---|
57 | const mappedHits = [];
|
---|
58 | let source;
|
---|
59 | let skip;
|
---|
60 |
|
---|
61 | branchMeta.locations.forEach((loc, i) => {
|
---|
62 | const mapping = getMapping(sourceMap, loc, fc.path);
|
---|
63 | if (mapping) {
|
---|
64 | if (!source) {
|
---|
65 | source = mapping.source;
|
---|
66 | }
|
---|
67 |
|
---|
68 | if (mapping.source !== source) {
|
---|
69 | skip = true;
|
---|
70 | }
|
---|
71 |
|
---|
72 | locs.push(mapping.loc);
|
---|
73 | mappedHits.push(hits[i]);
|
---|
74 | }
|
---|
75 | });
|
---|
76 |
|
---|
77 | if (!skip && locs.length > 0) {
|
---|
78 | changes += 1;
|
---|
79 | const mappedCoverage = coverageMapper(source);
|
---|
80 | mappedCoverage.addBranch(
|
---|
81 | branchMeta.type,
|
---|
82 | locs[0] /* XXX */,
|
---|
83 | locs,
|
---|
84 | mappedHits
|
---|
85 | );
|
---|
86 | }
|
---|
87 | });
|
---|
88 |
|
---|
89 | return changes > 0;
|
---|
90 | }
|
---|
91 |
|
---|
92 | async transform(coverageMap) {
|
---|
93 | const uniqueFiles = {};
|
---|
94 | const getMappedCoverage = file => {
|
---|
95 | const key = getUniqueKey(file);
|
---|
96 | if (!uniqueFiles[key]) {
|
---|
97 | uniqueFiles[key] = {
|
---|
98 | file,
|
---|
99 | mappedCoverage: new MappedCoverage(file)
|
---|
100 | };
|
---|
101 | }
|
---|
102 |
|
---|
103 | return uniqueFiles[key].mappedCoverage;
|
---|
104 | };
|
---|
105 |
|
---|
106 | for (const file of coverageMap.files()) {
|
---|
107 | const fc = coverageMap.fileCoverageFor(file);
|
---|
108 | const sourceMap = await this.finder(file, fc);
|
---|
109 |
|
---|
110 | if (sourceMap) {
|
---|
111 | const changed = this.processFile(
|
---|
112 | fc,
|
---|
113 | sourceMap,
|
---|
114 | getMappedCoverage
|
---|
115 | );
|
---|
116 | if (!changed) {
|
---|
117 | debug(`File [${file}] ignored, nothing could be mapped`);
|
---|
118 | }
|
---|
119 | } else {
|
---|
120 | uniqueFiles[getUniqueKey(file)] = {
|
---|
121 | file,
|
---|
122 | mappedCoverage: new MappedCoverage(fc)
|
---|
123 | };
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | return libCoverage.createCoverageMap(getOutput(uniqueFiles));
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | module.exports = {
|
---|
132 | SourceMapTransformer
|
---|
133 | };
|
---|