Repository: denoland/deno
File: libs/resolver/workspace.rs (Rust)

Code before:

  fn base_message_for_specifier(&self, specifier: &Url) -> String {
    match self {
      Self::JsToTs => {
        let media_type = MediaType::from_specifier(specifier);
        format!("change the extension to '{}'", media_type.as_ts_extension())
      }
      Self::NoExtension => {
        let media_type = MediaType::from_specifier(specifier);
        format!("add a '{}' extension", media_type.as_ts_extension())
      }
      Self::Directory => {
        let file_name = specifier
          .path()
          .rsplit_once('/')
          .map(|(_, file_name)| file_name)
          .unwrap_or(specifier.path());
        format!("specify path to '{}' file in directory instead", file_name)
      }
    }
  }
}

#[derive(Debug)]
struct SloppyImportsResolver<TSys: FsMetadata> {
  compiler_options_resolver: RwLock<CompilerOptionsResolverRc>,
  fs: CachedMetadataFs<TSys>,
  enabled_by_options: bool,
}

impl<TSys: FsMetadata> SloppyImportsResolver<TSys> {
  fn new(fs: CachedMetadataFs<TSys>, options: SloppyImportsOptions) -> Self {
    Self {
      compiler_options_resolver: Default::default(),
      fs,
      enabled_by_options: match options {
        SloppyImportsOptions::Enabled => true,
        SloppyImportsOptions::Unspecified => false,
      },
    }
  }

  pub fn set_compiler_options_resolver(
    &self,
    value: CompilerOptionsResolverRc,
  ) {
    *self.compiler_options_resolver.write() = value;
  }

  fn resolve(
    &self,

Review comment (suggestion):
Is there a way we could put this in something like a `CompilerOptionsResolverCell` and have that passed in as a ctor parameter then share that with the factory? That way we can always ensure the two are always in sync and then we don't need an explicit `set_compiler_options_resolver` method here.

Code after:

  fn base_message_for_specifier(&self, specifier: &Url) -> String {
    match self {
      Self::JsToTs => {
        let media_type = MediaType::from_specifier(specifier);
        format!("change the extension to '{}'", media_type.as_ts_extension())
      }
      Self::NoExtension => {
        let media_type = MediaType::from_specifier(specifier);
        format!("add a '{}' extension", media_type.as_ts_extension())
      }
      Self::Directory => {
        let file_name = specifier
          .path()
          .rsplit_once('/')
          .map(|(_, file_name)| file_name)
          .unwrap_or(specifier.path());
        format!("specify path to '{}' file in directory instead", file_name)
      }
    }
  }
}

#[derive(Debug)]
struct SloppyImportsResolver<TSys: FsMetadata> {
  compiler_options_resolver: CompilerOptionsResolverCellRc,
  fs: CachedMetadataFs<TSys>,
  enabled_by_options: bool,
}

impl<TSys: FsMetadata> SloppyImportsResolver<TSys> {
  fn new(
    fs: CachedMetadataFs<TSys>,
    compiler_options_resolver: CompilerOptionsResolverCellRc,
    options: SloppyImportsOptions,
  ) -> Self {
    Self {
      fs,
      compiler_options_resolver,
      enabled_by_options: match options {
        SloppyImportsOptions::Enabled => true,
        SloppyImportsOptions::Unspecified => false,
      },
    }
  }

  fn resolve(
    &self,
    specifier: &Url,
    referrer: &Url,
    resolution_kind: ResolutionKind,