Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/internal/modules/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ function processTypeScriptCode(code, options) {
return transformedCode;
}

function stripTypeScriptTypesForCoverage(code) {
validateString(code, 'code');
return processTypeScriptCode(code, { mode: 'strip-only' });
}


/**
* Performs type-stripping to TypeScript source code internally.
Expand Down Expand Up @@ -205,4 +210,5 @@ function addSourceMap(code, sourceMap) {
module.exports = {
stripTypeScriptModuleTypes,
stripTypeScriptTypes,
stripTypeScriptTypesForCoverage,
};
75 changes: 72 additions & 3 deletions lib/internal/test_runner/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
StringPrototypeIncludes,
StringPrototypeLocaleCompare,
StringPrototypeStartsWith,
StringPrototypeTrim,
} = primordials;
const {
copyFileSync,
Expand Down Expand Up @@ -44,6 +45,20 @@ const kIgnoreRegex = /\/\* node:coverage ignore next (?<count>\d+ )?\*\//;
const kLineEndingRegex = /\r?\n$/u;
const kLineSplitRegex = /(?<=\r?\n)/u;
const kStatusRegex = /\/\* node:coverage (?<status>enable|disable) \*\//;
const kTypeOnlyImportRegex = /^\s*import\s+type\b/u;
const kTypeScriptSourceRegex = /\.(?:cts|mts|ts)$/u;

let stripTypeScriptTypesForCoverage;

function getStripTypeScriptTypesForCoverage() {
if (!process.config.variables.node_use_amaro) {
return;
}

stripTypeScriptTypesForCoverage ??=
require('internal/modules/typescript').stripTypeScriptTypesForCoverage;
return stripTypeScriptTypesForCoverage;
}

class CoverageLine {
constructor(line, startOffset, src, length = src?.length) {
Expand All @@ -69,6 +84,7 @@ class TestCoverage {
}

#sourceLines = new SafeMap();
#typeScriptLines = new SafeSet();

getLines(fileUrl, source) {
// Split the file source into lines. Make sure the lines maintain their
Expand Down Expand Up @@ -133,6 +149,57 @@ class TestCoverage {
return lines;
}

markTypeScriptOnlyLines(fileUrl, source) {
if (this.#typeScriptLines.has(fileUrl)) {
return;
}
this.#typeScriptLines.add(fileUrl);

if (RegExpPrototypeExec(kTypeScriptSourceRegex, fileUrl) === null) {
return;
}

const lines = this.getLines(fileUrl, source);
if (!lines) {
return;
}

let strippedLines;
const stripSource = getStripTypeScriptTypesForCoverage();

if (stripSource) {
source ??= readFileSync(fileURLToPath(fileUrl), 'utf8');

try {
strippedLines = RegExpPrototypeSymbolSplit(
kLineSplitRegex,
stripSource(source),
);
} catch {
strippedLines = undefined;
}
}

for (let i = 0; i < lines.length; ++i) {
const originalLine = lines[i].src;

if (StringPrototypeTrim(originalLine).length === 0) {
continue;
}

if (strippedLines?.[i] !== undefined) {
if (StringPrototypeTrim(strippedLines[i]).length === 0) {
lines[i].ignore = true;
}
continue;
}

if (RegExpPrototypeExec(kTypeOnlyImportRegex, originalLine) !== null) {
lines[i].ignore = true;
}
}
}

summary() {
internalBinding('profiler').takeCoverage();
const coverage = this.getCoverageFromDirectory();
Expand Down Expand Up @@ -368,10 +435,12 @@ class TestCoverage {
offset += length + 1;
return coverageLine;
});
if (data.sourcesContent != null) {
for (let j = 0; j < data.sources.length; ++j) {
this.getLines(data.sources[j], data.sourcesContent[j]);
for (let j = 0; j < data.sources.length; ++j) {
const source = data.sourcesContent?.[j];
if (source != null) {
this.getLines(data.sources[j], source);
}
this.markTypeScriptOnlyLines(data.sources[j], source);
}
const sourceMap = new SourceMap(data, { __proto__: null, lineLengths });

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log('Hi');
export {};
//# sourceMappingURL=a.mjs.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type {} from 'node:assert';

console.log('Hi');
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './dist/a.mjs';
25 changes: 25 additions & 0 deletions test/parallel/test-runner-coverage-source-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,31 @@ describe('Coverage with source maps', async () => {
t.assert.strictEqual(spawned.code, 1);
});

await it('should ignore erased TypeScript import type lines', async (t) => {
const report = generateReport([
'# ----------------------------------------------------------',
'# file | line % | branch % | funcs % | uncovered lines',
'# ----------------------------------------------------------',
'# src | | | | ',
'# a.mts | 100.00 | 100.00 | 100.00 | ',
'# test.mjs | 100.00 | 100.00 | 100.00 | ',
'# ----------------------------------------------------------',
'# all files | 100.00 | 100.00 | 100.00 | ',
'# ----------------------------------------------------------',
]);

const spawned = await common.spawnPromisified(process.execPath, [
...flags,
'test.mjs',
], {
cwd: fixtures.path('test-runner', 'source-maps', 'type-only-import'),
});

t.assert.strictEqual(spawned.stderr, '');
t.assert.ok(spawned.stdout.includes(report));
t.assert.strictEqual(spawned.code, 0);
});

await it('properly accounts for line endings in source maps', async (t) => {
const report = generateReport([
'# ------------------------------------------------------------------',
Expand Down
Loading