2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-01-13 02:51:32 -05:00
|
|
|
|
2022-08-22 12:14:59 -04:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_ast::MediaType;
|
|
|
|
use deno_ast::ModuleSpecifier;
|
|
|
|
use deno_ast::ParsedSource;
|
|
|
|
use deno_core::parking_lot::Mutex;
|
|
|
|
use deno_graph::CapturingModuleParser;
|
2024-04-14 17:15:17 -04:00
|
|
|
use deno_graph::DefaultModuleParser;
|
2022-08-22 12:14:59 -04:00
|
|
|
use deno_graph::ModuleParser;
|
2024-01-09 18:20:52 -05:00
|
|
|
use deno_graph::ParseOptions;
|
2024-04-14 17:15:17 -04:00
|
|
|
use deno_graph::ParsedSourceStore;
|
2022-08-22 12:14:59 -04:00
|
|
|
|
2024-02-06 15:57:10 -05:00
|
|
|
/// 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,
|
2024-02-08 20:40:26 -05:00
|
|
|
) -> Result<Option<deno_ast::ParsedSource>, deno_ast::ParseDiagnostic> {
|
2024-02-06 15:57:10 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 18:13:22 -04:00
|
|
|
#[derive(Default)]
|
2022-08-22 12:14:59 -04:00
|
|
|
pub struct ParsedSourceCache {
|
2023-10-25 18:13:22 -04:00
|
|
|
sources: Mutex<HashMap<ModuleSpecifier, ParsedSource>>,
|
2022-08-22 12:14:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ParsedSourceCache {
|
2024-01-31 22:15:22 -05:00
|
|
|
pub fn get_parsed_source_from_js_module(
|
2022-08-22 12:14:59 -04:00
|
|
|
&self,
|
2024-01-31 22:15:22 -05:00
|
|
|
module: &deno_graph::JsModule,
|
2024-02-08 20:40:26 -05:00
|
|
|
) -> Result<ParsedSource, deno_ast::ParseDiagnostic> {
|
2024-04-14 17:15:17 -04:00
|
|
|
let parser = self.as_capturing_parser();
|
|
|
|
// this will conditionally parse because it's using a CapturingModuleParser
|
|
|
|
parser.parse_module(ParseOptions {
|
|
|
|
specifier: &module.specifier,
|
|
|
|
source: module.source.clone(),
|
|
|
|
media_type: module.media_type,
|
|
|
|
// don't bother enabling because this method is currently only used for vendoring
|
|
|
|
scope_analysis: false,
|
|
|
|
})
|
2022-08-22 12:14:59 -04:00
|
|
|
}
|
|
|
|
|
2024-04-14 17:15:17 -04:00
|
|
|
pub fn remove_or_parse_module(
|
2022-08-22 12:14:59 -04:00
|
|
|
&self,
|
2024-04-14 17:15:17 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2022-08-22 12:14:59 -04:00
|
|
|
source: Arc<str>,
|
|
|
|
media_type: MediaType,
|
2024-04-14 17:15:17 -04:00
|
|
|
) -> Result<ParsedSource, deno_ast::ParseDiagnostic> {
|
|
|
|
if let Some(parsed_source) = self.remove_parsed_source(specifier) {
|
|
|
|
if parsed_source.media_type() == media_type
|
|
|
|
&& parsed_source.text_info().text_str() == source.as_ref()
|
|
|
|
{
|
|
|
|
return Ok(parsed_source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let options = ParseOptions {
|
2024-01-09 18:20:52 -05:00
|
|
|
specifier,
|
|
|
|
source,
|
|
|
|
media_type,
|
|
|
|
// don't bother enabling because this method is currently only used for emitting
|
|
|
|
scope_analysis: false,
|
2024-04-14 17:15:17 -04:00
|
|
|
};
|
|
|
|
DefaultModuleParser.parse_module(options)
|
2022-08-22 12:14:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Frees the parsed source from memory.
|
|
|
|
pub fn free(&self, specifier: &ModuleSpecifier) {
|
2023-10-25 18:13:22 -04:00
|
|
|
self.sources.lock().remove(specifier);
|
2022-08-22 12:14:59 -04:00
|
|
|
}
|
2022-08-29 14:24:10 -04:00
|
|
|
|
|
|
|
/// Creates a parser that will reuse a ParsedSource from the store
|
|
|
|
/// if it exists, or else parse.
|
|
|
|
pub fn as_capturing_parser(&self) -> CapturingModuleParser {
|
2023-10-25 18:13:22 -04:00
|
|
|
CapturingModuleParser::new(None, self)
|
2022-08-29 14:24:10 -04:00
|
|
|
}
|
2022-08-22 12:14:59 -04:00
|
|
|
}
|
|
|
|
|
2023-10-25 18:13:22 -04:00
|
|
|
/// It's ok that this is racy since in non-LSP situations
|
|
|
|
/// this will only ever store one form of a parsed source
|
|
|
|
/// and in LSP settings the concurrency will be enforced
|
|
|
|
/// at a higher level to ensure this will have the latest
|
|
|
|
/// parsed source.
|
|
|
|
impl deno_graph::ParsedSourceStore for ParsedSourceCache {
|
|
|
|
fn set_parsed_source(
|
2022-08-22 12:14:59 -04:00
|
|
|
&self,
|
2024-04-14 17:15:17 -04:00
|
|
|
specifier: ModuleSpecifier,
|
2023-10-25 18:13:22 -04:00
|
|
|
parsed_source: ParsedSource,
|
|
|
|
) -> Option<ParsedSource> {
|
|
|
|
self.sources.lock().insert(specifier, parsed_source)
|
2023-02-09 09:17:48 -05:00
|
|
|
}
|
|
|
|
|
2023-10-25 18:13:22 -04:00
|
|
|
fn get_parsed_source(
|
2022-08-22 12:14:59 -04:00
|
|
|
&self,
|
2024-04-14 17:15:17 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2023-10-25 18:13:22 -04:00
|
|
|
) -> Option<ParsedSource> {
|
|
|
|
self.sources.lock().get(specifier).cloned()
|
2022-08-22 12:14:59 -04:00
|
|
|
}
|
2024-01-09 18:20:52 -05:00
|
|
|
|
2024-04-14 17:15:17 -04:00
|
|
|
fn remove_parsed_source(
|
|
|
|
&self,
|
|
|
|
specifier: &ModuleSpecifier,
|
|
|
|
) -> Option<ParsedSource> {
|
|
|
|
self.sources.lock().remove(specifier)
|
|
|
|
}
|
|
|
|
|
2024-01-09 18:20:52 -05:00
|
|
|
fn get_scope_analysis_parsed_source(
|
|
|
|
&self,
|
2024-04-14 17:15:17 -04:00
|
|
|
specifier: &ModuleSpecifier,
|
2024-01-09 18:20:52 -05:00
|
|
|
) -> Option<ParsedSource> {
|
|
|
|
let mut sources = self.sources.lock();
|
|
|
|
let parsed_source = sources.get(specifier)?;
|
|
|
|
if parsed_source.has_scope_analysis() {
|
|
|
|
Some(parsed_source.clone())
|
|
|
|
} else {
|
|
|
|
// upgrade to have scope analysis
|
|
|
|
let parsed_source = sources.remove(specifier).unwrap();
|
|
|
|
let parsed_source = parsed_source.into_with_scope_analysis();
|
|
|
|
sources.insert(specifier.clone(), parsed_source.clone());
|
|
|
|
Some(parsed_source)
|
|
|
|
}
|
|
|
|
}
|
2022-08-22 12:14:59 -04:00
|
|
|
}
|