Repository: denoland/deno
Pull request: fix(coverage): Exclude comment lines from coverage reports

fixes #25938

I think comments should be excluded from line coverage.

### current result

![html-report-unpached](https://github.com/user-attachments/assets/cddaefbf-09da-493e-8869-9afc39ecb0a9)

### patched result

![html-report-pached](https://github.com/user-attachments/assets/5b6328ec-5e5a-4275-805d-f98ca7419907)


Changed files:
filename: cli/tools/coverage/mod.rs
patch: @@ -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

filename: tests/testdata/coverage/complex.ts
patch: @@ -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.
@@ -19,6 +24,7 @@ function dependency(
   bar: string,
   baz: string,
 ): Complex {
+  // inline comment in tested function
   return {
     foo,
     bar,
@@ -34,6 +40,9 @@ export function complex(
   bar: string,
   baz: string,
 ): Complex {
+  /*
+   * block comment in tested function
+   */
   return dependency(
     foo,
     bar,
@@ -48,6 +57,7 @@ export function unused(
   bar: string,
   baz: string,
 ): Complex {
+  // inline comment in untested function
   return complex(
     foo,
     bar,
@@ -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
   );

filename: tests/testdata/coverage/complex_expected.lcov
patch: @@ -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
@@ -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

filename: tests/testdata/coverage/complex_expected.out
patch: @@ -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);