Skip to content

Commit

Permalink
fix: if LEAST_UPPER_BOUND returns null, try GREATEST_LOWER_BOUND (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe authored and coreyfarrell committed Apr 22, 2019
1 parent e10e646 commit 72b0f05
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions packages/istanbul-lib-source-maps/lib/get-mapping.js
Expand Up @@ -5,6 +5,10 @@
'use strict';

const pathutils = require('./pathutils');
const {
GREATEST_LOWER_BOUND,
LEAST_UPPER_BOUND
} = require('source-map').SourceMapConsumer;

/**
* AST ranges are inclusive for start positions and exclusive for end positions.
Expand All @@ -30,10 +34,11 @@ function originalEndPositionFor(sourceMap, generatedEnd) {
// generated file end location. Note however that this position on its
// own is not useful because it is the position of the _start_ of the range
// on the original file, and we want the _end_ of the range.
const beforeEndMapping = sourceMap.originalPositionFor({
line: generatedEnd.line,
column: generatedEnd.column - 1
});
const beforeEndMapping = originalPositionTryBoth(
sourceMap,
generatedEnd.line,
generatedEnd.column - 1
);
if (beforeEndMapping.source === null) {
return null;
}
Expand All @@ -47,7 +52,7 @@ function originalEndPositionFor(sourceMap, generatedEnd) {
source: beforeEndMapping.source,
line: beforeEndMapping.line,
column: beforeEndMapping.column + 1,
bias: 2
bias: LEAST_UPPER_BOUND
});
if (
// If this is null, it means that we've hit the end of the file,
Expand All @@ -70,6 +75,28 @@ function originalEndPositionFor(sourceMap, generatedEnd) {
return sourceMap.originalPositionFor(afterEndMapping);
}

/**
* Attempts to determine the original source position, first
* returning the closest element to the left (GREATEST_LOWER_BOUND),
* and next returning the closest element to the right (LEAST_UPPER_BOUND).
*/
function originalPositionTryBoth(sourceMap, line, column) {
const mapping = sourceMap.originalPositionFor({
line,
column,
bias: GREATEST_LOWER_BOUND
});
if (mapping.source === null) {
return sourceMap.originalPositionFor({
line,
column,
bias: LEAST_UPPER_BOUND
});
} else {
return mapping;
}
}

function isInvalidPosition(pos) {
return (
!pos ||
Expand Down Expand Up @@ -98,7 +125,11 @@ function getMapping(sourceMap, generatedLocation, origFile) {
return null;
}

const start = sourceMap.originalPositionFor(generatedLocation.start);
const start = originalPositionTryBoth(
sourceMap,
generatedLocation.start.line,
generatedLocation.start.column
);
let end = originalEndPositionFor(sourceMap, generatedLocation.end);

/* istanbul ignore if: edge case too hard to test for */
Expand Down Expand Up @@ -128,7 +159,7 @@ function getMapping(sourceMap, generatedLocation, origFile) {
end = sourceMap.originalPositionFor({
line: generatedLocation.end.line,
column: generatedLocation.end.column,
bias: 2
bias: LEAST_UPPER_BOUND
});
end.column -= 1;
}
Expand Down

0 comments on commit 72b0f05

Please sign in to comment.