deno/libs/node_resolver/npm.rs
David Sherret c27d8f047b
Some checks are pending
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / build libs (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions
fix: make '@types' package resolution more consistent (#31512)
Shifts `@types` package resolution down into node_resolver so it's
always done. We were being inconsistent.
2025-12-06 11:59:06 -05:00

52 lines
1.5 KiB
Rust

// Copyright 2018-2025 the Deno authors. MIT license.
use std::path::Path;
use std::path::PathBuf;
use deno_path_util::url_from_directory_path;
use deno_path_util::url_from_file_path;
use deno_semver::Version;
use url::Url;
use crate::errors;
use crate::path::PathClean;
use crate::path::UrlOrPathRef;
pub trait NpmPackageFolderResolver {
/// Resolves an npm package folder path from the specified referrer.
fn resolve_package_folder_from_package(
&self,
specifier: &str,
referrer: &UrlOrPathRef,
) -> Result<PathBuf, errors::PackageFolderResolveError>;
/// Finds the `@types` package closest to the provided `@types` package name
/// and version of the original package.
fn resolve_types_package_folder(
&self,
types_package_name: &str,
maybe_package_version: Option<&Version>,
maybe_referrer: Option<&UrlOrPathRef>,
) -> Option<PathBuf>;
}
/// Checks if a provided specifier is in an npm package.
pub trait InNpmPackageChecker {
fn in_npm_package(&self, specifier: &Url) -> bool;
fn in_npm_package_at_dir_path(&self, path: &Path) -> bool {
let specifier = match url_from_directory_path(&path.to_path_buf().clean()) {
Ok(p) => p,
Err(_) => return false,
};
self.in_npm_package(&specifier)
}
fn in_npm_package_at_file_path(&self, path: &Path) -> bool {
let specifier = match url_from_file_path(&path.to_path_buf().clean()) {
Ok(p) => p,
Err(_) => return false,
};
self.in_npm_package(&specifier)
}
}