Skip to content

Commit

Permalink
fix(coverage): exclude comment lines from coverage reports (#25939)
Browse files Browse the repository at this point in the history
  • Loading branch information
usualoma authored and bartlomieju committed Nov 5, 2024
1 parent 8d2012a commit 12c8b80
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 65 deletions.
19 changes: 18 additions & 1 deletion cli/tools/coverage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,31 @@ fn generate_coverage_report(

coverage_report.found_lines =
if let Some(source_map) = maybe_source_map.as_ref() {
let script_source_lines = script_source.lines().collect::<Vec<_>>();
let mut found_lines = line_counts
.iter()
.enumerate()
.flat_map(|(index, count)| {
// get all the mappings from this destination line to a different src line
let mut results = source_map
.tokens()
.filter(move |token| token.get_dst_line() as usize == index)
.filter(|token| {
let dst_line = token.get_dst_line() as usize;
dst_line == index && {
let dst_col = token.get_dst_col() as usize;
let content = script_source_lines
.get(dst_line)
.and_then(|line| {
line.get(dst_col..std::cmp::min(dst_col + 2, line.len()))
})
.unwrap_or("");

!content.is_empty()
&& content != "/*"
&& content != "*/"
&& content != "//"
}
})
.map(move |token| (token.get_src_line() as usize, *count))
.collect::<Vec<_>>();
// only keep the results that point at different src lines
Expand Down
13 changes: 13 additions & 0 deletions tests/testdata/coverage/complex.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* @module
* Complex module
*/

// This entire interface should be completely ignored by the coverage tool.
export interface Complex {
// These comments should be ignored.
Expand All @@ -19,6 +24,7 @@ function dependency(
bar: string,
baz: string,
): Complex {
// inline comment in tested function
return {
foo,
bar,
Expand All @@ -34,6 +40,9 @@ export function complex(
bar: string,
baz: string,
): Complex {
/*
* block comment in tested function
*/
return dependency(
foo,
bar,
Expand All @@ -48,6 +57,7 @@ export function unused(
bar: string,
baz: string,
): Complex {
// inline comment in untested function
return complex(
foo,
bar,
Expand All @@ -62,6 +72,9 @@ export const π = Math.PI;
// And same applies for this one, this one is unused and will show up in
// lacking coverage.
export function ƒ(): number {
/*
* block comment in untested function
*/
return (
0
);
Expand Down
82 changes: 33 additions & 49 deletions tests/testdata/coverage/complex_expected.lcov
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
SF:[WILDCARD]complex.ts
FN:17,dependency
FN:32,complex
FN:46,unused
FN:64
FN:22,dependency
FN:38,complex
FN:55,unused
FN:74
FNDA:1,dependency
FNDA:1,complex
FNDA:0,unused
Expand All @@ -11,57 +11,41 @@ FNF:4
FNH:2
BRF:0
BRH:0
DA:1,1
DA:13,1
DA:14,1
DA:15,1
DA:16,1
DA:17,2
DA:18,2
DA:19,2
DA:20,2
DA:22,2
DA:23,2
DA:24,2
DA:25,2
DA:26,2
DA:27,2
DA:29,1
DA:30,1
DA:31,1
DA:32,1
DA:33,1
DA:34,1
DA:35,1
DA:37,2
DA:38,2
DA:39,2
DA:40,2
DA:42,2
DA:44,1
DA:45,1
DA:46,0
DA:47,0
DA:48,0
DA:49,0
DA:51,0
DA:52,0
DA:53,0
DA:54,0
DA:28,2
DA:29,2
DA:30,2
DA:31,2
DA:32,2
DA:33,2
DA:38,1
DA:39,1
DA:40,1
DA:41,1
DA:46,2
DA:47,2
DA:48,2
DA:49,2
DA:51,2
DA:55,0
DA:56,0
DA:58,1
DA:59,1
DA:60,1
DA:62,1
DA:63,1
DA:57,0
DA:58,0
DA:61,0
DA:62,0
DA:63,0
DA:64,0
DA:65,0
DA:66,0
DA:68,0
DA:70,1
DA:71,0
DA:73,1
DA:74,1
LH:37
LF:51
DA:74,0
DA:78,0
DA:79,0
DA:81,0
DA:84,0
DA:87,1
LH:21
LF:35
end_of_record
31 changes: 16 additions & 15 deletions tests/testdata/coverage/complex_expected.out
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
cover [WILDCARD]/coverage/complex.ts ... 72.549% (37/51)
46 | export function unused(
47 | foo: string,
48 | bar: string,
49 | baz: string,
cover [WILDCARD]/coverage/complex.ts ... 60.000% (21/35)
55 | export function unused(
56 | foo: string,
57 | bar: string,
58 | baz: string,
-----|-----
51 | return complex(
52 | foo,
53 | bar,
54 | baz,
61 | return complex(
62 | foo,
63 | bar,
64 | baz,
-----|-----
56 | }
66 | }
-----|-----
64 | export function ƒ(): number {
65 | return (
66 | 0
74 | export function ƒ(): number {
-----|-----
68 | }
78 | return (
79 | 0
-----|-----
71 | console.log("%s", () => 1);
81 | }
-----|-----
84 | console.log("%s", () => 1);

0 comments on commit 12c8b80

Please sign in to comment.