From c6def993e052626be3933de4299bf4b2eb76e48a Mon Sep 17 00:00:00 2001 From: David Sherret Date: Tue, 6 Feb 2024 15:57:10 -0500 Subject: fix(publish): lazily parse sources (#22301) Closes #22290 --- cli/cache/mod.rs | 1 + cli/cache/parsed_source.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) (limited to 'cli/cache') diff --git a/cli/cache/mod.rs b/cli/cache/mod.rs index 7004f11af..fc6421056 100644 --- a/cli/cache/mod.rs +++ b/cli/cache/mod.rs @@ -46,6 +46,7 @@ pub use emit::EmitCache; pub use incremental::IncrementalCache; pub use module_info::ModuleInfoCache; pub use node::NodeAnalysisCache; +pub use parsed_source::LazyGraphSourceParser; pub use parsed_source::ParsedSourceCache; /// Permissions used to save a file in the disk caches. diff --git a/cli/cache/parsed_source.rs b/cli/cache/parsed_source.rs index 7bb1a72a7..75170aaf9 100644 --- a/cli/cache/parsed_source.rs +++ b/cli/cache/parsed_source.rs @@ -11,6 +11,39 @@ use deno_graph::CapturingModuleParser; use deno_graph::ModuleParser; use deno_graph::ParseOptions; +/// Lazily parses JS/TS sources from a `deno_graph::ModuleGraph` given +/// a `ParsedSourceCache`. Note that deno_graph doesn't necessarily cause +/// files to end up in the `ParsedSourceCache` because it might have all +/// the information it needs via caching in order to skip parsing. +#[derive(Clone, Copy)] +pub struct LazyGraphSourceParser<'a> { + cache: &'a ParsedSourceCache, + graph: &'a deno_graph::ModuleGraph, +} + +impl<'a> LazyGraphSourceParser<'a> { + pub fn new( + cache: &'a ParsedSourceCache, + graph: &'a deno_graph::ModuleGraph, + ) -> Self { + Self { cache, graph } + } + + pub fn get_or_parse_source( + &self, + module_specifier: &ModuleSpecifier, + ) -> Result, deno_ast::Diagnostic> { + let Some(deno_graph::Module::Js(module)) = self.graph.get(module_specifier) + else { + return Ok(None); + }; + self + .cache + .get_parsed_source_from_js_module(module) + .map(Some) + } +} + #[derive(Default)] pub struct ParsedSourceCache { sources: Mutex>, -- cgit v1.2.3