Skip to content

Commit 2a2f8a9

Browse files
committed
add clarifying sectioning comments
1 parent 3a0d6f4 commit 2a2f8a9

1 file changed

Lines changed: 21 additions & 17 deletions

File tree

src/lib/components/coverage/calculate-coverage.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { prettify } from './prettify'
33
import { ext } from './ext'
44

55
interface HtmlParser {
6-
(html: string): Document;
6+
(html: string): Document
77
}
88

99
function get_css_and_ranges_from_html(parse_html: HtmlParser, html: string, old_ranges: Range[]) {
@@ -29,7 +29,7 @@ function get_css_and_ranges_from_html(parse_html: HtmlParser, html: string, old_
2929
if (range.start >= start_index && range.end <= end_index) {
3030
new_ranges.push({
3131
start: current_offset + (range.start - start_index),
32-
end: current_offset + (range.end - start_index),
32+
end: current_offset + (range.end - start_index)
3333
})
3434
}
3535
}
@@ -41,7 +41,7 @@ function get_css_and_ranges_from_html(parse_html: HtmlParser, html: string, old_
4141
return {
4242
css: combined_css,
4343
ranges: new_ranges
44-
};
44+
}
4545
}
4646

4747
export function calculate_coverage(browser_coverage: Coverage[], parse_html: HtmlParser) {
@@ -51,6 +51,7 @@ export function calculate_coverage(browser_coverage: Coverage[], parse_html: Htm
5151
let files_found = browser_coverage.length
5252
let filtered_coverage = []
5353

54+
// SECTION: filter input for eligible CSS input
5455
for (let entry of browser_coverage) {
5556
if (!entry.text) continue
5657
let extension = ext(entry.url).toLowerCase()
@@ -71,19 +72,22 @@ export function calculate_coverage(browser_coverage: Coverage[], parse_html: Htm
7172
filtered_coverage.push({
7273
url: entry.url,
7374
text: css,
74-
ranges,
75+
ranges
7576
})
7677
}
7778

79+
// SECTION: prettify css and update coverage accordingly
7880
let prettified_coverage = prettify(filtered_coverage)
7981

82+
// SECTION: De-duplicate coverage URL's and ranges
83+
//
8084
// prerequisites
8185
// - we check each stylesheet content only once (to avoid counting the same content multiple times)
8286
// - if a duplicate stylesheet enters the room, we add it's ranges to the existing stylesheet's ranges
8387
// - only bytes of deduplicated stylesheets are counted
8488
// - after all entries have been processed, we calculate the total bytes, used bytes, and unused bytes
8589

86-
let checked_stylesheets = new Map<string, { url: string, ranges: Range[] }>()
90+
let checked_stylesheets = new Map<string, { url: string; ranges: Range[] }>()
8791

8892
for (let entry of prettified_coverage) {
8993
let text = entry.text || ''
@@ -108,14 +112,14 @@ export function calculate_coverage(browser_coverage: Coverage[], parse_html: Htm
108112
total_bytes += text.length
109113
checked_stylesheets.set(text, {
110114
url: entry.url,
111-
ranges: entry.ranges,
115+
ranges: entry.ranges
112116
})
113117
}
114118
}
115119

120+
// SECTION: calculate used vs. unused bytes
116121
// We sort the ranges by their start position
117122
// Then we iterate over the ranges and calculate the used bytes
118-
// Unused pieces of the stylesheet are stored in the unused_parts array
119123
for (let [text, { ranges }] of checked_stylesheets) {
120124
let last_position = 0
121125
ranges.sort((a, b) => a.start - b.start)
@@ -129,6 +133,7 @@ export function calculate_coverage(browser_coverage: Coverage[], parse_html: Htm
129133
}
130134
}
131135

136+
// SECTION: calculate coverage for each individual stylesheet we found
132137
let coverage_per_stylesheet = Array.from(checked_stylesheets).map(([text, { url, ranges }]) => {
133138
let used = ranges.reduce((acc, range) => acc + (range.end - range.start), 0)
134139
let trimmed_text = text.trim()
@@ -162,15 +167,12 @@ export function calculate_coverage(browser_coverage: Coverage[], parse_html: Htm
162167
if (is_in_range && !is_closing_brace && !is_empty) {
163168
lines_covered++
164169
line_coverage[index] = 1
165-
}
166-
else if ((is_empty || is_closing_brace) && prev_is_covered) {
170+
} else if ((is_empty || is_closing_brace) && prev_is_covered) {
167171
lines_covered++
168172
line_coverage[index] = 1
169-
}
170-
else if (is_empty && line_coverage[index - 1] === 0) {
173+
} else if (is_empty && line_coverage[index - 1] === 0) {
171174
line_coverage[index] = 0
172-
}
173-
else {
175+
} else {
174176
line_coverage[index] = 0
175177
}
176178
offset = next_offset
@@ -187,18 +189,20 @@ export function calculate_coverage(browser_coverage: Coverage[], parse_html: Htm
187189
line_coverage,
188190
total_lines: line_coverage.length,
189191
covered_lines: lines_covered,
190-
uncovered_lines: line_coverage.length - lines_covered,
192+
uncovered_lines: line_coverage.length - lines_covered
191193
}
192194
})
193195

194-
let coverage_ratio = coverage_per_stylesheet.reduce((acc, sheet) => acc + sheet.coverage_ratio, 0) / coverage_per_stylesheet.length
196+
// TODO: calculate byte_coverage AND line_coverage
197+
let coverage_ratio =
198+
coverage_per_stylesheet.reduce((acc, sheet) => acc + sheet.coverage_ratio, 0) / coverage_per_stylesheet.length
195199

196200
return {
197201
files_found,
198202
total_bytes,
199203
used_bytes,
200204
unused_bytes,
201205
coverage_ratio,
202-
coverage_per_stylesheet,
206+
coverage_per_stylesheet
203207
}
204-
}
208+
}

0 commit comments

Comments
 (0)