use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use import_lint::diagnostics::line_col;
use import_lint::{LintConfig, ModuleGraph, Provenance, Severity, check_files};
use crate::output::{OutputSeverity, RenderedDiagnostic};
use crate::overlay::Overlays;
use crate::timing;
#[derive(Debug, Clone, Copy, Default)]
pub struct ReportOptions {
pub report_unresolved: bool,
pub quiet: bool,
}
pub struct ReportResult {
pub diagnostics: Vec<RenderedDiagnostic>,
pub has_error: bool,
}
pub fn build_report(
module_graph: &ModuleGraph,
config: &LintConfig,
project_root: &Path,
options: &ReportOptions,
overlays: &Overlays,
) -> ReportResult {
let targets: Vec<&Path> = module_graph
.lint_targets
.iter()
.map(PathBuf::as_path)
.collect();
let by_file = timing::phase("check_graph", || {
diagnostics_by_file(
module_graph,
config,
project_root,
options,
&targets,
overlays,
)
});
finish_report(by_file.into_values().flatten(), options)
}
pub fn finish_report(
diagnostics: impl IntoIterator<Item = RenderedDiagnostic>,
options: &ReportOptions,
) -> ReportResult {
let mut diagnostics: Vec<RenderedDiagnostic> = diagnostics.into_iter().collect();
diagnostics.sort_by(|a, b| (&a.file, a.line, a.column).cmp(&(&b.file, b.line, b.column)));
let has_error = diagnostics
.iter()
.any(|d| d.severity == OutputSeverity::Error);
if options.quiet {
diagnostics.retain(|d| d.severity != OutputSeverity::Warn);
}
ReportResult {
diagnostics,
has_error,
}
}
pub fn diagnostics_by_file(
module_graph: &ModuleGraph,
config: &LintConfig,
project_root: &Path,
options: &ReportOptions,
files: &[&Path],
overlays: &Overlays,
) -> HashMap<PathBuf, Vec<RenderedDiagnostic>> {
let mut source_cache: HashMap<PathBuf, String> = HashMap::new();
let mut by_file: HashMap<PathBuf, Vec<RenderedDiagnostic>> = files
.iter()
.map(|file| (file.to_path_buf(), Vec::new()))
.collect();
let severity = config.rules.jsdoc.severity;
if severity != Severity::Off {
let output_severity = match severity {
Severity::Error => OutputSeverity::Error,
Severity::Warn => OutputSeverity::Warn,
Severity::Off => unreachable!("checked above"),
};
let core_diagnostics = check_files(
module_graph,
&config.rules.jsdoc.options,
project_root,
files,
);
for diagnostic in &core_diagnostics {
let source = read_cached(&mut source_cache, &diagnostic.path, overlays);
let (line, column) = line_col(source, diagnostic.span.start);
let (end_line, end_column) = line_col(source, diagnostic.span.end);
by_file
.entry(diagnostic.path.clone())
.or_default()
.push(RenderedDiagnostic {
file: diagnostic.path.clone(),
line,
column,
end_line,
end_column,
severity: output_severity,
rule_id: "import-access/jsdoc",
message: diagnostic.message(),
message_id: diagnostic.message_id.as_str().to_string(),
});
}
}
if options.report_unresolved {
collect_unresolved(
module_graph,
files,
&mut source_cache,
&mut by_file,
overlays,
);
}
for diagnostics in by_file.values_mut() {
diagnostics.sort_by_key(|d| (d.line, d.column));
}
by_file
}
fn read_cached<'a>(
cache: &'a mut HashMap<PathBuf, String>,
path: &Path,
overlays: &Overlays,
) -> &'a str {
cache.entry(path.to_path_buf()).or_insert_with(|| {
overlays
.content(path)
.map(|content| content.to_string())
.unwrap_or_else(|| fs::read_to_string(path).unwrap_or_default())
})
}
fn collect_unresolved(
graph: &ModuleGraph,
files: &[&Path],
source_cache: &mut HashMap<PathBuf, String>,
by_file: &mut HashMap<PathBuf, Vec<RenderedDiagnostic>>,
overlays: &Overlays,
) {
for &target in files {
let Some(file) = graph.file(target) else {
continue;
};
for entry in &file.checked_entries {
if !matches!(
graph.resolution(target, &entry.specifier),
Some(Provenance::Unresolved)
) {
continue;
}
let source = read_cached(source_cache, target, overlays);
let (line, column) = line_col(source, entry.span.start);
let (end_line, end_column) = line_col(source, entry.span.end);
by_file
.entry(target.to_path_buf())
.or_default()
.push(RenderedDiagnostic {
file: target.to_path_buf(),
line,
column,
end_line,
end_column,
severity: OutputSeverity::Warn,
rule_id: "import-access/unresolved",
message: format!("Unresolved import specifier '{}'", entry.specifier),
message_id: "unresolved".to_string(),
});
}
}
}