Skip to content

Commit 20eb56b

Browse files
committed
improve CJS-ESM bridge exclusion
by applying bcoe/c8@f321527
1 parent 512ea20 commit 20eb56b

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ const codecovBashPath = process.platform === 'win32' ? join(cwd, 'coverage', uui
3939
const originalScriptPath = require.resolve('c8/lib/report.js');
4040
const originalScript = await promisify(readFile)(originalScriptPath, 'utf8');
4141
await promisify(writeFile)(originalScriptPath, originalScript.replace(
42-
`script.applyCoverage(v8ScriptCov.functions)
43-
map.merge(script.toIstanbul())`,
42+
/(?<=createCoverageMap\(\{\}\)\n\n)[^9]+(?=this\._allCoverageFiles =)/u,
4443
await promisify(readFile)(patchPath, 'utf8')
4544
));
4645
await promisify(unlink)(patchPath);

patch.txt

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
1-
if (v8ScriptCov.functions.every(({ functionName, ranges }) => {
2-
if (!script.source.includes(functionName)) {
3-
return false
4-
}
1+
const resultCountPerPath = new Map()
2+
const possibleCjsEsmBridges = new Map()
3+
4+
for (const v8ScriptCov of v8ProcessCov.result) {
5+
try {
6+
const path = resolve(this.resolve, v8ScriptCov.url)
7+
const script = v8toIstanbul(path, this.wrapperLength)
8+
9+
if (resultCountPerPath.has(path)) {
10+
resultCountPerPath.set(path, resultCountPerPath.get(path) + 1)
11+
} else {
12+
resultCountPerPath.set(path, 0)
13+
}
514

6-
for (const { endOffset } of ranges) {
7-
if (endOffset > script.source.length) {
8-
return false
15+
// https://github.com/nodejs/node/blob/v12.0.0/lib/internal/modules/esm/create_dynamic_module.js#L12-L20
16+
if (
17+
v8ScriptCov.functions.length === 3 &&
18+
v8ScriptCov.functions[0].functionName === '' &&
19+
v8ScriptCov.functions[0].isBlockCoverage === true &&
20+
v8ScriptCov.functions[1].functionName === 'get' &&
21+
v8ScriptCov.functions[1].isBlockCoverage === false &&
22+
v8ScriptCov.functions[2].functionName === 'set' &&
23+
v8ScriptCov.functions[2].isBlockCoverage === true
24+
) {
25+
possibleCjsEsmBridges.set(script, {
26+
path,
27+
functions: v8ScriptCov.functions
28+
})
29+
} else {
30+
script.applyCoverage(v8ScriptCov.functions)
31+
map.merge(script.toIstanbul())
932
}
33+
} catch (err) {
34+
console.warn(`file: ${v8ScriptCov.url} error: ${err.stack}`)
1035
}
36+
}
1137

12-
return true
13-
})) {
14-
script.applyCoverage(v8ScriptCov.functions)
15-
map.merge(script.toIstanbul())
38+
for (const [script, { path, functions }] of possibleCjsEsmBridges) {
39+
if (resultCountPerPath.get(path) <= 1) {
40+
script.applyCoverage(functions)
41+
map.merge(script.toIstanbul())
42+
}
1643
}

0 commit comments

Comments
 (0)