Repository: denoland/deno
File: resolvers/node/resolution.rs (Rust)

Code before:
use crate::errors::PackageImportNotDefinedError;
use crate::errors::PackageImportsResolveError;
use crate::errors::PackageImportsResolveErrorKind;
use crate::errors::PackagePathNotExportedError;
use crate::errors::PackageResolveError;
use crate::errors::PackageSubpathResolveError;
use crate::errors::PackageSubpathResolveErrorKind;
use crate::errors::PackageTargetNotFoundError;
use crate::errors::PackageTargetResolveError;
use crate::errors::PackageTargetResolveErrorKind;
use crate::errors::ResolveBinaryCommandsError;
use crate::errors::ResolvePkgJsonBinExportError;
use crate::errors::TypesNotFoundError;
use crate::errors::TypesNotFoundErrorData;
use crate::errors::UnsupportedDirImportError;
use crate::errors::UnsupportedEsmUrlSchemeError;
use crate::npm::InNpmPackageCheckerRc;
use crate::NpmPackageFolderResolverRc;
use crate::PackageJsonResolverRc;
use crate::PathClean;

pub static DEFAULT_CONDITIONS: &[&str] = &["deno", "node", "import"];
pub static REQUIRE_CONDITIONS: &[&str] = &["require", "node"];
static TYPES_ONLY_CONDITIONS: &[&str] = &["types"];

pub fn deno_conditions_from_resolution_mode(
  resolution_mode: ResolutionMode,
) -> &'static [&'static str] {
  match resolution_mode {
    ResolutionMode::Import => DEFAULT_CONDITIONS,
    ResolutionMode::Require => REQUIRE_CONDITIONS,
  }
}

pub struct ConditionsFromResolutionMode {
  pub func: Box<
    dyn Fn(ResolutionMode) -> &'static [&'static str] + Send + Sync + 'static,
  >,
}

impl Debug for ConditionsFromResolutionMode {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("ConditionsFromResolutionMode").finish()
  }
}

impl ConditionsFromResolutionMode {
  pub fn new(
    func: Option<
      Box<
        dyn Fn(ResolutionMode) -> &'static [&'static str]

Review comment (suggestion):
Perhaps this can instead be a method on the `ResolutionMode` enum?

```
impl ResolutionMode {
  pub fn default_conditions(&self) -> &'static [&'static str] {
    // ...
  }
}

Code after:
use crate::errors::PackageImportNotDefinedError;
use crate::errors::PackageImportsResolveError;
use crate::errors::PackageImportsResolveErrorKind;
use crate::errors::PackagePathNotExportedError;
use crate::errors::PackageResolveError;
use crate::errors::PackageSubpathResolveError;
use crate::errors::PackageSubpathResolveErrorKind;
use crate::errors::PackageTargetNotFoundError;
use crate::errors::PackageTargetResolveError;
use crate::errors::PackageTargetResolveErrorKind;
use crate::errors::ResolveBinaryCommandsError;
use crate::errors::ResolvePkgJsonBinExportError;
use crate::errors::TypesNotFoundError;
use crate::errors::TypesNotFoundErrorData;
use crate::errors::UnsupportedDirImportError;
use crate::errors::UnsupportedEsmUrlSchemeError;
use crate::npm::InNpmPackageCheckerRc;
use crate::NpmPackageFolderResolverRc;
use crate::PackageJsonResolverRc;
use crate::PathClean;

pub static DEFAULT_CONDITIONS: &[&str] = &["deno", "node", "import"];
pub static REQUIRE_CONDITIONS: &[&str] = &["require", "node"];
static TYPES_ONLY_CONDITIONS: &[&str] = &["types"];

type ConditionsFromResolutionModeFn = Box<
  dyn Fn(ResolutionMode) -> &'static [&'static str] + Send + Sync + 'static,
>;

#[derive(Default)]
pub struct ConditionsFromResolutionMode(Option<ConditionsFromResolutionModeFn>);

impl Debug for ConditionsFromResolutionMode {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("ConditionsFromResolutionMode").finish()
  }
}

impl ConditionsFromResolutionMode {
  pub fn new(func: ConditionsFromResolutionModeFn) -> Self {
    Self(Some(func))
  }

  fn resolve(
    &self,
    resolution_mode: ResolutionMode,
  ) -> &'static [&'static str] {
    match &self.0 {
      Some(func) => func(ResolutionMode::Import),
      None => resolution_mode.default_conditions(),
    }