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
16 changes: 16 additions & 0 deletions lib/internal/source_map/source_map_cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
ArrayPrototypeIncludes,
ArrayPrototypeIndexOf,
ArrayPrototypePush,
JSONParse,
Expand Down Expand Up @@ -338,6 +339,12 @@ function sourceMapFromDataUrl(sourceURL, url) {
// src in a html document).
// If the sources are absolute paths, the sources are converted to absolute file URLs.
function sourcesToAbsolute(baseURL, data) {
if (data.sections) {
for (let i = 0; i < data.sections.length; ++i) {
sourcesToAbsolute(baseURL, data.sections[i].map);
}
return data;
}
data.sources = data.sources.map((source) => {
source = (data.sourceRoot || '') + source;
if (isAbsolute(source)) {
Expand Down Expand Up @@ -443,6 +450,15 @@ function findSourceMap(sourceURL) {
* @returns {string | undefined} - the source content or undefined if file not found
*/
function getOriginalSource(payload, originalSourcePath) {
if (payload.sections) {
for (let i = 0; i < payload.sections.length; ++i) {
const { map } = payload.sections[i];
if (ArrayPrototypeIncludes(map.sources, originalSourcePath)) {
return getOriginalSource(map, originalSourcePath);
}
}
return;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If no section matches, could this fall through to ArrayPrototypeIndexOf(payload.sources, ...) and throw, since index maps have no top-level sources? Should we return undefined here? If so, a regression test for this case would also be helpful.

let source;
// payload.sources has been normalized to be an array of absolute urls.
const sourceContentIndex =
Expand Down
14 changes: 9 additions & 5 deletions lib/internal/test_runner/coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,12 +494,16 @@ class TestCoverage {
offset += length + 1;
return coverageLine;
});
for (let j = 0; j < data.sources.length; ++j) {
const source = data.sourcesContent?.[j];
if (source != null) {
this.getLines(data.sources[j], source);
const maps = data.sections ? ArrayPrototypeMap(data.sections, (section) => section.map) : [data];
for (let m = 0; m < maps.length; ++m) {
const { sources, sourcesContent } = maps[m];
for (let j = 0; j < sources.length; ++j) {
const source = sourcesContent?.[j];
if (source != null) {
this.getLines(sources[j], source);
}
this.markTypeScriptOnlyLines(sources[j], source);
}
this.markTypeScriptOnlyLines(data.sources[j], source);
}
const sourceMap = new SourceMap(data, { __proto__: null, lineLengths });

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/source-map/index-map-unmapped.js

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

14 changes: 14 additions & 0 deletions test/fixtures/source-map/index-map-unmapped.js.map

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

5 changes: 5 additions & 0 deletions test/fixtures/source-map/index-map.js

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

15 changes: 15 additions & 0 deletions test/fixtures/source-map/index-map.js.map

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

5 changes: 5 additions & 0 deletions test/fixtures/test-runner/source-maps/index-map/index.js

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

15 changes: 15 additions & 0 deletions test/fixtures/test-runner/source-maps/index-map/index.js.map

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

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 @@ -123,6 +123,31 @@ describe('Coverage with source maps', async () => {
t.assert.strictEqual(spawned.code, 0);
});

await it('should work with index source maps', async (t) => {
const report = generateReport([
'# -----------------------------------------------------------------',
'# file | line % | branch % | funcs % | uncovered lines',
'# -----------------------------------------------------------------',
'# test | | | | ',
'# fixtures | | | | ',
'# test-runner | | | | ',
'# source-maps | | | | ',
'# index-map | | | | ',
'# index.ts | 100.00 | 100.00 | 100.00 | ',
'# -----------------------------------------------------------------',
'# all files | 100.00 | 100.00 | 100.00 | ',
'# -----------------------------------------------------------------',
]);

const spawned = await common.spawnPromisified(process.execPath, [
...flags,
fixtures.path('test-runner', 'source-maps', 'index-map', 'index.js'),
]);
t.assert.strictEqual(spawned.stderr, '');
t.assert.ok(spawned.stdout.includes(report));
t.assert.strictEqual(spawned.code, 0);
});

await it('should throw when a source map is missing a source file', async (t) => {
const file = fixtures.path('test-runner', 'source-maps', 'missing-sources', 'index.js');
const missing = fixtures.path('test-runner', 'source-maps', 'missing-sources', 'nonexistent.js');
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-source-map-enable.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,29 @@ function nextdir() {
);
}

// Applies index source-maps to stack trace.
{
const output = spawnSync(process.execPath, [
'--enable-source-maps',
require.resolve('../fixtures/source-map/index-map.js'),
]);
assert.match(
output.stderr.toString(),
/at fail \([^)]+index-map-original\.js:2:1\)/
);
assert.match(output.stderr.toString(), /throw new Error\('original'\);\r?\n\^/);
}

// Does not throw when an index source-map has no mapping for a position.
{
const output = spawnSync(process.execPath, [
'--enable-source-maps',
require.resolve('../fixtures/source-map/index-map-unmapped.js'),
]);
assert.match(output.stderr.toString(), /AssertionError/);
assert.doesNotMatch(output.stderr.toString(), /TypeError/);
}

// Applies source-maps in esm modules to stack trace.
{
const output = spawnSync(process.execPath, [
Expand Down
Loading