Skip to content

Commit c32bea8

Browse files
committed
soooo close now
1 parent 1a55306 commit c32bea8

File tree

5 files changed

+59
-7
lines changed

5 files changed

+59
-7
lines changed

src/lib/decuplicate.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
import type { Coverage, Range } from './parse-coverage.js'
22

3+
// Combine multiple adjecent ranges into a single one
4+
export function concatenate(ranges: Set<Range>): Range[] {
5+
let result: Range[] = []
6+
7+
for (let range of ranges) {
8+
// Update the last range if this range starts at last-range-end + 1
9+
if (result.length > 0 && (result.at(-1)!.end === range.start - 1 || result.at(-1)!.end === range.start)) {
10+
result.at(-1)!.end = range.end
11+
} else {
12+
result.push(range)
13+
}
14+
}
15+
16+
return result
17+
}
18+
319
function dedupe_list(ranges: Range[]): Set<Range> {
420
let new_ranges: Set<Range> = new Set()
521

@@ -75,6 +91,6 @@ export function deduplicate_entries(entries: Coverage[]): Coverage[] {
7591
return Array.from(checked_stylesheets, ([text, { url, ranges }]) => ({
7692
text,
7793
url,
78-
ranges: Array.from(ranges).sort((a, b) => a.start - b.start),
94+
ranges: concatenate(ranges).sort((a, b) => a.start - b.start),
7995
}))
8096
}

src/lib/deduplicate.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ test('merges different ranges on identical CSS, different URLs', () => {
5050
},
5151
]
5252
let first = entries.at(0)!
53-
expect(deduplicate_entries(entries)).toEqual([{ text: first.text, url: first.url, ranges: [first.ranges[0], entries[1]!.ranges[0]] }])
53+
expect(deduplicate_entries(entries)).toEqual([{ text: first.text, url: first.url, ranges: [{ start: 0, end: 9 }] }])
5454
})
5555

5656
test('merges different ranges on identical CSS, identical URLs', () => {
@@ -66,9 +66,7 @@ test('merges different ranges on identical CSS, identical URLs', () => {
6666
url: 'example.com',
6767
},
6868
]
69-
expect(deduplicate_entries(entries)).toEqual([
70-
{ text: entries[0]!.text, url: entries[0]!.url, ranges: [entries[0]!.ranges[0], entries[1]!.ranges[0]] },
71-
])
69+
expect(deduplicate_entries(entries)).toEqual([{ text: entries[0]!.text, url: entries[0]!.url, ranges: [{ start: 0, end: 9 }] }])
7270
})
7371

7472
test('does not merge different CSS with different URLs and identical ranges', () => {

src/lib/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type Coverage, type Range } from './parse-coverage.js'
22
import { prettify, type PrettifiedChunk, type PrettifiedCoverage } from './prettify.js'
3-
import { deduplicate_entries } from './decuplicate.js'
3+
import { deduplicate_entries, concatenate } from './decuplicate.js'
44
import { filter_coverage } from './filter-entries.js'
55
import { extend_ranges } from './extend-ranges.js'
66
import { chunkify, type ChunkedCoverage } from './chunkify.js'
@@ -95,6 +95,26 @@ export async function calculate_coverage(coverage: Coverage[]): Promise<Coverage
9595
// console.log('extended')
9696
// console.log(extended.at(0))
9797
// console.log()
98+
// let concatenated: Coverage[] = extended.map((cov) => ({
99+
// ...cov,
100+
// ranges: concatenate(cov.ranges),
101+
// }))
102+
// console.log('concatenated')
103+
// console.log(concatenated.at(0))
104+
// console.log()
105+
// if (concatenated.at(0)!.ranges[0].start !== 0) {
106+
// console.log(0, concatenated.at(0)!.ranges[0].start)
107+
// console.log(concatenated.at(0)!.text.substring(0, concatenated.at(0)!.ranges[0].start))
108+
// console.log()
109+
// }
110+
// for (let i = 0; i < concatenated.at(0)!.ranges.length; i++) {
111+
// let str = concatenated.at(0)!.text.slice(concatenated.at(0)!.ranges[i].end, concatenated.at(0)!.ranges[i + 1]?.start)
112+
// if (str.trim().length > 0) {
113+
// console.log(concatenated.at(0)!.ranges[i].end, concatenated.at(0)!.ranges[i + 1]?.start)
114+
// console.log(str)
115+
// console.log()
116+
// }
117+
// }
98118
let chunkified: ChunkedCoverage[] = extended.map((sheet) => chunkify(sheet))
99119
// console.log('chunkified')
100120
// console.log(chunkified.at(0))

src/lib/prettify.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function prettify(stylesheet: ChunkedCoverage): PrettifiedCoverage {
3030
} else {
3131
// mark the newline after the previous uncovered block as covered
3232
// and mark the line between this chunk and the next on as covered
33-
css = '\n' + css + (is_last ? '' : '\n')
33+
css = '\n' + css + '\n'
3434
}
3535
}
3636

src/lib/test/kitchen-sink.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,22 @@ test.describe('Wallace mega soverage suite', () => {
271271
{ is_covered: true, start_line: 25, end_line: 44 },
272272
])
273273
})
274+
275+
// This was a notoriously difficult one to fix
276+
test('main sheet has partial coverage', async () => {
277+
let data = coverage.find(({ url }) => url.endsWith('0.RC2DzJv0.css')) as Coverage
278+
let result = await calculate_coverage([data])
279+
let sheet = result.coverage_per_stylesheet.at(0)!
280+
expect.soft(sheet.chunks.filter((c) => !c.is_covered).map(({ start_line, end_line, css }) => ({ start_line, end_line }))).toEqual([
281+
{ start_line: 77, end_line: 81 }, // @media print { .nav-list.svelte-1h32yp1
282+
{ start_line: 127, end_line: 130 }, // .nav-popover-trigger.svelte-1h32yp1.invisible
283+
{ start_line: 146, end_line: 152 }, // @supports not (right: anchor(end)) { .nav-popover
284+
{ start_line: 171, end_line: 173 }, // .popover-item.svelte-1h32yp1[aria-current=\"page\"]:hover
285+
{ start_line: 198, end_line: 202 }, // @media print { .footer.svelte-jz8lnl
286+
{ start_line: 275, end_line: 277 }, // .shortcut.svelte-1c8nzn6:hover
287+
{ start_line: 296, end_line: 298 }, // .cmd-k.svelte-1ux7hi0:focus-visible
288+
{ start_line: 1291, end_line: 1293 }, // .theme-popover-trigger.svelte-mls84d:focus-visible
289+
{ start_line: 1318, end_line: 1324 }, // @supports not (right: anchor(end)) { .theme-popover.svelte-mls84d
290+
])
291+
})
274292
})

0 commit comments

Comments
 (0)