Skip to content

Commit

Permalink
fix: do not throw on missing range locations
Browse files Browse the repository at this point in the history
Fix: #753
  • Loading branch information
isaacs committed Nov 6, 2023
1 parent c9f6e64 commit 94da2d4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/istanbul-lib-coverage/lib/file-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const keyFromLoc = ({ start, end }) =>
// that both sections are hit.
// Returns null if no containing item is found.
const findNearestContainer = (item, map) => {
if (!item.loc) return null;
// the B item is not an identified range in the A set, BUT
// it may be contained by an identified A range. If so, then
// any hit of that containing A range counts as a hit of this
Expand All @@ -63,6 +64,7 @@ const findNearestContainer = (item, map) => {
let containerKey = null;
for (const [i, mapItem] of Object.entries(map)) {
const mapLoc = mapItem.loc;
if (!mapLoc) continue;
const itemLoc = item.loc;
// contained if all of line distances are > 0
// or line distance is 0 and col dist is >= 0
Expand Down
19 changes: 19 additions & 0 deletions packages/istanbul-lib-coverage/test/file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1123,3 +1123,22 @@ describe('addNearestContainerHits unit coverage', () => {
);
});
});

describe('findNearestContainer missing loc defense', () => {
it('does not throw if loc is missing', () => {
const loc = (sl, sc, el, ec) => ({
loc: {
start: { line: sl, column: sc },
end: { line: el, column: ec }
}
});
const map = {
0: loc(1, 1, 100, 100),
1: {},
2: loc(10, 10, 50, 50),
3: loc(20, 20, 40, 40)
};
assert.equal(findNearestContainer({ no: 'loc' }, map), null);
assert.equal(findNearestContainer(loc(30, 30, 35, 35), '3'));
});
});

0 comments on commit 94da2d4

Please sign in to comment.