Skip to content

Commit 6ca4345

Browse files
authored
feat: add support for 1:1 source-maps (bcoe#85)
BREAKING CHANGE: c8 will now load source-maps if possible and remap coverage accordingly
1 parent fde596e commit 6ca4345

22 files changed

+388
-26
lines changed

bin/c8.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ let argv = buildYargs().parse(instrumenterArgs)
2727
}
2828

2929
process.env.NODE_V8_COVERAGE = argv.tempDirectory
30-
foreground(hideInstrumenterArgs(argv), done => {
31-
outputReport(argv)
30+
foreground(hideInstrumenterArgs(argv), async (done) => {
31+
await outputReport(argv)
3232
done()
3333
})
3434
}

lib/commands/check-coverage.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ exports.handler = function (argv) {
2525
exports.checkCoverages(argv, report)
2626
}
2727

28-
exports.checkCoverages = function (argv, report) {
28+
exports.checkCoverages = async function (argv, report) {
2929
const thresholds = {
3030
lines: argv.lines,
3131
functions: argv.functions,
3232
branches: argv.branches,
3333
statements: argv.statements
3434
}
35-
const map = report.getCoverageMapFromAllCoverageFiles()
35+
const map = await report.getCoverageMapFromAllCoverageFiles()
3636
if (argv.perFile) {
3737
map.files().forEach(file => {
3838
checkCoverage(map.fileCoverageFor(file).toSummary(), thresholds, file)

lib/commands/report.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ exports.command = 'report'
55

66
exports.describe = 'read V8 coverage data from temp and output report'
77

8-
exports.handler = function (argv) {
9-
exports.outputReport(argv)
8+
exports.handler = async function (argv) {
9+
await exports.outputReport(argv)
1010
}
1111

12-
exports.outputReport = function (argv) {
12+
exports.outputReport = async function (argv) {
1313
const report = Report({
1414
include: argv.include,
1515
exclude: argv.exclude,
@@ -21,6 +21,6 @@ exports.outputReport = function (argv) {
2121
omitRelative: argv.omitRelative,
2222
wrapperLength: argv.wrapperLength
2323
})
24-
report.run()
24+
await report.run()
2525
if (argv.checkCoverage) checkCoverages(argv, report)
2626
}

lib/report.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class Report {
3333
this.omitRelative = omitRelative
3434
this.wrapperLength = wrapperLength
3535
}
36-
run () {
37-
const map = this.getCoverageMapFromAllCoverageFiles()
36+
async run () {
37+
const map = await this.getCoverageMapFromAllCoverageFiles()
3838
var context = libReport.createContext({
3939
dir: this.reportsDirectory,
4040
watermarks: this.watermarks
@@ -47,7 +47,7 @@ class Report {
4747
})
4848
}
4949

50-
getCoverageMapFromAllCoverageFiles () {
50+
async getCoverageMapFromAllCoverageFiles () {
5151
// the merge process can be very expensive, and it's often the case that
5252
// check-coverage is called immediately after a report. We memoize the
5353
// result from getCoverageMapFromAllCoverageFiles() to address this
@@ -61,9 +61,10 @@ class Report {
6161
for (const v8ScriptCov of v8ProcessCov.result) {
6262
try {
6363
const path = resolve(this.resolve, v8ScriptCov.url)
64-
const script = v8toIstanbul(path, this.wrapperLength)
65-
script.applyCoverage(v8ScriptCov.functions)
66-
map.merge(script.toIstanbul())
64+
const converter = v8toIstanbul(path, this.wrapperLength)
65+
await converter.load()
66+
converter.applyCoverage(v8ScriptCov.functions)
67+
map.merge(converter.toIstanbul())
6768
} catch (err) {
6869
console.warn(`file: ${v8ScriptCov.url} error: ${err.stack}`)
6970
}

package-lock.json

Lines changed: 15 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@
4141
"istanbul-reports": "^2.0.0",
4242
"rimraf": "^2.6.2",
4343
"test-exclude": "^5.0.0",
44-
"uuid": "^3.3.2",
45-
"v8-to-istanbul": "^2.0.4",
44+
"v8-to-istanbul": "^3.0.1",
4645
"yargs": "^13.1.0",
4746
"yargs-parser": "^10.1.0"
4847
},
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
if (false) {
2+
console.info('unreachable')
3+
} else if (true) {
4+
console.info('reachable')
5+
} else {
6+
console.info('unreachable')
7+
}
8+
9+
function branch (a) {
10+
if (a) {
11+
console.info('a = true')
12+
} else if (undefined) {
13+
console.info('unreachable')
14+
} else {
15+
console.info('a = false')
16+
}
17+
}
18+
19+
branch(true)
20+
branch(false)

test/fixtures/source-maps/branches/branches.nyc.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/source-maps/branches/branches.typescript.js

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/source-maps/branches/branches.typescript.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
enum ATrue {
2+
IsTrue = 1,
3+
IsFalse = 0
4+
}
5+
6+
if (false) {
7+
console.info('unreachable')
8+
} else if (true) {
9+
console.info('reachable')
10+
} else {
11+
console.info('unreachable')
12+
}
13+
14+
function branch (a: boolean) {
15+
if (a) {
16+
console.info('a = true')
17+
} else if (undefined) {
18+
console.info('unreachable')
19+
} else {
20+
console.info('a = false')
21+
}
22+
}
23+
24+
branch(!!ATrue.IsTrue)
25+
branch(!!ATrue.IsFalse)

test/fixtures/source-maps/branches/branches.uglify.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/source-maps/branches/branches.uglify.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Foo {
2+
constructor (x=33) {
3+
this.x = x ? x : 99
4+
if (this.x) {
5+
console.info('covered')
6+
} else {
7+
console.info('uncovered')
8+
}
9+
this.methodC()
10+
}
11+
methodA () {
12+
console.info('covered')
13+
}
14+
methodB () {
15+
console.info('uncovered')
16+
}
17+
methodC () {
18+
console.info('covered')
19+
}
20+
methodD () {
21+
console.info('uncovered')
22+
}
23+
}
24+
25+
const a = new Foo(0)
26+
const b = new Foo(33)
27+
a.methodA()

test/fixtures/source-maps/classes/classes.nyc.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/source-maps/classes/classes.typescript.js

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/source-maps/classes/classes.typescript.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)