diff options
Diffstat (limited to 'node_modules/istanbul-lib-coverage/lib')
5 files changed, 618 insertions, 0 deletions
diff --git a/node_modules/istanbul-lib-coverage/lib/coverage-map.js b/node_modules/istanbul-lib-coverage/lib/coverage-map.js new file mode 100644 index 0000000..0a1ebd0 --- /dev/null +++ b/node_modules/istanbul-lib-coverage/lib/coverage-map.js @@ -0,0 +1,134 @@ +/* + Copyright 2012-2015, Yahoo Inc. + Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. + */ +'use strict'; + +const { FileCoverage } = require('./file-coverage'); +const { CoverageSummary } = require('./coverage-summary'); + +function maybeConstruct(obj, klass) { + if (obj instanceof klass) { + return obj; + } + + return new klass(obj); +} + +function loadMap(source) { + const data = Object.create(null); + if (!source) { + return data; + } + + Object.entries(source).forEach(([k, cov]) => { + data[k] = maybeConstruct(cov, FileCoverage); + }); + + return data; +} + +/** CoverageMap is a map of `FileCoverage` objects keyed by file paths. */ +class CoverageMap { + /** + * @constructor + * @param {Object} [obj=undefined] obj A coverage map from which to initialize this + * map's contents. This can be the raw global coverage object. + */ + constructor(obj) { + if (obj instanceof CoverageMap) { + this.data = obj.data; + } else { + this.data = loadMap(obj); + } + } + + /** + * merges a second coverage map into this one + * @param {CoverageMap} obj - a CoverageMap or its raw data. Coverage is merged + * correctly for the same files and additional file coverage keys are created + * as needed. + */ + merge(obj) { + const other = maybeConstruct(obj, CoverageMap); + Object.values(other.data).forEach(fc => { + this.addFileCoverage(fc); + }); + } + + /** + * filter the coveragemap based on the callback provided + * @param {Function (filename)} callback - Returns true if the path + * should be included in the coveragemap. False if it should be + * removed. + */ + filter(callback) { + Object.keys(this.data).forEach(k => { + if (!callback(k)) { + delete this.data[k]; + } + }); + } + + /** + * returns a JSON-serializable POJO for this coverage map + * @returns {Object} + */ + toJSON() { + return this.data; + } + + /** + * returns an array for file paths for which this map has coverage + * @returns {Array{string}} - array of files + */ + files() { + return Object.keys(this.data); + } + + /** + * returns the file coverage for the specified file. + * @param {String} file + * @returns {FileCoverage} + */ + fileCoverageFor(file) { + const fc = this.data[file]; + if (!fc) { + throw new Error(`No file coverage available for: ${file}`); + } + return fc; + } + + /** + * adds a file coverage object to this map. If the path for the object, + * already exists in the map, it is merged with the existing coverage + * otherwise a new key is added to the map. + * @param {FileCoverage} fc the file coverage to add + */ + addFileCoverage(fc) { + const cov = new FileCoverage(fc); + const { path } = cov; + if (this.data[path]) { + this.data[path].merge(cov); + } else { + this.data[path] = cov; + } + } + + /** + * returns the coverage summary for all the file coverage objects in this map. + * @returns {CoverageSummary} + */ + getCoverageSummary() { + const ret = new CoverageSummary(); + Object.values(this.data).forEach(fc => { + ret.merge(fc.toSummary()); + }); + + return ret; + } +} + +module.exports = { + CoverageMap +}; diff --git a/node_modules/istanbul-lib-coverage/lib/coverage-summary.js b/node_modules/istanbul-lib-coverage/lib/coverage-summary.js new file mode 100644 index 0000000..9b769c6 --- /dev/null +++ b/node_modules/istanbul-lib-coverage/lib/coverage-summary.js @@ -0,0 +1,112 @@ +/* + Copyright 2012-2015, Yahoo Inc. + Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. + */ +'use strict'; + +const percent = require('./percent'); +const dataProperties = require('./data-properties'); + +function blankSummary() { + const empty = () => ({ + total: 0, + covered: 0, + skipped: 0, + pct: 'Unknown' + }); + + return { + lines: empty(), + statements: empty(), + functions: empty(), + branches: empty(), + branchesTrue: empty() + }; +} + +// asserts that a data object "looks like" a summary coverage object +function assertValidSummary(obj) { + const valid = + obj && obj.lines && obj.statements && obj.functions && obj.branches; + if (!valid) { + throw new Error( + 'Invalid summary coverage object, missing keys, found:' + + Object.keys(obj).join(',') + ); + } +} + +/** + * CoverageSummary provides a summary of code coverage . It exposes 4 properties, + * `lines`, `statements`, `branches`, and `functions`. Each of these properties + * is an object that has 4 keys `total`, `covered`, `skipped` and `pct`. + * `pct` is a percentage number (0-100). + */ +class CoverageSummary { + /** + * @constructor + * @param {Object|CoverageSummary} [obj=undefined] an optional data object or + * another coverage summary to initialize this object with. + */ + constructor(obj) { + if (!obj) { + this.data = blankSummary(); + } else if (obj instanceof CoverageSummary) { + this.data = obj.data; + } else { + this.data = obj; + } + assertValidSummary(this.data); + } + + /** + * merges a second summary coverage object into this one + * @param {CoverageSummary} obj - another coverage summary object + */ + merge(obj) { + const keys = [ + 'lines', + 'statements', + 'branches', + 'functions', + 'branchesTrue' + ]; + keys.forEach(key => { + if (obj[key]) { + this[key].total += obj[key].total; + this[key].covered += obj[key].covered; + this[key].skipped += obj[key].skipped; + this[key].pct = percent(this[key].covered, this[key].total); + } + }); + + return this; + } + + /** + * returns a POJO that is JSON serializable. May be used to get the raw + * summary object. + */ + toJSON() { + return this.data; + } + + /** + * return true if summary has no lines of code + */ + isEmpty() { + return this.lines.total === 0; + } +} + +dataProperties(CoverageSummary, [ + 'lines', + 'statements', + 'functions', + 'branches', + 'branchesTrue' +]); + +module.exports = { + CoverageSummary +}; diff --git a/node_modules/istanbul-lib-coverage/lib/data-properties.js b/node_modules/istanbul-lib-coverage/lib/data-properties.js new file mode 100644 index 0000000..3a12d40 --- /dev/null +++ b/node_modules/istanbul-lib-coverage/lib/data-properties.js @@ -0,0 +1,12 @@ +'use strict'; + +module.exports = function dataProperties(klass, properties) { + properties.forEach(p => { + Object.defineProperty(klass.prototype, p, { + enumerable: true, + get() { + return this.data[p]; + } + }); + }); +}; diff --git a/node_modules/istanbul-lib-coverage/lib/file-coverage.js b/node_modules/istanbul-lib-coverage/lib/file-coverage.js new file mode 100644 index 0000000..80a36ab --- /dev/null +++ b/node_modules/istanbul-lib-coverage/lib/file-coverage.js @@ -0,0 +1,345 @@ +/* + Copyright 2012-2015, Yahoo Inc. + Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. + */ +'use strict'; + +const percent = require('./percent'); +const dataProperties = require('./data-properties'); +const { CoverageSummary } = require('./coverage-summary'); + +// returns a data object that represents empty coverage +function emptyCoverage(filePath, reportLogic) { + const cov = { + path: filePath, + statementMap: {}, + fnMap: {}, + branchMap: {}, + s: {}, + f: {}, + b: {} + }; + if (reportLogic) cov.bT = {}; + return cov; +} + +// asserts that a data object "looks like" a coverage object +function assertValidObject(obj) { + const valid = + obj && + obj.path && + obj.statementMap && + obj.fnMap && + obj.branchMap && + obj.s && + obj.f && + obj.b; + if (!valid) { + throw new Error( + 'Invalid file coverage object, missing keys, found:' + + Object.keys(obj).join(',') + ); + } +} + +const keyFromLoc = ({ start, end }) => + `${start.line}|${start.column}|${end.line}|${end.column}`; + +const mergeProp = (aHits, aMap, bHits, bMap, itemKey = keyFromLoc) => { + const aItems = {}; + for (const [key, itemHits] of Object.entries(aHits)) { + const item = aMap[key]; + aItems[itemKey(item)] = [itemHits, item]; + } + for (const [key, bItemHits] of Object.entries(bHits)) { + const bItem = bMap[key]; + const k = itemKey(bItem); + + if (aItems[k]) { + const aPair = aItems[k]; + if (bItemHits.forEach) { + // should this throw an exception if aPair[0] is not an array? + bItemHits.forEach((hits, h) => { + if (aPair[0][h] !== undefined) aPair[0][h] += hits; + else aPair[0][h] = hits; + }); + } else { + aPair[0] += bItemHits; + } + } else { + aItems[k] = [bItemHits, bItem]; + } + } + const hits = {}; + const map = {}; + + Object.values(aItems).forEach(([itemHits, item], i) => { + hits[i] = itemHits; + map[i] = item; + }); + + return [hits, map]; +}; + +/** + * provides a read-only view of coverage for a single file. + * The deep structure of this object is documented elsewhere. It has the following + * properties: + * + * * `path` - the file path for which coverage is being tracked + * * `statementMap` - map of statement locations keyed by statement index + * * `fnMap` - map of function metadata keyed by function index + * * `branchMap` - map of branch metadata keyed by branch index + * * `s` - hit counts for statements + * * `f` - hit count for functions + * * `b` - hit count for branches + */ +class FileCoverage { + /** + * @constructor + * @param {Object|FileCoverage|String} pathOrObj is a string that initializes + * and empty coverage object with the specified file path or a data object that + * has all the required properties for a file coverage object. + */ + constructor(pathOrObj, reportLogic = false) { + if (!pathOrObj) { + throw new Error( + 'Coverage must be initialized with a path or an object' + ); + } + if (typeof pathOrObj === 'string') { + this.data = emptyCoverage(pathOrObj, reportLogic); + } else if (pathOrObj instanceof FileCoverage) { + this.data = pathOrObj.data; + } else if (typeof pathOrObj === 'object') { + this.data = pathOrObj; + } else { + throw new Error('Invalid argument to coverage constructor'); + } + assertValidObject(this.data); + } + + /** + * returns computed line coverage from statement coverage. + * This is a map of hits keyed by line number in the source. + */ + getLineCoverage() { + const statementMap = this.data.statementMap; + const statements = this.data.s; + const lineMap = Object.create(null); + + Object.entries(statements).forEach(([st, count]) => { + /* istanbul ignore if: is this even possible? */ + if (!statementMap[st]) { + return; + } + const { line } = statementMap[st].start; + const prevVal = lineMap[line]; + if (prevVal === undefined || prevVal < count) { + lineMap[line] = count; + } + }); + return lineMap; + } + + /** + * returns an array of uncovered line numbers. + * @returns {Array} an array of line numbers for which no hits have been + * collected. + */ + getUncoveredLines() { + const lc = this.getLineCoverage(); + const ret = []; + Object.entries(lc).forEach(([l, hits]) => { + if (hits === 0) { + ret.push(l); + } + }); + return ret; + } + + /** + * returns a map of branch coverage by source line number. + * @returns {Object} an object keyed by line number. Each object + * has a `covered`, `total` and `coverage` (percentage) property. + */ + getBranchCoverageByLine() { + const branchMap = this.branchMap; + const branches = this.b; + const ret = {}; + Object.entries(branchMap).forEach(([k, map]) => { + const line = map.line || map.loc.start.line; + const branchData = branches[k]; + ret[line] = ret[line] || []; + ret[line].push(...branchData); + }); + Object.entries(ret).forEach(([k, dataArray]) => { + const covered = dataArray.filter(item => item > 0); + const coverage = (covered.length / dataArray.length) * 100; + ret[k] = { + covered: covered.length, + total: dataArray.length, + coverage + }; + }); + return ret; + } + + /** + * return a JSON-serializable POJO for this file coverage object + */ + toJSON() { + return this.data; + } + + /** + * merges a second coverage object into this one, updating hit counts + * @param {FileCoverage} other - the coverage object to be merged into this one. + * Note that the other object should have the same structure as this one (same file). + */ + merge(other) { + if (other.all === true) { + return; + } + + if (this.all === true) { + this.data = other.data; + return; + } + + let [hits, map] = mergeProp( + this.s, + this.statementMap, + other.s, + other.statementMap + ); + this.data.s = hits; + this.data.statementMap = map; + + const keyFromLocProp = x => keyFromLoc(x.loc); + const keyFromLocationsProp = x => keyFromLoc(x.locations[0]); + + [hits, map] = mergeProp( + this.f, + this.fnMap, + other.f, + other.fnMap, + keyFromLocProp + ); + this.data.f = hits; + this.data.fnMap = map; + + [hits, map] = mergeProp( + this.b, + this.branchMap, + other.b, + other.branchMap, + keyFromLocationsProp + ); + this.data.b = hits; + this.data.branchMap = map; + + // Tracking additional information about branch truthiness + // can be optionally enabled: + if (this.bT && other.bT) { + [hits, map] = mergeProp( + this.bT, + this.branchMap, + other.bT, + other.branchMap, + keyFromLocationsProp + ); + this.data.bT = hits; + } + } + + computeSimpleTotals(property) { + let stats = this[property]; + + if (typeof stats === 'function') { + stats = stats.call(this); + } + + const ret = { + total: Object.keys(stats).length, + covered: Object.values(stats).filter(v => !!v).length, + skipped: 0 + }; + ret.pct = percent(ret.covered, ret.total); + return ret; + } + + computeBranchTotals(property) { + const stats = this[property]; + const ret = { total: 0, covered: 0, skipped: 0 }; + + Object.values(stats).forEach(branches => { + ret.covered += branches.filter(hits => hits > 0).length; + ret.total += branches.length; + }); + ret.pct = percent(ret.covered, ret.total); + return ret; + } + + /** + * resets hit counts for all statements, functions and branches + * in this coverage object resulting in zero coverage. + */ + resetHits() { + const statements = this.s; + const functions = this.f; + const branches = this.b; + const branchesTrue = this.bT; + Object.keys(statements).forEach(s => { + statements[s] = 0; + }); + Object.keys(functions).forEach(f => { + functions[f] = 0; + }); + Object.keys(branches).forEach(b => { + branches[b].fill(0); + }); + // Tracking additional information about branch truthiness + // can be optionally enabled: + if (branchesTrue) { + Object.keys(branchesTrue).forEach(bT => { + branchesTrue[bT].fill(0); + }); + } + } + + /** + * returns a CoverageSummary for this file coverage object + * @returns {CoverageSummary} + */ + toSummary() { + const ret = {}; + ret.lines = this.computeSimpleTotals('getLineCoverage'); + ret.functions = this.computeSimpleTotals('f', 'fnMap'); + ret.statements = this.computeSimpleTotals('s', 'statementMap'); + ret.branches = this.computeBranchTotals('b'); + // Tracking additional information about branch truthiness + // can be optionally enabled: + if (this['bt']) { + ret.branchesTrue = this.computeBranchTotals('bT'); + } + return new CoverageSummary(ret); + } +} + +// expose coverage data attributes +dataProperties(FileCoverage, [ + 'path', + 'statementMap', + 'fnMap', + 'branchMap', + 's', + 'f', + 'b', + 'bT', + 'all' +]); + +module.exports = { + FileCoverage +}; diff --git a/node_modules/istanbul-lib-coverage/lib/percent.js b/node_modules/istanbul-lib-coverage/lib/percent.js new file mode 100644 index 0000000..c7b7aaf --- /dev/null +++ b/node_modules/istanbul-lib-coverage/lib/percent.js @@ -0,0 +1,15 @@ +/* + Copyright 2012-2015, Yahoo Inc. + Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms. + */ +'use strict'; + +module.exports = function percent(covered, total) { + let tmp; + if (total > 0) { + tmp = (1000 * 100 * covered) / total; + return Math.floor(tmp / 10) / 100; + } else { + return 100.0; + } +}; |