diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-02-06 15:57:10 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-06 15:57:10 -0500 |
commit | c6def993e052626be3933de4299bf4b2eb76e48a (patch) | |
tree | d377208be4e20bbefb51adb85e546a6f0e65c43b /cli/cache | |
parent | a6b2a4474e50952f28cb933ada0d698fc1055578 (diff) |
fix(publish): lazily parse sources (#22301)
Closes #22290
Diffstat (limited to 'cli/cache')
-rw-r--r-- | cli/cache/mod.rs | 1 | ||||
-rw-r--r-- | cli/cache/parsed_source.rs | 33 |
2 files changed, 34 insertions, 0 deletions
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<Option<deno_ast::ParsedSource>, 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<HashMap<ModuleSpecifier, ParsedSource>>, |