use std::collections::{HashMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::SystemTime;
use import_lint::{FileModuleInfo, ModuleGraph, ProjectResolver, Provenance, SelfReferenceMode};
use oxc_allocator::AllocatorPool;
use oxc_parser::Parser as OxcParser;
use oxc_str::CompactStr;
use rayon::prelude::*;
use crate::overlay::Overlays;
use crate::source_type::source_type_for_path;
use crate::timing;
#[derive(Debug, Clone, Default)]
pub struct RunnerOptions {
pub roots: Vec<PathBuf>,
pub project_root: PathBuf,
pub tsconfig: Option<PathBuf>,
pub self_reference_mode: SelfReferenceMode,
pub exclude: Vec<String>,
}
impl RunnerOptions {
pub fn default_tsconfig(project_root: &Path) -> Option<PathBuf> {
let candidate = project_root.join("tsconfig.json");
candidate.is_file().then_some(candidate)
}
}
#[derive(PartialEq)]
enum SourceStamp {
Disk { mtime: SystemTime, size: u64 },
Overlay { version: u64 },
}
pub(crate) struct CachedExtraction {
stamp: SourceStamp,
info: Arc<FileModuleInfo>,
}
pub(crate) type ExtractionCache = HashMap<PathBuf, CachedExtraction>;
pub(crate) struct Extracted {
pub(crate) files: Vec<Arc<FileModuleInfo>>,
pub(crate) extracted_files: usize,
}
pub struct RunOutcome {
pub graph: ModuleGraph,
pub extracted_files: usize,
}
pub fn run(options: &RunnerOptions) -> ModuleGraph {
let mut cache = ExtractionCache::new();
run_with_cache(options, &mut cache, &Overlays::default()).graph
}
pub(crate) fn run_with_cache(
options: &RunnerOptions,
cache: &mut ExtractionCache,
overlays: &Overlays,
) -> RunOutcome {
let lint_target_paths = timing::phase("walk", || {
crate::walk::walk_with_excludes(
&options.roots,
Some(options.project_root.as_path()),
&options.exclude,
)
});
let pool = AllocatorPool::new(rayon::current_num_threads());
let initial = extract_with_cache(&lint_target_paths, &pool, cache, overlays);
let resolver = build_resolver_from_files(options, &lint_target_paths, &initial.files);
extract_and_link_from(
&lint_target_paths,
initial,
&resolver,
&pool,
cache,
overlays,
)
}
pub(crate) fn build_resolver_from_files(
options: &RunnerOptions,
lint_target_paths: &[PathBuf],
initial_files: &[Arc<FileModuleInfo>],
) -> ProjectResolver {
let lint_targets: HashSet<PathBuf> = lint_target_paths.iter().cloned().collect();
let files_by_path: HashMap<PathBuf, Arc<FileModuleInfo>> = initial_files
.iter()
.map(|file| (file.path.clone(), file.clone()))
.collect();
let ambient_modules = build_ambient_registry(&files_by_path, &lint_targets);
ProjectResolver::new(
&options.project_root,
options.tsconfig.clone(),
ambient_modules,
options.self_reference_mode,
)
}
pub(crate) fn extract_and_link_from(
lint_target_paths: &[PathBuf],
initial: Extracted,
resolver: &ProjectResolver,
pool: &AllocatorPool,
cache: &mut ExtractionCache,
overlays: &Overlays,
) -> RunOutcome {
let lint_targets: HashSet<PathBuf> = lint_target_paths.iter().cloned().collect();
let mut attempted: HashSet<PathBuf> = lint_targets.clone();
let mut files: HashMap<PathBuf, Arc<FileModuleInfo>> = HashMap::new();
let mut resolutions: HashMap<(PathBuf, CompactStr), Provenance> = HashMap::new();
let mut extracted_files = initial.extracted_files;
let mut pending = initial.files;
timing::phase(&format!("files_index({} files)", pending.len()), || {
for file in &pending {
files.insert(file.path.clone(), file.clone());
}
});
loop {
let round = timing::phase(&format!("resolve({} files)", pending.len()), || {
resolve_pairs(&pending, resolver)
});
timing::phase(&format!("resolutions_merge({} pairs)", round.len()), || {
resolutions.extend(round);
});
let mut new_targets: Vec<PathBuf> = resolutions
.values()
.filter_map(|provenance| match provenance {
Provenance::Internal(path) if !attempted.contains(path) => Some(path.clone()),
_ => None,
})
.collect();
new_targets.sort();
new_targets.dedup();
if new_targets.is_empty() {
break;
}
attempted.extend(new_targets.iter().cloned());
let next = extract_with_cache(&new_targets, pool, cache, overlays);
extracted_files += next.extracted_files;
pending = next.files;
for file in &pending {
files.insert(file.path.clone(), file.clone());
}
}
let graph = timing::phase("graph_build", || {
ModuleGraph::build(files.into_values().collect(), resolutions, lint_targets)
});
RunOutcome {
graph,
extracted_files,
}
}
pub(crate) fn extract_with_cache(
paths: &[PathBuf],
pool: &AllocatorPool,
cache: &mut ExtractionCache,
overlays: &Overlays,
) -> Extracted {
let mut files: Vec<Arc<FileModuleInfo>> = Vec::with_capacity(paths.len());
let mut miss_paths: Vec<PathBuf> = Vec::new();
timing::phase(&format!("stat({} paths)", paths.len()), || {
for path in paths {
match current_stamp(path, overlays) {
Some(stamp) => {
let cache_hit = cache.get(path).is_some_and(|cached| cached.stamp == stamp);
if cache_hit {
files.push(cache.get(path).unwrap().info.clone());
continue;
}
miss_paths.push(path.clone());
}
None => {
cache.remove(path);
miss_paths.push(path.clone());
}
}
}
});
let extracted_files = miss_paths.len();
let newly_extracted = timing::phase(&format!("parse({extracted_files} files)"), || {
extract_files(&miss_paths, pool, overlays)
});
for info in newly_extracted {
let arc = Arc::new(info);
if let Some(stamp) = current_stamp(&arc.path, overlays) {
cache.insert(
arc.path.clone(),
CachedExtraction {
stamp,
info: arc.clone(),
},
);
}
files.push(arc);
}
Extracted {
files,
extracted_files,
}
}
fn current_stamp(path: &Path, overlays: &Overlays) -> Option<SourceStamp> {
if let Some(version) = overlays.version(path) {
return Some(SourceStamp::Overlay { version });
}
stat(path).map(|(mtime, size)| SourceStamp::Disk { mtime, size })
}
fn stat(path: &Path) -> Option<(SystemTime, u64)> {
let meta = fs::metadata(path).ok()?;
let mtime = meta.modified().ok()?;
Some((mtime, meta.len()))
}
fn extract_files(
paths: &[PathBuf],
pool: &AllocatorPool,
overlays: &Overlays,
) -> Vec<FileModuleInfo> {
paths
.par_iter()
.filter_map(|path| extract_one(path, pool, overlays))
.collect()
}
fn extract_one(path: &Path, pool: &AllocatorPool, overlays: &Overlays) -> Option<FileModuleInfo> {
let Some(source_type) = source_type_for_path(path) else {
eprintln!(
"import-lint: {}: unrecognized file extension, skipping",
path.display()
);
return None;
};
let source_text: Arc<str> = match overlays.content(path) {
Some(content) => content,
None => match fs::read_to_string(path) {
Ok(text) => Arc::from(text),
Err(err) => {
eprintln!(
"import-lint: cannot read {}: {err}, skipping",
path.display()
);
return None;
}
},
};
let allocator = pool.get();
let preflight = OxcParser::new(&allocator, &source_text, source_type).parse();
if preflight.panicked || preflight.diagnostics.has_errors() {
eprintln!("import-lint: failed to parse {}, skipping:", path.display());
for diagnostic in preflight.diagnostics.errors() {
eprintln!(" {diagnostic}");
}
return None;
}
Some(import_lint::extract_file(
path,
&source_text,
source_type,
&allocator,
))
}
fn resolve_pairs(
files: &[Arc<FileModuleInfo>],
resolver: &ProjectResolver,
) -> HashMap<(PathBuf, CompactStr), Provenance> {
let pairs: Vec<(&Path, &CompactStr)> = files
.iter()
.flat_map(|file| {
file.specifiers
.iter()
.map(move |specifier| (file.path.as_path(), specifier))
})
.collect();
pairs
.par_iter()
.map(|(path, specifier)| {
let provenance = resolver.resolve(path, specifier);
((path.to_path_buf(), (*specifier).clone()), provenance)
})
.collect()
}
fn build_ambient_registry(
files: &HashMap<PathBuf, Arc<FileModuleInfo>>,
lint_targets: &HashSet<PathBuf>,
) -> HashMap<CompactStr, PathBuf> {
let mut declaration_files: Vec<&Arc<FileModuleInfo>> = files
.values()
.filter(|file| lint_targets.contains(&file.path))
.filter(|file| {
source_type_for_path(&file.path).is_some_and(|t| t.is_typescript_definition())
})
.collect();
declaration_files.sort_by(|a, b| a.path.cmp(&b.path));
let mut registry = HashMap::new();
for file in declaration_files {
for specifier in &file.ambient_modules {
registry
.entry(specifier.clone())
.or_insert_with(|| file.path.clone());
}
}
registry
}