| 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 | this.resolveMapping = opts.getMapping || getMapping;
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | processFile(fc, sourceMap, coverageMapper) {
|
|---|
| 21 | let changes = 0;
|
|---|
| 22 |
|
|---|
| 23 | Object.entries(fc.statementMap).forEach(([s, loc]) => {
|
|---|
| 24 | const hits = fc.s[s];
|
|---|
| 25 | const mapping = this.resolveMapping(sourceMap, loc, fc.path);
|
|---|
| 26 |
|
|---|
| 27 | if (mapping) {
|
|---|
| 28 | changes += 1;
|
|---|
| 29 | const mappedCoverage = coverageMapper(mapping.source);
|
|---|
| 30 | mappedCoverage.addStatement(mapping.loc, hits);
|
|---|
| 31 | }
|
|---|
| 32 | });
|
|---|
| 33 |
|
|---|
| 34 | Object.entries(fc.fnMap).forEach(([f, fnMeta]) => {
|
|---|
| 35 | const hits = fc.f[f];
|
|---|
| 36 | const mapping = this.resolveMapping(
|
|---|
| 37 | sourceMap,
|
|---|
| 38 | fnMeta.decl,
|
|---|
| 39 | fc.path
|
|---|
| 40 | );
|
|---|
| 41 |
|
|---|
| 42 | const spanMapping = this.resolveMapping(
|
|---|
| 43 | sourceMap,
|
|---|
| 44 | fnMeta.loc,
|
|---|
| 45 | fc.path
|
|---|
| 46 | );
|
|---|
| 47 |
|
|---|
| 48 | if (
|
|---|
| 49 | mapping &&
|
|---|
| 50 | spanMapping &&
|
|---|
| 51 | mapping.source === spanMapping.source
|
|---|
| 52 | ) {
|
|---|
| 53 | changes += 1;
|
|---|
| 54 | const mappedCoverage = coverageMapper(mapping.source);
|
|---|
| 55 | mappedCoverage.addFunction(
|
|---|
| 56 | fnMeta.name,
|
|---|
| 57 | mapping.loc,
|
|---|
| 58 | spanMapping.loc,
|
|---|
| 59 | hits
|
|---|
| 60 | );
|
|---|
| 61 | }
|
|---|
| 62 | });
|
|---|
| 63 |
|
|---|
| 64 | Object.entries(fc.branchMap).forEach(([b, branchMeta]) => {
|
|---|
| 65 | const hits = fc.b[b];
|
|---|
| 66 | const locs = [];
|
|---|
| 67 | const mappedHits = [];
|
|---|
| 68 | let source;
|
|---|
| 69 | let skip;
|
|---|
| 70 |
|
|---|
| 71 | branchMeta.locations.forEach((loc, i) => {
|
|---|
| 72 | const mapping = this.resolveMapping(sourceMap, loc, fc.path);
|
|---|
| 73 | if (mapping) {
|
|---|
| 74 | if (!source) {
|
|---|
| 75 | source = mapping.source;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | if (mapping.source !== source) {
|
|---|
| 79 | skip = true;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | locs.push(mapping.loc);
|
|---|
| 83 | mappedHits.push(hits[i]);
|
|---|
| 84 | }
|
|---|
| 85 | });
|
|---|
| 86 |
|
|---|
| 87 | const locMapping = branchMeta.loc
|
|---|
| 88 | ? this.resolveMapping(sourceMap, branchMeta.loc, fc.path)
|
|---|
| 89 | : null;
|
|---|
| 90 |
|
|---|
| 91 | if (!skip && locs.length > 0) {
|
|---|
| 92 | changes += 1;
|
|---|
| 93 | const mappedCoverage = coverageMapper(source);
|
|---|
| 94 | mappedCoverage.addBranch(
|
|---|
| 95 | branchMeta.type,
|
|---|
| 96 | locMapping ? locMapping.loc : locs[0],
|
|---|
| 97 | locs,
|
|---|
| 98 | mappedHits
|
|---|
| 99 | );
|
|---|
| 100 | }
|
|---|
| 101 | });
|
|---|
| 102 |
|
|---|
| 103 | return changes > 0;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | async transform(coverageMap) {
|
|---|
| 107 | const uniqueFiles = {};
|
|---|
| 108 | const getMappedCoverage = file => {
|
|---|
| 109 | const key = getUniqueKey(file);
|
|---|
| 110 | if (!uniqueFiles[key]) {
|
|---|
| 111 | uniqueFiles[key] = {
|
|---|
| 112 | file,
|
|---|
| 113 | mappedCoverage: new MappedCoverage(file)
|
|---|
| 114 | };
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | return uniqueFiles[key].mappedCoverage;
|
|---|
| 118 | };
|
|---|
| 119 |
|
|---|
| 120 | for (const file of coverageMap.files()) {
|
|---|
| 121 | const fc = coverageMap.fileCoverageFor(file);
|
|---|
| 122 | const sourceMap = await this.finder(file, fc);
|
|---|
| 123 |
|
|---|
| 124 | if (sourceMap) {
|
|---|
| 125 | const changed = this.processFile(
|
|---|
| 126 | fc,
|
|---|
| 127 | sourceMap,
|
|---|
| 128 | getMappedCoverage
|
|---|
| 129 | );
|
|---|
| 130 | if (!changed) {
|
|---|
| 131 | debug(`File [${file}] ignored, nothing could be mapped`);
|
|---|
| 132 | }
|
|---|
| 133 | } else {
|
|---|
| 134 | uniqueFiles[getUniqueKey(file)] = {
|
|---|
| 135 | file,
|
|---|
| 136 | mappedCoverage: new MappedCoverage(fc)
|
|---|
| 137 | };
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | return libCoverage.createCoverageMap(getOutput(uniqueFiles));
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | module.exports = {
|
|---|
| 146 | SourceMapTransformer
|
|---|
| 147 | };
|
|---|