Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions packages/istanbul-lib-coverage/src/file-coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,23 +184,74 @@ const addNearestContainerHits = (
}
};

// Source maps may leave the end column of a range unresolved, e.g. Vite's SSR vs. client
// transforms of the same file. In memory it's `Infinity`, which becomes `null` once
// serialized as JSON. Treat both as unknown: match it with the single item in `others`
// that has the same start position and end line.
const hasUnknownEndColumn = (key: string): boolean =>
key.endsWith("|Infinity") || key.endsWith("|null");

const resolveUnknownEndColumns = <V extends [Hits | null, unknown]>(
items: Record<string, V>,
others: Record<string, V>,
): Record<string, V> => {
// "start.line|start.column|end.line" -> key in others, or null when ambiguous
const candidates = new Map<string, string | null>();

for (const key of Object.keys(others)) {
const prefix = key.slice(0, key.lastIndexOf("|"));
candidates.set(prefix, candidates.has(prefix) ? null : key);
}

const resolved: Record<string, V> = {};

for (const [key, value] of Object.entries(items)) {
const match =
hasUnknownEndColumn(key) && !(key in others)
? candidates.get(key.slice(0, key.lastIndexOf("|")))
: null;

if (match && !(match in items)) {
// Use the resolved range from the other side
resolved[match] = [value[0], others[match][1]] as V;
} else {
resolved[key] = value;
}
}

return resolved;
};

const mergeProp = <H extends Hits, T>(
aHits: Record<string, H>,
aMap: Record<string, T>,
bHits: Record<string, H>,
bMap: Record<string, T>,
itemKey: (item: T) => string = keyFromLoc as unknown as (item: T) => string,
): [Record<string, H>, Record<string, T>] => {
const aItems: Record<string, [Hits | null, T]> = {};
let hasUnknownEndColumns = false;

let aItems: Record<string, [Hits | null, T]> = {};
for (const [key, itemHits] of Object.entries(aHits)) {
const item = aMap[key];
aItems[itemKey(item)] = [itemHits, item];
const itemKeyStr = itemKey(item);
hasUnknownEndColumns ||= hasUnknownEndColumn(itemKeyStr);
aItems[itemKeyStr] = [itemHits, item];
}
const bItems: Record<string, [Hits | null, T]> = {};

let bItems: Record<string, [Hits | null, T]> = {};
for (const [key, itemHits] of Object.entries(bHits)) {
const item = bMap[key];
bItems[itemKey(item)] = [itemHits, item];
const itemKeyStr = itemKey(item);
hasUnknownEndColumns ||= hasUnknownEndColumn(itemKeyStr);
bItems[itemKeyStr] = [itemHits, item];
}

if (hasUnknownEndColumns) {
aItems = resolveUnknownEndColumns(aItems, bItems);
bItems = resolveUnknownEndColumns(bItems, aItems);
}

const mergedItems: Record<string, [Hits | null, T]> = {};
for (const [key, aValue] of Object.entries(aItems)) {
let aItemHits = aValue[0];
Expand Down
166 changes: 166 additions & 0 deletions packages/istanbul-lib-coverage/test/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1249,3 +1249,169 @@ describe("findNearestContainer missing loc defense", () => {
).toBe(null);
});
});

describe("merging ranges with unknown end columns", () => {
// Source maps may leave end columns unresolved as `null`
const loc = (sl: number, sc: number, el: number, ec: number | null): Range => ({
start: { line: sl, column: sc },
end: { line: el, column: ec as number },
});

const coverage = (data: Partial<FileCoverageData>) =>
new FileCoverage({
path: "/source.ts",
statementMap: {},
fnMap: {},
branchMap: {},
s: {},
f: {},
b: {},
...data,
});

it("merges statements when unknown end columns are on this side", () => {
// https://github.com/vitest-dev/vitest/issues/9366
const jsdom = coverage({
statementMap: {
0: loc(1, 22, 7, null),
1: loc(9, 21, 9, null),
2: loc(11, 22, 15, null),
},
s: { 0: 1, 1: 1, 2: 0 },
});
const browser = coverage({
statementMap: {
0: loc(1, 22, 7, 10),
1: loc(9, 21, 9, 58),
2: loc(11, 22, 15, 10),
},
s: { 0: 2, 1: 0, 2: 0 },
});

jsdom.merge(browser);

expect(jsdom.statementMap).toEqual({
0: loc(1, 22, 7, 10),
1: loc(9, 21, 9, 58),
2: loc(11, 22, 15, 10),
});
expect(jsdom.s).toEqual({ 0: 3, 1: 1, 2: 0 });
});

it("merges statements when unknown end columns are on the other side", () => {
// https://github.com/vitest-dev/vitest/issues/11921
const node = coverage({
statementMap: { 0: loc(3, 20, 3, 29), 1: loc(4, 2, 4, null) },
s: { 0: 0, 1: 0 },
});
const browser = coverage({
statementMap: { 0: loc(3, 20, 3, null), 1: loc(4, 2, 4, null) },
s: { 0: 1, 1: 1 },
});

node.merge(browser);

expect(node.statementMap).toEqual({ 0: loc(3, 20, 3, 29), 1: loc(4, 2, 4, null) });
expect(node.s).toEqual({ 0: 1, 1: 1 });
});

it("merges statements whose unknown end columns are Infinity", () => {
// Before JSON serialization unknown end columns are `Infinity`, not `null`
const jsdom = coverage({
statementMap: { 0: loc(1, 22, 7, Infinity), 1: loc(9, 21, 9, Infinity) },
s: { 0: 2, 1: 2 },
});
const browser = coverage({
statementMap: { 0: loc(1, 22, 7, 10), 1: loc(9, 21, 9, 58) },
s: { 0: 4, 1: 4 },
});

jsdom.merge(browser);

expect(jsdom.statementMap).toEqual({ 0: loc(1, 22, 7, 10), 1: loc(9, 21, 9, 58) });
expect(jsdom.s).toEqual({ 0: 6, 1: 6 });
});

it("does not merge when the match is ambiguous", () => {
const a = coverage({
statementMap: { 0: loc(1, 0, 1, null) },
s: { 0: 1 },
});
const b = coverage({
statementMap: { 0: loc(1, 0, 1, 5), 1: loc(1, 0, 1, 9) },
s: { 0: 2, 1: 3 },
});

a.merge(b);

expect(a.statementMap).toEqual({
0: loc(1, 0, 1, null),
1: loc(1, 0, 1, 5),
2: loc(1, 0, 1, 9),
});
expect(a.s).toEqual({ 0: 1, 1: 2, 2: 3 });
});

it("does not merge into a range that already has an exact match", () => {
const a = coverage({
statementMap: { 0: loc(1, 0, 1, 5), 1: loc(1, 0, 1, null) },
s: { 0: 1, 1: 2 },
});
const b = coverage({
statementMap: { 0: loc(1, 0, 1, 5) },
s: { 0: 3 },
});

a.merge(b);

expect(a.statementMap).toEqual({ 0: loc(1, 0, 1, 5), 1: loc(1, 0, 1, null) });
expect(a.s).toEqual({ 0: 4, 1: 2 });
});

it("does not merge ranges whose end lines differ", () => {
const a = coverage({
statementMap: { 0: loc(1, 0, 2, null) },
s: { 0: 1 },
});
const b = coverage({
statementMap: { 0: loc(1, 0, 3, 1) },
s: { 0: 1 },
});

a.merge(b);

expect(Object.keys(a.statementMap)).toHaveLength(2);
});

it("merges functions with unknown end columns", () => {
const fn = (ec: number | null) => ({
name: "greet",
decl: loc(2, 16, 2, 21),
loc: loc(2, 24, 5, ec),
line: 2,
});
const a = coverage({ fnMap: { 0: fn(null) }, f: { 0: 1 } });
const b = coverage({ fnMap: { 0: fn(1) }, f: { 0: 2 } });

a.merge(b);

expect(a.fnMap).toEqual({ 0: fn(1) });
expect(a.f).toEqual({ 0: 3 });
});

it("merges branches with unknown end columns", () => {
const branch = (ec: number | null, ec2: number | null) => ({
type: "if",
line: 2,
loc: loc(2, 0, 2, ec2),
locations: [loc(2, 0, 2, ec), loc(2, 35, 2, ec2)],
});
const a = coverage({ branchMap: { 0: branch(null, null) }, b: { 0: [1, 0] } });
const b = coverage({ branchMap: { 0: branch(30, 60) }, b: { 0: [0, 1] } });

a.merge(b);

expect(a.branchMap).toEqual({ 0: branch(30, 60) });
expect(a.b).toEqual({ 0: [1, 1] });
});
});
Loading