CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
transformer.js161 linesDownload Raw Back to lib
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 7const debug = require('debug')('istanbuljs');8const libCoverage = require('istanbul-lib-coverage');9const { MappedCoverage } = require('./mapped');10const getMapping = require('./get-mapping');11const { getUniqueKey, getOutput } = require('./transform-utils');12 13class 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.path40            );41 42            const spanMapping = this.resolveMapping(43                sourceMap,44                fnMeta.loc,45                fc.path46            );47 48            if (49                mapping &&50                spanMapping &&51                mapping.source === spanMapping.source52            ) {53                changes += 1;54                const mappedCoverage = coverageMapper(mapping.source);55                mappedCoverage.addFunction(56                    fnMeta.name,57                    mapping.loc,58                    spanMapping.loc,59                    hits60                );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                // Check if this is an implicit else86                else if (87                    source &&88                    branchMeta.type === 'if' &&89                    i > 0 &&90                    loc.start.line === undefined &&91                    loc.start.end === undefined &&92                    loc.end.line === undefined &&93                    loc.end.end === undefined94                ) {95                    locs.push(loc);96                    mappedHits.push(hits[i]);97                }98            });99 100            const locMapping = branchMeta.loc101                ? this.resolveMapping(sourceMap, branchMeta.loc, fc.path)102                : null;103 104            if (!skip && locs.length > 0) {105                changes += 1;106                const mappedCoverage = coverageMapper(source);107                mappedCoverage.addBranch(108                    branchMeta.type,109                    locMapping ? locMapping.loc : locs[0],110                    locs,111                    mappedHits112                );113            }114        });115 116        return changes > 0;117    }118 119    async transform(coverageMap) {120        const uniqueFiles = {};121        const getMappedCoverage = file => {122            const key = getUniqueKey(file);123            if (!uniqueFiles[key]) {124                uniqueFiles[key] = {125                    file,126                    mappedCoverage: new MappedCoverage(file)127                };128            }129 130            return uniqueFiles[key].mappedCoverage;131        };132 133        for (const file of coverageMap.files()) {134            const fc = coverageMap.fileCoverageFor(file);135            const sourceMap = await this.finder(file, fc);136 137            if (sourceMap) {138                const changed = this.processFile(139                    fc,140                    sourceMap,141                    getMappedCoverage142                );143                if (!changed) {144                    debug(`File [${file}] ignored, nothing could be mapped`);145                }146            } else {147                uniqueFiles[getUniqueKey(file)] = {148                    file,149                    mappedCoverage: new MappedCoverage(fc)150                };151            }152        }153 154        return libCoverage.createCoverageMap(getOutput(uniqueFiles));155    }156}157 158module.exports = {159    SourceMapTransformer160};161 
basant307/AI_Governance_Project · CoolFace