fix(coverage): exclude comment lines from coverage reports (#25939)

This commit is contained in:
Taku Amano 2024-10-31 23:20:26 +09:00 committed by GitHub
parent 56f25af2c7
commit 50ea707b58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 80 additions and 65 deletions

View File

@ -327,6 +327,7 @@ fn generate_coverage_report(
coverage_report.found_lines = coverage_report.found_lines =
if let Some(source_map) = maybe_source_map.as_ref() { 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 let mut found_lines = line_counts
.iter() .iter()
.enumerate() .enumerate()
@ -334,7 +335,23 @@ fn generate_coverage_report(
// get all the mappings from this destination line to a different src line // get all the mappings from this destination line to a different src line
let mut results = source_map let mut results = source_map
.tokens() .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)) .map(move |token| (token.get_src_line() as usize, *count))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
// only keep the results that point at different src lines // only keep the results that point at different src lines

View File

@ -1,3 +1,8 @@
/**
* @module
* Complex module
*/
// This entire interface should be completely ignored by the coverage tool. // This entire interface should be completely ignored by the coverage tool.
export interface Complex { export interface Complex {
// These comments should be ignored. // These comments should be ignored.
@ -19,6 +24,7 @@ function dependency(
bar: string, bar: string,
baz: string, baz: string,
): Complex { ): Complex {
// inline comment in tested function
return { return {
foo, foo,
bar, bar,
@ -34,6 +40,9 @@ export function complex(
bar: string, bar: string,
baz: string, baz: string,
): Complex { ): Complex {
/*
* block comment in tested function
*/
return dependency( return dependency(
foo, foo,
bar, bar,
@ -48,6 +57,7 @@ export function unused(
bar: string, bar: string,
baz: string, baz: string,
): Complex { ): Complex {
// inline comment in untested function
return complex( return complex(
foo, foo,
bar, bar,
@ -62,6 +72,9 @@ export const π = Math.PI;
// And same applies for this one, this one is unused and will show up in // And same applies for this one, this one is unused and will show up in
// lacking coverage. // lacking coverage.
export function ƒ(): number { export function ƒ(): number {
/*
* block comment in untested function
*/
return ( return (
0 0
); );

View File

@ -1,8 +1,8 @@
SF:[WILDCARD]complex.ts SF:[WILDCARD]complex.ts
FN:17,dependency FN:22,dependency
FN:32,complex FN:38,complex
FN:46,unused FN:55,unused
FN:64,ƒ FN:74,ƒ
FNDA:1,dependency FNDA:1,dependency
FNDA:1,complex FNDA:1,complex
FNDA:0,unused FNDA:0,unused
@ -11,57 +11,41 @@ FNF:4
FNH:2 FNH:2
BRF:0 BRF:0
BRH: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:22,2
DA:23,2 DA:23,2
DA:24,2 DA:24,2
DA:25,2 DA:25,2
DA:26,2 DA:28,2
DA:27,2 DA:29,2
DA:29,1 DA:30,2
DA:30,1 DA:31,2
DA:31,1 DA:32,2
DA:32,1 DA:33,2
DA:33,1 DA:38,1
DA:34,1 DA:39,1
DA:35,1 DA:40,1
DA:37,2 DA:41,1
DA:38,2 DA:46,2
DA:39,2 DA:47,2
DA:40,2 DA:48,2
DA:42,2 DA:49,2
DA:44,1 DA:51,2
DA:45,1 DA:55,0
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:56,0 DA:56,0
DA:58,1 DA:57,0
DA:59,1 DA:58,0
DA:60,1 DA:61,0
DA:62,1 DA:62,0
DA:63,1 DA:63,0
DA:64,0 DA:64,0
DA:65,0
DA:66,0 DA:66,0
DA:68,0
DA:70,1 DA:70,1
DA:71,0 DA:74,0
DA:73,1 DA:78,0
DA:74,1 DA:79,0
LH:37 DA:81,0
LF:51 DA:84,0
DA:87,1
LH:21
LF:35
end_of_record end_of_record

View File

@ -1,20 +1,21 @@
cover [WILDCARD]/coverage/complex.ts ... 72.549% (37/51) cover [WILDCARD]/coverage/complex.ts ... 60.000% (21/35)
46 | export function unused( 55 | export function unused(
47 | foo: string, 56 | foo: string,
48 | bar: string, 57 | bar: string,
49 | baz: string, 58 | baz: string,
-----|----- -----|-----
51 | return complex( 61 | return complex(
52 | foo, 62 | foo,
53 | bar, 63 | bar,
54 | baz, 64 | baz,
-----|----- -----|-----
56 | } 66 | }
-----|----- -----|-----
64 | export function ƒ(): number { 74 | export function ƒ(): number {
65 | return (
66 | 0
-----|----- -----|-----
68 | } 78 | return (
79 | 0
-----|----- -----|-----
71 | console.log("%s", () => 1); 81 | }
-----|-----
84 | console.log("%s", () => 1);