Skip to content

Commit

Permalink
fix: use allGeneratedPositionsFor for more accurate source map tran…
Browse files Browse the repository at this point in the history
…sforms (#771)
  • Loading branch information
jridgewell committed Feb 26, 2024
1 parent 9290113 commit dde947c
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions packages/istanbul-lib-source-maps/lib/get-mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const pathutils = require('./pathutils');
const {
originalPositionFor,
generatedPositionFor,
allGeneratedPositionsFor,
GREATEST_LOWER_BOUND,
LEAST_UPPER_BOUND
} = require('@jridgewell/trace-mapping');
Expand Down Expand Up @@ -50,31 +50,31 @@ function originalEndPositionFor(sourceMap, generatedEnd) {
// for mappings in the original-order sorted list, this will find the
// mapping that corresponds to the one immediately after the
// beforeEndMapping mapping.
const afterEndMapping = generatedPositionFor(sourceMap, {
const afterEndMappings = allGeneratedPositionsFor(sourceMap, {
source: beforeEndMapping.source,
line: beforeEndMapping.line,
column: beforeEndMapping.column + 1,
bias: LEAST_UPPER_BOUND
});
if (
// If this is null, it means that we've hit the end of the file,
// so we can use Infinity as the end column.
afterEndMapping.line === null ||
// If these don't match, it means that the call to
// 'generatedPositionFor' didn't find any other original mappings on
// the line we gave, so consider the binding to extend to infinity.
originalPositionFor(sourceMap, afterEndMapping).line !==
beforeEndMapping.line
) {
return {
source: beforeEndMapping.source,
line: beforeEndMapping.line,
column: Infinity
};

for (let i = 0; i < afterEndMappings.length; i++) {
const afterEndMapping = afterEndMappings[i];
if (afterEndMapping.line === null) continue;

const original = originalPositionFor(sourceMap, afterEndMapping);
// If the lines match, it means that something comes after our mapping,
// so it must end where this one begins.
if (original.line === beforeEndMapping.line) return original;
}

// Convert the end mapping into the real original position.
return originalPositionFor(sourceMap, afterEndMapping);
// If a generated mapping wasn't found (or all that were found were not on
// the same line), then there's nothing after this range and we can
// consider it to extend to infinity.
return {
source: beforeEndMapping.source,
line: beforeEndMapping.line,
column: Infinity
};
}

/**
Expand Down

0 comments on commit dde947c

Please sign in to comment.