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

fix: npm export missing declaration file error

This commit is contained in:
Marvin Hagemeister 2023-12-08 10:35:29 +01:00
parent b24356d9b9
commit ee03656d9c
6 changed files with 103 additions and 23 deletions

View file

@ -424,6 +424,14 @@ itest!(types_exports_import_types {
exit_code: 1,
});
itest!(types_export_missing_declaration_file {
args: "check --all npm/types_export_missing_declaration_file/main.ts",
output: "npm/types_export_missing_declaration_file/main.out",
envs: env_vars_for_npm_tests(),
http_server: true,
exit_code: 1,
});
itest!(types_no_types_entry {
args: "check --all npm/types_no_types_entry/main.ts",
output: "npm/types_no_types_entry/main.out",

View file

@ -8,6 +8,9 @@
"require": "./lib/foo.js",
"import": "./lib/foo-esm.js"
},
"./sub": {
"default": "./lib/foo.js"
},
"./*": "./*"
},
"type": "module"

View file

@ -0,0 +1,5 @@
Download http://localhost:4545/npm/registry/@denotest/no-types-in-conditional-exports
Download http://localhost:4545/npm/registry/@denotest/no-types-in-conditional-exports/1.0.0.tgz
Check file:///[WILDCARD]/cli/tests/testdata/npm/types_export_missing_declaration_file/main.ts
error: Uncaught Error: [TS7016] Could not find a declaration file for subpath ./sub in /[WILDCARD]/npm/registry/@denotest/no-types-in-conditional-exports/1.0.0/package.json. /[WILDCARD]/npm/registry/@denotest/no-types-in-conditional-exports/1.0.0/lib/foo.js implicitly has an 'any' type.
[WILDCARD]

View file

@ -0,0 +1,2 @@
// should error here
import "npm:@denotest/no-types-in-conditional-exports/sub";

View file

@ -1,5 +1,6 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use std::path::Path;
use std::path::PathBuf;
use deno_core::error::generic_error;
@ -92,6 +93,21 @@ pub fn err_invalid_package_target(
generic_error(msg)
}
pub fn err_missing_declaration_file(
path: &PathBuf,
package_json_path: &Path,
subpath: &str,
) -> AnyError {
let msg = format!(
"[TS7016] Could not find a declaration file for subpath {} in {}. {} implicitly has an 'any' type.",
subpath,
&package_json_path.display(),
&path.display()
);
generic_error(msg)
}
pub fn err_package_path_not_exported(
mut pkg_path: String,
subpath: &str,

View file

@ -340,6 +340,29 @@ impl NodeResolver {
let package_subpath = package_subpath
.map(|s| format!("./{s}"))
.unwrap_or_else(|| ".".to_string());
let maybe_resolved_path2 = self.resolve_package_subpath(
&package_json,
&package_subpath,
referrer,
node_module_kind,
DEFAULT_CONDITIONS,
mode,
permissions,
);
if !mode.is_types() {
maybe_resolved_path2.with_context(|| {
format!(
"Failed resolving package subpath '{}' for '{}'",
package_subpath,
package_json.path.display()
)
})?;
} else if maybe_resolved_path2.is_err() {
maybe_resolved_path2?;
}
let maybe_resolved_path = self
.resolve_package_subpath(
&package_json,
@ -351,11 +374,15 @@ impl NodeResolver {
permissions,
)
.with_context(|| {
format!(
"Failed resolving package subpath '{}' for '{}'",
package_subpath,
package_json.path.display()
)
if !mode.is_types() {
format!(
"Failed resolving package subpath '{}' for '{}'",
package_subpath,
package_json.path.display()
)
} else {
"".to_string()
}
})?;
let resolved_path = match maybe_resolved_path {
Some(resolved_path) => resolved_path,
@ -744,27 +771,38 @@ impl NodeResolver {
permissions: &dyn NodePermissions,
) -> Result<Option<PathBuf>, AnyError> {
if let Some(target) = target.as_str() {
return self
.resolve_package_target_string(
target.to_string(),
subpath,
package_subpath,
package_json_path,
referrer,
referrer_kind,
pattern,
internal,
conditions,
mode,
permissions,
)
.map(|path| {
return match self.resolve_package_target_string(
target.to_string(),
subpath,
package_subpath,
package_json_path,
referrer,
referrer_kind,
pattern,
internal,
conditions,
mode,
permissions,
) {
Ok(path) => {
if mode.is_types() {
self.path_to_declaration_path(path, referrer_kind)
if let Some(decl_path) =
self.path_to_declaration_path(path.to_owned(), referrer_kind)
{
return Ok(Some(decl_path));
} else {
Err(throw_missing_declaration_file(
&path,
&package_json_path,
&package_subpath,
))
}
} else {
Some(path)
Ok(Some(path))
}
});
}
Err(err) => Err(err),
};
} else if let Some(target_arr) = target.as_array() {
if target_arr.is_empty() {
return Ok(None);
@ -1430,6 +1468,14 @@ fn throw_invalid_package_target(
)
}
fn throw_missing_declaration_file(
path: &PathBuf,
package_json_path: &Path,
subpath: &str,
) -> AnyError {
errors::err_missing_declaration_file(&path, &package_json_path, &subpath)
}
fn throw_invalid_subpath(
subpath: String,
package_json_path: &Path,