Repository: denoland/deno
File: libs/node_resolver/resolution.rs (Rust)

Code before:
        if url.scheme() == "file" {
          (url_to_file_path(&url)?, Some(url))
        } else {
          return Ok(UrlOrPath::Url(url));
        }
      }
      LocalUrlOrPath::Path(LocalPath { path, known_exists }) => {
        if known_exists {
          // no need to do the finalization checks
          return Ok(UrlOrPath::Path(path));
        } else {
          (path, None)
        }
      }
    };

    // TODO(bartlomieju): currently not supported
    // if (getOptionValue('--experimental-specifier-resolution') === 'node') {
    //   ...
    // }

    let p_str = path.to_str().unwrap();
    let path = p_str
      .strip_suffix('/')
      .map(Path::new)
      .unwrap_or(path.as_path());

    let maybe_file_type = self.sys.get_file_type(&path);
    match maybe_file_type {
      Ok(FileType::Dir) => {
        if resolution_mode == ResolutionMode::Import
          && !self.resolution_config.bundle_mode
        {
          let suggested_file_name = ["index.mjs", "index.js", "index.cjs"]
            .into_iter()
            .find(|e| self.sys.is_file(&path.join(e)));
          Err(
            UnsupportedDirImportError {
              dir_url: UrlOrPath::Path(path.to_path_buf()),
              maybe_referrer: maybe_referrer.map(|r| r.display()),
              suggested_file_name,
            }
            .into(),
          )
        } else {
          // prefer the file over the directory
          let path_with_ext = with_known_extension(&path, "js");
          if self.sys.is_file(&path_with_ext) {
            Ok(UrlOrPath::Path(path_with_ext))
          } else {
            let (resolved_url, resolved_method) = self

Review comment (suggestion):
To avoid an allocation, maybe use `Cow` here and then below `.into_owned()`? (instead of `.to_path_buf()`)

Code after:
        if url.scheme() == "file" {
          (url_to_file_path(&url)?, Some(url))
        } else {
          return Ok(UrlOrPath::Url(url));
        }
      }
      LocalUrlOrPath::Path(LocalPath { path, known_exists }) => {
        if known_exists {
          // no need to do the finalization checks
          return Ok(UrlOrPath::Path(path));
        } else {
          (path, None)
        }
      }
    };

    // TODO(bartlomieju): currently not supported
    // if (getOptionValue('--experimental-specifier-resolution') === 'node') {
    //   ...
    // }

    let p_str = path.to_str().unwrap();
    let path = match p_str.strip_suffix('/') {
      Some(s) => Cow::Borrowed(Path::new(s)),
      None => Cow::Owned(path),
    };

    let maybe_file_type = self.sys.get_file_type(&path);
    match maybe_file_type {
      Ok(FileType::Dir) => {
        if resolution_mode == ResolutionMode::Import
          && !self.resolution_config.bundle_mode
        {
          let suggested_file_name = ["index.mjs", "index.js", "index.cjs"]
            .into_iter()
            .find(|e| self.sys.is_file(&path.join(e)));
          Err(
            UnsupportedDirImportError {
              dir_url: UrlOrPath::Path(path.into_owned()),
              maybe_referrer: maybe_referrer.map(|r| r.display()),
              suggested_file_name,
            }
            .into(),
          )
        } else {
          // prefer the file over the directory
          let path_with_ext = with_known_extension(&path, "js");
          if self.sys.is_file(&path_with_ext) {
            Ok(UrlOrPath::Path(path_with_ext))
          } else {
            let (resolved_url, resolved_method) = self