Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use allGeneratedPositionsFor for more accurate source map transforms #768

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 18 additions & 18 deletions packages/istanbul-lib-source-maps/lib/get-mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,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 = sourceMap.generatedPositionFor({
const afterEndMappings = sourceMap.allGeneratedPositionsFor({
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.
sourceMap.originalPositionFor(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 = sourceMap.originalPositionFor(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 sourceMap.originalPositionFor(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
2 changes: 1 addition & 1 deletion packages/istanbul-lib-source-maps/test/map-store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('map store', () => {
},
end: {
line: 5,
column: Infinity
column: 1
}
}
},
Expand Down