1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-21 23:04:45 -05:00

fix(npm): properly handle getting @types package for scoped packages (#16655)

This commit is contained in:
David Sherret 2022-11-15 21:48:29 -05:00 committed by GitHub
parent 6da6ed8985
commit 38542e849d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View file

@ -128,3 +128,25 @@ pub fn ensure_registry_read_permission(
format!("Reading {} is not allowed", path.display()),
))
}
/// Gets the corresponding @types package for the provided package name.
pub fn types_package_name(package_name: &str) -> String {
debug_assert!(!package_name.starts_with("@types/"));
// Scoped packages will get two underscores for each slash
// https://github.com/DefinitelyTyped/DefinitelyTyped/tree/15f1ece08f7b498f4b9a2147c2a46e94416ca777#what-about-scoped-packages
format!("@types/{}", package_name.replace('/', "__"))
}
#[cfg(test)]
mod test {
use super::types_package_name;
#[test]
fn test_types_package_name() {
assert_eq!(types_package_name("name"), "@types/name");
assert_eq!(
types_package_name("@scoped/package"),
"@types/@scoped__package"
);
}
}

View file

@ -26,6 +26,7 @@ use crate::npm::NpmPackageReq;
use crate::npm::RealNpmRegistryApi;
use super::common::ensure_registry_read_permission;
use super::common::types_package_name;
use super::common::InnerNpmPackageResolver;
/// Resolves packages from the global npm cache.
@ -97,7 +98,7 @@ impl InnerNpmPackageResolver for GlobalNpmPackageResolver {
}
}
let name = format!("@types/{}", name);
let name = types_package_name(name);
let pkg = self
.resolution
.resolve_package_from_package(&name, &referrer_pkg_id)?;

View file

@ -34,6 +34,7 @@ use crate::npm::NpmResolutionPackage;
use crate::npm::RealNpmRegistryApi;
use super::common::ensure_registry_read_permission;
use super::common::types_package_name;
use super::common::InnerNpmPackageResolver;
/// Resolver that creates a local node_modules directory
@ -172,7 +173,7 @@ impl InnerNpmPackageResolver for LocalNpmPackageResolver {
// if doing type resolution, check for the existance of a @types package
if conditions == TYPES_CONDITIONS && !name.starts_with("@types/") {
let sub_dir =
join_package_name(current_folder, &format!("@types/{}", name));
join_package_name(current_folder, &types_package_name(name));
if sub_dir.is_dir() {
return Ok(sub_dir);
}