From 66ce35bdc2677449540ce597fd6312535ef08aab Mon Sep 17 00:00:00 2001 From: PerryLink <255665900+PerryLink@users.noreply.github.com> Date: Sun, 18 Oct 2026 12:05:16 +0000 Subject: [PATCH] fix: resolve a source that already matches one of consumer.sources --- lib/source-map-consumer.js | 8 ++++++++ test/test-source-map-consumer.js | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/source-map-consumer.js b/lib/source-map-consumer.js index 17ef92b1..e1968692 100644 --- a/lib/source-map-consumer.js +++ b/lib/source-map-consumer.js @@ -269,6 +269,14 @@ class BasicSourceMapConsumer extends SourceMapConsumer { return index; } + // The source may already be fully resolved, for example when it came from + // `consumer.sources`, so accept it as-is before giving up. + if (this._absoluteSources.has(aSource)) { + const index = this._absoluteSources.indexOf(aSource); + this._sourceLookupCache.set(aSource, index); + return index; + } + // To avoid this cache growing forever, we do not cache lookup misses. return -1; } diff --git a/test/test-source-map-consumer.js b/test/test-source-map-consumer.js index 8cf50988..e59a6275 100644 --- a/test/test-source-map-consumer.js +++ b/test/test-source-map-consumer.js @@ -1091,6 +1091,33 @@ exports[ consumer.destroy(); }; +exports[ + "test resolved sources are accepted by sourceContentFor and generatedPositionFor" +] = async function (assert) { + const map = { + version: 3, + sources: ["one.js"], + names: [], + mappings: "AAAA", + sourcesContent: ["var one = 1;"], + }; + const consumer = await new SourceMapConsumer(map, "maps/app.map"); + + assert.equal(consumer.sources[0], "maps/one.js"); + assert.ok(consumer.hasContentsOfAllSources()); + assert.equal(consumer.sourceContentFor(consumer.sources[0]), "var one = 1;"); + assert.equal( + consumer.generatedPositionFor({ + source: consumer.sources[0], + line: 1, + column: 0, + }).line, + 1 + ); + + consumer.destroy(); +}; + exports["test sourceRoot + generatedPositionFor"] = async function (assert) { let map = new SourceMapGenerator({ sourceRoot: "foo/bar",