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

Code before:
      .resolve(resolution_mode);
    let referrer = UrlOrPathRef::from_url(referrer);
    let (url, resolved_kind) = self.module_resolve(
      specifier,
      &referrer,
      resolution_mode,
      conditions,
      resolution_kind,
    )?;

    let url_or_path =
      self.finalize_resolution(url, resolved_kind, Some(&referrer))?;
    (|| {
      let package_resolution_lookup_cache =
        self.package_resolution_lookup_cache.as_ref()?;
      if specifier_is_url
        || specifier.starts_with("./")
        || specifier.starts_with("../")
        || specifier.starts_with("/")
      {
        return None;
      }
      let url = url_or_path.clone().into_url().ok()?;
      package_resolution_lookup_cache.insert(url, specifier.to_string());
      Some(())
    })();
    let resolve_response = NodeResolution::Module(url_or_path);
    // TODO(bartlomieju): skipped checking errors for commonJS resolution and
    // "preserveSymlinksMain"/"preserveSymlinks" options.
    Ok(resolve_response)
  }

  fn module_resolve(
    &self,
    specifier: &str,
    referrer: &UrlOrPathRef,
    resolution_mode: ResolutionMode,
    conditions: &[&str],
    resolution_kind: NodeResolutionKind,
  ) -> Result<(MaybeTypesResolvedUrl, ResolvedMethod), NodeResolveError> {
    if should_be_treated_as_relative_or_absolute_path(specifier) {
      let referrer_url = referrer.url()?;
      let url = node_join_url(referrer_url, specifier).map_err(|err| {
        NodeResolveRelativeJoinError {
          path: specifier.to_string(),
          base: referrer_url.clone(),
          source: err,
        }
      })?;
      let url = self.maybe_resolve_types(
        LocalUrlOrPath::Url(url),

Review comment (refactor):
Not too big of a fan of these IIFEs. I find it harder to follow along than if statements or extracting out a method/function.

Code after:
      .resolve(resolution_mode);
    let referrer = UrlOrPathRef::from_url(referrer);
    let (url, resolved_kind) = self.module_resolve(
      specifier,
      &referrer,
      resolution_mode,
      conditions,
      resolution_kind,
    )?;

    let url_or_path =
      self.finalize_resolution(url, resolved_kind, Some(&referrer))?;
    let maybe_cache_resolution = || {
      let package_resolution_lookup_cache =
        self.package_resolution_lookup_cache.as_ref()?;
      if specifier_is_url
        || specifier.starts_with("./")
        || specifier.starts_with("../")
        || specifier.starts_with("/")
      {
        return None;
      }
      let url = url_or_path.clone().into_url().ok()?;
      package_resolution_lookup_cache.insert(url, specifier.to_string());
      Some(())
    };
    maybe_cache_resolution();
    let resolve_response = NodeResolution::Module(url_or_path);
    // TODO(bartlomieju): skipped checking errors for commonJS resolution and
    // "preserveSymlinksMain"/"preserveSymlinks" options.
    Ok(resolve_response)
  }

  fn module_resolve(
    &self,
    specifier: &str,
    referrer: &UrlOrPathRef,
    resolution_mode: ResolutionMode,
    conditions: &[&str],
    resolution_kind: NodeResolutionKind,
  ) -> Result<(MaybeTypesResolvedUrl, ResolvedMethod), NodeResolveError> {
    if should_be_treated_as_relative_or_absolute_path(specifier) {
      let referrer_url = referrer.url()?;
      let url = node_join_url(referrer_url, specifier).map_err(|err| {
        NodeResolveRelativeJoinError {
          path: specifier.to_string(),
          base: referrer_url.clone(),
          source: err,
        }
      })?;
      let url = self.maybe_resolve_types(