Skip to content

Commit

Permalink
refactor!: replace source-map with @jridgewell/trace-mapping (#685)
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Feb 26, 2024
1 parent c6d0982 commit 293f8b9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
46 changes: 24 additions & 22 deletions packages/istanbul-lib-source-maps/lib/get-mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

const pathutils = require('./pathutils');
const {
originalPositionFor,
generatedPositionFor,
GREATEST_LOWER_BOUND,
LEAST_UPPER_BOUND
} = require('source-map').SourceMapConsumer;
} = require('@jridgewell/trace-mapping');

/**
* AST ranges are inclusive for start positions and exclusive for end positions.
Expand Down Expand Up @@ -48,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 afterEndMappings = sourceMap.allGeneratedPositionsFor({
const afterEndMapping = generatedPositionFor(sourceMap, {
source: beforeEndMapping.source,
line: beforeEndMapping.line,
column: beforeEndMapping.column + 1,
bias: LEAST_UPPER_BOUND
});

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;
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
};
}

// 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
};
// Convert the end mapping into the real original position.
return originalPositionFor(sourceMap, afterEndMapping);
}

/**
Expand All @@ -81,13 +83,13 @@ function originalEndPositionFor(sourceMap, generatedEnd) {
* and next returning the closest element to the right (LEAST_UPPER_BOUND).
*/
function originalPositionTryBoth(sourceMap, line, column) {
const mapping = sourceMap.originalPositionFor({
const mapping = originalPositionFor(sourceMap, {
line,
column,
bias: GREATEST_LOWER_BOUND
});
if (mapping.source === null) {
return sourceMap.originalPositionFor({
return originalPositionFor(sourceMap, {
line,
column,
bias: LEAST_UPPER_BOUND
Expand Down Expand Up @@ -156,7 +158,7 @@ function getMapping(sourceMap, generatedLocation, origFile) {
}

if (start.line === end.line && start.column === end.column) {
end = sourceMap.originalPositionFor({
end = originalPositionFor(sourceMap, {
line: generatedLocation.end.line,
column: generatedLocation.end.column,
bias: LEAST_UPPER_BOUND
Expand Down
6 changes: 3 additions & 3 deletions packages/istanbul-lib-source-maps/lib/map-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
const path = require('path');
const fs = require('fs');
const debug = require('debug')('istanbuljs');
const { SourceMapConsumer } = require('source-map');
const { TraceMap, sourceContentFor } = require('@jridgewell/trace-mapping');
const pathutils = require('./pathutils');
const { SourceMapTransformer } = require('./transformer');

Expand Down Expand Up @@ -190,9 +190,9 @@ class MapStore {
return null;
}

const smc = new SourceMapConsumer(obj);
const smc = new TraceMap(obj);
smc.sources.forEach(s => {
const content = smc.sourceContentFor(s);
const content = sourceContentFor(smc, s);
if (content) {
const sourceFilePath = pathutils.relativeTo(
s,
Expand Down
4 changes: 2 additions & 2 deletions packages/istanbul-lib-source-maps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"test": "nyc mocha"
},
"dependencies": {
"@jridgewell/trace-mapping": "^0.3.23",
"debug": "^4.1.1",
"istanbul-lib-coverage": "^3.0.0",
"source-map": "^0.6.1"
"istanbul-lib-coverage": "^3.0.0"
},
"devDependencies": {
"chai": "^4.2.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/istanbul-lib-source-maps/test/transformer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const path = require('path');
const assert = require('chai').assert;
const createMap = require('istanbul-lib-coverage').createCoverageMap;
const { SourceMapConsumer } = require('source-map');
const { TraceMap } = require('@jridgewell/trace-mapping');
const { SourceMapTransformer } = require('../lib/transformer');

const coverageData = {
Expand Down Expand Up @@ -84,7 +84,7 @@ describe('transformer', () => {
coverageMap.addFileCoverage(testDataSlash.coverageData);

const transformer = new SourceMapTransformer(
() => new SourceMapConsumer(testDataSlash.sourceMap)
() => new TraceMap(testDataSlash.sourceMap)
);
const mapped = await transformer.transform(coverageMap);

Expand All @@ -111,7 +111,7 @@ describe('transformer', () => {

const transformer = new SourceMapTransformer(file =>
file === testDataSlash.coverageData.path
? new SourceMapConsumer(testDataSlash.sourceMap)
? new TraceMap(testDataSlash.sourceMap)
: undefined
);
const mapped = await transformer.transform(coverageMap);
Expand Down

0 comments on commit 293f8b9

Please sign in to comment.