Skip to content

Commit 54bd4de

Browse files
committed
more amazing work done
1 parent f56875c commit 54bd4de

File tree

4 files changed

+68
-72
lines changed

4 files changed

+68
-72
lines changed

src/lib/extend-ranges.ts

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,25 @@ export function extend_ranges(coverage: Coverage[]): Coverage[] {
77
return coverage.map(({ text, ranges, url }) => {
88
// Adjust ranges to include @-rule name (only preludes included)
99
// Cannot reliably include closing } because it may not be the end of the range
10-
11-
console.log('Before extending')
12-
console.log({
13-
ranges: ranges.map((r) => ({
14-
...r,
15-
text: text.slice(r.start, r.end),
16-
})),
17-
})
18-
console.log()
19-
20-
for (let range of ranges) {
10+
let new_ranges = ranges.map((range, index) => {
11+
let prev_range = ranges[index - 1]
2112
// Add @atrule-name to the front of the range
22-
// Heuristic: atrule names are no longer than 20 characters ('@font-palette-values'.length === 20)
23-
for (let i = 1; i >= -LONGEST_ATRULE_NAME; i--) {
24-
let char_position = range.start + i
13+
// Heuristic: atrule names are no longer than LONGEST_ATRULE_NAME
14+
for (let i = range.start; i >= range.start - LONGEST_ATRULE_NAME; i--) {
15+
// Make sure to not overlap with the previous range
16+
if (prev_range && prev_range.end > i) {
17+
break
18+
}
19+
20+
let char_position = i
2521
if (text.charCodeAt(char_position) === AT_SIGN) {
2622
// Move the start cursor back to the start of the @-sign
2723
range.start = char_position
2824

2925
// Look if the next character might be the opening { of the atrule's block
30-
// First eat all the whitespace that might be in-between
3126
let next_offset = range.end
3227
let next_char = text.charAt(next_offset)
33-
28+
// First eat all the whitespace that might be in-between
3429
while (/\s/.test(next_char)) {
3530
next_offset++
3631
next_char = text.charAt(next_offset)
@@ -52,15 +47,25 @@ export function extend_ranges(coverage: Coverage[]): Coverage[] {
5247
if (next_char === '}') {
5348
range.end = offset + 1
5449
}
55-
}
5650

57-
console.log('EXTENDED RANGES')
58-
console.log({
59-
ranges: ranges.map((r) => ({
60-
...r,
61-
text: text.slice(r.start, r.end),
62-
})),
51+
return range
6352
})
64-
return { text, ranges, url }
53+
54+
// console.log('Before extending')
55+
// console.log({
56+
// ranges: ranges.map((r) => ({
57+
// ...r,
58+
// text: text.slice(r.start, r.end),
59+
// })),
60+
// })
61+
// console.log()
62+
// console.log('after extending')
63+
// console.log({
64+
// ranges: new_ranges.map((r) => ({
65+
// ...r,
66+
// text: text.slice(r.start, r.end),
67+
// })),
68+
// })
69+
return { text, ranges: new_ranges, url }
6570
})
6671
}

src/lib/index.test.ts

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -137,37 +137,36 @@ test.describe('from coverage data downloaded directly from the browser as JSON',
137137
},
138138
]
139139

140-
test.skip('counts totals', async () => {
140+
test('counts totals', async () => {
141141
let result = await calculate_coverage(coverage)
142-
expect.soft(result.covered_lines).toBe(9)
143-
expect.soft(result.uncovered_lines).toBe(5)
144-
expect.soft(result.total_lines).toBe(14)
145-
expect.soft(result.line_coverage_ratio).toBe(9 / 14)
142+
expect.soft(result.covered_lines).toBe(11)
143+
expect.soft(result.uncovered_lines).toBe(4)
144+
expect.soft(result.total_lines).toBe(15)
146145
expect.soft(result.total_stylesheets).toBe(1)
147146
})
148147

149-
test.skip('extracts and formats css', async () => {
148+
test('extracts and formats css', async () => {
150149
let result = await calculate_coverage(coverage)
151150
expect(result.coverage_per_stylesheet.at(0)?.text).toEqual(
152-
format(`h1 {
153-
color: blue;
154-
font-size: 24px;
155-
}
156-
157-
/* not covered */
158-
p {
159-
color: red;
160-
}
161-
162-
@media (width > 30em) {
163-
h1 {
164-
color: green;
165-
}
166-
}`),
151+
`h1 {
152+
color: blue;
153+
font-size: 24px;
154+
}
155+
156+
/* not covered */
157+
p {
158+
color: red;
159+
}
160+
161+
@media (width > 30em) {
162+
h1 {
163+
color: green;
164+
}
165+
}`,
167166
)
168167
})
169168

170-
test.skip('calculates line coverage', async () => {
169+
test('calculates line coverage', async () => {
171170
let result = await calculate_coverage(coverage)
172171
let sheet = result.coverage_per_stylesheet.at(0)!
173172
expect(
@@ -179,18 +178,7 @@ test.describe('from coverage data downloaded directly from the browser as JSON',
179178
])
180179
})
181180

182-
test.skip('calculates chunks', async () => {
183-
let result = await calculate_coverage(coverage)
184-
expect(result.coverage_per_stylesheet.at(0)?.chunks).toEqual([
185-
{ start_line: 1, is_covered: true, end_line: 4, total_lines: 4 },
186-
{ start_line: 5, is_covered: false, end_line: 8, total_lines: 4 },
187-
{ start_line: 9, is_covered: true, end_line: 10, total_lines: 2 },
188-
{ start_line: 11, is_covered: false, end_line: 11, total_lines: 1 },
189-
{ start_line: 12, is_covered: true, end_line: 14, total_lines: 3 },
190-
])
191-
})
192-
193-
test.skip('calculates chunks for fully covered file', async () => {
181+
test('calculates chunks for fully covered file', async () => {
194182
let result = await calculate_coverage([
195183
{
196184
url: 'https://example.com',
@@ -204,30 +192,32 @@ test.describe('from coverage data downloaded directly from the browser as JSON',
204192
},
205193
])
206194
expect(result.coverage_per_stylesheet.at(0)?.text).toEqual('h1 {\n\tcolor: blue;\n}')
207-
expect(result.coverage_per_stylesheet.at(0)?.chunks).toEqual([
195+
expect(
196+
result.coverage_per_stylesheet.at(0)?.chunks.map(({ start_line, end_line, is_covered }) => ({ start_line, end_line, is_covered })),
197+
).toEqual([
208198
{
209199
start_line: 1,
210200
is_covered: true,
211201
end_line: 3,
212-
total_lines: 3,
213202
},
214203
])
215204
})
216205

217-
test.skip('calculates chunks for fully uncovered file', async () => {
206+
test('calculates chunks for fully uncovered file', async () => {
218207
let result = await calculate_coverage([
219208
{
220209
url: 'https://example.com',
221210
ranges: [],
222211
text: 'h1 { color: blue; }',
223212
},
224213
])
225-
expect(result.coverage_per_stylesheet.at(0)?.chunks).toEqual([
214+
expect(
215+
result.coverage_per_stylesheet.at(0)?.chunks.map(({ start_line, end_line, is_covered }) => ({ start_line, end_line, is_covered })),
216+
).toEqual([
226217
{
227218
start_line: 1,
228219
is_covered: false,
229220
end_line: 3,
230-
total_lines: 3,
231221
},
232222
])
233223
})

src/lib/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ export async function calculate_coverage(coverage: Coverage[]): Promise<Coverage
9292
// console.log(deduplicated.at(0))
9393
// console.log()
9494
let extended: Coverage[] = extend_ranges(deduplicated)
95-
console.log('extended')
96-
console.log(extended.at(0))
97-
console.log()
95+
// console.log('extended')
96+
// console.log(extended.at(0))
97+
// console.log()
9898
let chunkified: ChunkedCoverage[] = extended.map((sheet) => chunkify(sheet))
99-
console.log('chunkified')
100-
console.log(chunkified.at(0))
101-
console.log()
99+
// console.log('chunkified')
100+
// console.log(chunkified.at(0))
101+
// console.log()
102102
let prettified: PrettifiedCoverage[] = chunkified.map((sheet) => prettify(sheet))
103103
// console.log('prettified')
104104
// console.log(prettified.at(0))

src/lib/prettify.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,23 @@ export function prettify(stylesheet: ChunkedCoverage): PrettifiedCoverage {
2020
let css = format(stylesheet.text.slice(chunk.start_offset, chunk.end_offset - 1)).trim()
2121

2222
if (chunk.is_covered) {
23+
let is_last = index === stylesheet.chunks.length - 1
2324
if (index === 0) {
2425
// mark the line between this chunk and the next on as covered
25-
css = css + '\n'
26+
css = css + (is_last ? '' : '\n')
2627
} else if (index === stylesheet.chunks.length - 1) {
2728
// mark the newline after the previous uncovered block as covered
2829
css = '\n' + css
2930
} else {
3031
// mark the newline after the previous uncovered block as covered
3132
// and mark the line between this chunk and the next on as covered
32-
css = '\n' + css + '\n'
33+
css = '\n' + css + (is_last ? '' : '\n')
3334
}
3435
}
3536

3637
let line_count = css.split('\n').length
3738
let start_offset = offset
38-
let end_offset = Math.max(offset + css.length - 1, 0)
39+
let end_offset = offset + css.length - 1
3940
let start_line = line
4041
let end_line = line + line_count
4142

0 commit comments

Comments
 (0)