Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(resolver): add resolution mode #303

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use deno_graph::source::CacheInfo;
use deno_graph::source::CacheSetting;
use deno_graph::source::LoadFuture;
use deno_graph::source::Loader;
use deno_graph::source::ResolutionMode;
use deno_graph::source::ResolveError;
use deno_graph::source::Resolver;
use deno_graph::source::DEFAULT_JSX_IMPORT_SOURCE_MODULE;
Expand Down Expand Up @@ -140,6 +141,7 @@ impl Resolver for JsResolver {
&self,
specifier: &str,
referrer: &ModuleSpecifier,
_mode: ResolutionMode,
) -> Result<ModuleSpecifier, ResolveError> {
if let Some(resolve) = &self.maybe_resolve {
let this = JsValue::null();
Expand Down
40 changes: 29 additions & 11 deletions src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,8 +832,13 @@ impl GraphImport {
start: Position::zeroed(),
end: Position::zeroed(),
};
let maybe_type =
resolve(&import, referrer_range, maybe_resolver, maybe_npm_resolver);
let maybe_type = resolve(
&import,
referrer_range,
ResolutionMode::Types,
maybe_resolver,
maybe_npm_resolver,
);
(
import,
Dependency {
Expand Down Expand Up @@ -1551,11 +1556,12 @@ impl ModuleGraph {
fn resolve(
specifier_text: &str,
referrer_range: Range,
mode: ResolutionMode,
maybe_resolver: Option<&dyn Resolver>,
maybe_npm_resolver: Option<&dyn NpmResolver>,
) -> Resolution {
let response = if let Some(resolver) = maybe_resolver {
resolver.resolve(specifier_text, &referrer_range.specifier)
resolver.resolve(specifier_text, &referrer_range.specifier, mode)
} else {
resolve_import(specifier_text, &referrer_range.specifier)
.map_err(|err| err.into())
Expand Down Expand Up @@ -1784,6 +1790,7 @@ pub(crate) fn parse_esm_module_from_module_info(
dep.maybe_type = resolve(
&specifier.text,
range.clone(),
ResolutionMode::Types,
maybe_resolver,
maybe_npm_resolver,
);
Expand All @@ -1802,6 +1809,7 @@ pub(crate) fn parse_esm_module_from_module_info(
let dep_resolution = resolve(
&specifier.text,
range.clone(),
ResolutionMode::Types,
maybe_resolver,
maybe_npm_resolver,
);
Expand Down Expand Up @@ -1868,6 +1876,7 @@ pub(crate) fn parse_esm_module_from_module_info(
dep.maybe_code = resolve(
&specifier_text,
range.clone(),
ResolutionMode::Execution,
maybe_resolver,
maybe_npm_resolver,
);
Expand All @@ -1894,6 +1903,7 @@ pub(crate) fn parse_esm_module_from_module_info(
dep.maybe_type = resolve(
&specifier.text,
range.clone(),
ResolutionMode::Types,
maybe_resolver,
maybe_npm_resolver,
);
Expand Down Expand Up @@ -1921,6 +1931,7 @@ pub(crate) fn parse_esm_module_from_module_info(
dependency: resolve(
types_header,
range,
ResolutionMode::Types,
maybe_resolver,
maybe_npm_resolver,
),
Expand Down Expand Up @@ -1985,18 +1996,18 @@ pub(crate) fn parse_esm_module_from_module_info(
module.specifier.clone(),
desc.specifier_range.clone(),
);
let dep_resolution = resolve(
&desc.specifier,
range.clone(),
maybe_resolver,
maybe_npm_resolver,
);
if matches!(
desc.kind,
DependencyKind::ImportType | DependencyKind::ExportType
) {
if dep.maybe_type.is_none() {
dep.maybe_type = dep_resolution;
dep.maybe_type = resolve(
&desc.specifier,
range.clone(),
ResolutionMode::Types,
maybe_resolver,
maybe_npm_resolver,
);
}
dep.imports.push(Import {
specifier: desc.specifier.clone(),
Expand All @@ -2009,7 +2020,13 @@ pub(crate) fn parse_esm_module_from_module_info(
if dep.maybe_code.is_none() {
// This is a code import, the first one of that specifier in this module.
// Resolve and determine the initial `is_dynamic` value from it.
dep.maybe_code = dep_resolution;
dep.maybe_code = resolve(
&desc.specifier,
range.clone(),
ResolutionMode::Execution,
maybe_resolver,
maybe_npm_resolver,
);
dep.is_dynamic = desc.is_dynamic;
} else {
// This is a code import, but not the first one of that specifier in this
Expand All @@ -2032,6 +2049,7 @@ pub(crate) fn parse_esm_module_from_module_info(
resolve(
&pragma.specifier,
Range::from_position_range(specifier, pragma.range),
ResolutionMode::Types,
maybe_resolver,
maybe_npm_resolver,
)
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub fn parse_module_from_ast(
#[cfg(test)]
mod tests {
use crate::graph::ResolutionResolved;
use crate::source::ResolutionMode;

use super::*;
use pretty_assertions::assert_eq;
Expand Down Expand Up @@ -1146,6 +1147,7 @@ console.log(a);
&self,
specifier_text: &str,
referrer: &deno_ast::ModuleSpecifier,
_mode: ResolutionMode,
) -> Result<deno_ast::ModuleSpecifier, source::ResolveError> {
use import_map::ImportMapError;
Err(source::ResolveError::Other(
Expand Down
11 changes: 11 additions & 0 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ pub enum ResolveError {
Other(#[from] anyhow::Error),
}

/// The kind of resolution currently being done by deno_graph.
#[derive(Debug, Clone, Copy)]
pub enum ResolutionMode {
/// Resolving for code that will be executed.
Execution,
/// Resolving for code that will be used for type information.
Types,
}

/// A trait which allows the module graph to resolve specifiers and type only
/// dependencies. This can be use to provide import maps and override other
/// default resolution logic used by `deno_graph`.
Expand All @@ -169,6 +178,7 @@ pub trait Resolver: fmt::Debug {
&self,
specifier_text: &str,
referrer: &ModuleSpecifier,
_mode: ResolutionMode,
) -> Result<ModuleSpecifier, ResolveError> {
Ok(resolve_import(specifier_text, referrer)?)
}
Expand Down Expand Up @@ -472,6 +482,7 @@ pub mod tests {
&self,
specifier: &str,
referrer: &ModuleSpecifier,
_mode: ResolutionMode,
) -> Result<ModuleSpecifier, ResolveError> {
if let Some(map) = self.map.get(referrer) {
if let Some(resolved_specifier) = map.get(specifier) {
Expand Down