mirror of
https://github.com/denoland/deno.git
synced 2025-10-02 23:24:37 +00:00
fix(coverage): ignore comments (#8639)
This commit fixes coverage collection by ignoring comments when tallying up line counts.
This commit is contained in:
parent
47263ef6fa
commit
413f79a494
5 changed files with 45 additions and 1 deletions
|
@ -1,6 +1,9 @@
|
|||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
use crate::ast;
|
||||
use crate::ast::TokenOrComment;
|
||||
use crate::colors;
|
||||
use crate::media_type::MediaType;
|
||||
use crate::module_graph::TypeLib;
|
||||
use crate::program_state::ProgramState;
|
||||
use deno_core::error::AnyError;
|
||||
|
@ -15,6 +18,7 @@ use serde::Serialize;
|
|||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
use swc_common::Span;
|
||||
use uuid::Uuid;
|
||||
|
||||
pub struct CoverageCollector {
|
||||
|
@ -127,8 +131,16 @@ impl PrettyCoverageReporter {
|
|||
script_coverage: &ScriptCoverage,
|
||||
script_source: &str,
|
||||
) {
|
||||
let lines = script_source.split('\n').collect::<Vec<_>>();
|
||||
let mut ignored_spans: Vec<Span> = Vec::new();
|
||||
for item in ast::lex("", script_source, &MediaType::JavaScript) {
|
||||
if let TokenOrComment::Token(_) = item.inner {
|
||||
continue;
|
||||
}
|
||||
|
||||
ignored_spans.push(item.span);
|
||||
}
|
||||
|
||||
let lines = script_source.split('\n').collect::<Vec<_>>();
|
||||
let mut covered_lines: Vec<usize> = Vec::new();
|
||||
let mut uncovered_lines: Vec<usize> = Vec::new();
|
||||
|
||||
|
@ -138,6 +150,16 @@ impl PrettyCoverageReporter {
|
|||
|
||||
let mut count = 0;
|
||||
|
||||
let ignore = ignored_spans.iter().any(|span| {
|
||||
(span.lo.0 as usize) <= line_start_offset
|
||||
&& (span.hi.0 as usize) >= line_end_offset
|
||||
});
|
||||
|
||||
if ignore {
|
||||
covered_lines.push(index);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Count the hits of ranges that include the entire line which will always be at-least one
|
||||
// as long as the code has been evaluated.
|
||||
for function in &script_coverage.functions {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue