From 826e42a5b5880c974ae019a7a21aade6a718062c Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 1 Nov 2024 12:27:00 -0400 Subject: fix: improved support for cjs and cts modules (#26558) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * cts support * better cjs/cts type checking * deno compile cjs/cts support * More efficient detect cjs (going towards stabilization) * Determination of whether .js, .ts, .jsx, or .tsx is cjs or esm is only done after loading * Support `import x = require(...);` Co-authored-by: Bartek IwaƄczuk --- resolvers/node/errors.rs | 58 ------------------------------------------------ 1 file changed, 58 deletions(-) (limited to 'resolvers/node/errors.rs') diff --git a/resolvers/node/errors.rs b/resolvers/node/errors.rs index 4ba829eda..aacbecefb 100644 --- a/resolvers/node/errors.rs +++ b/resolvers/node/errors.rs @@ -81,29 +81,6 @@ pub trait NodeJsErrorCoded { fn code(&self) -> NodeJsErrorCode; } -kinded_err!( - ResolvePkgSubpathFromDenoModuleError, - ResolvePkgSubpathFromDenoModuleErrorKind -); - -impl NodeJsErrorCoded for ResolvePkgSubpathFromDenoModuleError { - fn code(&self) -> NodeJsErrorCode { - use ResolvePkgSubpathFromDenoModuleErrorKind::*; - match self.as_kind() { - PackageSubpathResolve(e) => e.code(), - UrlToNodeResolution(e) => e.code(), - } - } -} - -#[derive(Debug, Error)] -pub enum ResolvePkgSubpathFromDenoModuleErrorKind { - #[error(transparent)] - PackageSubpathResolve(#[from] PackageSubpathResolveError), - #[error(transparent)] - UrlToNodeResolution(#[from] UrlToNodeResolutionError), -} - // todo(https://github.com/denoland/deno_core/issues/810): make this a TypeError #[derive(Debug, Clone, Error)] #[error( @@ -394,37 +371,6 @@ impl NodeJsErrorCoded for CanonicalizingPkgJsonDirError { } } -#[derive(Debug, Error)] -#[error("TypeScript files are not supported in npm packages: {specifier}")] -pub struct TypeScriptNotSupportedInNpmError { - pub specifier: Url, -} - -impl NodeJsErrorCoded for TypeScriptNotSupportedInNpmError { - fn code(&self) -> NodeJsErrorCode { - NodeJsErrorCode::ERR_UNKNOWN_FILE_EXTENSION - } -} - -kinded_err!(UrlToNodeResolutionError, UrlToNodeResolutionErrorKind); - -impl NodeJsErrorCoded for UrlToNodeResolutionError { - fn code(&self) -> NodeJsErrorCode { - match self.as_kind() { - UrlToNodeResolutionErrorKind::TypeScriptNotSupported(e) => e.code(), - UrlToNodeResolutionErrorKind::ClosestPkgJson(e) => e.code(), - } - } -} - -#[derive(Debug, Error)] -pub enum UrlToNodeResolutionErrorKind { - #[error(transparent)] - TypeScriptNotSupported(#[from] TypeScriptNotSupportedInNpmError), - #[error(transparent)] - ClosestPkgJson(#[from] ClosestPkgJsonError), -} - // todo(https://github.com/denoland/deno_core/issues/810): make this a TypeError #[derive(Debug, Error)] #[error( @@ -533,8 +479,6 @@ pub enum NodeResolveErrorKind { TypesNotFound(#[from] TypesNotFoundError), #[error(transparent)] FinalizeResolution(#[from] FinalizeResolutionError), - #[error(transparent)] - UrlToNodeResolution(#[from] UrlToNodeResolutionError), } kinded_err!(FinalizeResolutionError, FinalizeResolutionErrorKind); @@ -728,8 +672,6 @@ pub enum ResolvePkgJsonBinExportError { MissingPkgJson { pkg_json_path: PathBuf }, #[error("Failed resolving binary export. {message}")] InvalidBinProperty { message: String }, - #[error(transparent)] - UrlToNodeResolution(#[from] UrlToNodeResolutionError), } #[derive(Debug, Error)] -- cgit v1.2.3 From 48b94c099526eb262287e101a75cb4571b8972b0 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Fri, 15 Nov 2024 23:22:50 -0500 Subject: refactor: use boxed_error in some places (#26887) --- resolvers/node/errors.rs | 70 +++++++++++++++++++----------------------------- 1 file changed, 27 insertions(+), 43 deletions(-) (limited to 'resolvers/node/errors.rs') diff --git a/resolvers/node/errors.rs b/resolvers/node/errors.rs index aacbecefb..0f332d2c9 100644 --- a/resolvers/node/errors.rs +++ b/resolvers/node/errors.rs @@ -4,39 +4,13 @@ use std::borrow::Cow; use std::fmt::Write; use std::path::PathBuf; +use boxed_error::Boxed; use thiserror::Error; use url::Url; use crate::NodeModuleKind; use crate::NodeResolutionMode; -macro_rules! kinded_err { - ($name:ident, $kind_name:ident) => { - #[derive(Error, Debug)] - #[error(transparent)] - pub struct $name(pub Box<$kind_name>); - - impl $name { - pub fn as_kind(&self) -> &$kind_name { - &self.0 - } - - pub fn into_kind(self) -> $kind_name { - *self.0 - } - } - - impl From for $name - where - $kind_name: From, - { - fn from(err: E) -> Self { - $name(Box::new($kind_name::from(err))) - } - } - }; -} - #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] #[allow(non_camel_case_types)] pub enum NodeJsErrorCode { @@ -102,7 +76,8 @@ impl NodeJsErrorCoded for InvalidModuleSpecifierError { } } -kinded_err!(LegacyResolveError, LegacyResolveErrorKind); +#[derive(Debug, Boxed)] +pub struct LegacyResolveError(pub Box); #[derive(Debug, Error)] pub enum LegacyResolveErrorKind { @@ -121,8 +96,6 @@ impl NodeJsErrorCoded for LegacyResolveError { } } -kinded_err!(PackageFolderResolveError, PackageFolderResolveErrorKind); - #[derive(Debug, Error)] #[error( "Could not find package '{}' from referrer '{}'{}.", @@ -186,6 +159,9 @@ impl NodeJsErrorCoded for PackageFolderResolveError { } } +#[derive(Debug, Boxed)] +pub struct PackageFolderResolveError(pub Box); + #[derive(Debug, Error)] pub enum PackageFolderResolveErrorKind { #[error(transparent)] @@ -196,8 +172,6 @@ pub enum PackageFolderResolveErrorKind { Io(#[from] PackageFolderResolveIoError), } -kinded_err!(PackageSubpathResolveError, PackageSubpathResolveErrorKind); - impl NodeJsErrorCoded for PackageSubpathResolveError { fn code(&self) -> NodeJsErrorCode { match self.as_kind() { @@ -208,6 +182,9 @@ impl NodeJsErrorCoded for PackageSubpathResolveError { } } +#[derive(Debug, Boxed)] +pub struct PackageSubpathResolveError(pub Box); + #[derive(Debug, Error)] pub enum PackageSubpathResolveErrorKind { #[error(transparent)] @@ -252,8 +229,6 @@ impl NodeJsErrorCoded for PackageTargetNotFoundError { } } -kinded_err!(PackageTargetResolveError, PackageTargetResolveErrorKind); - impl NodeJsErrorCoded for PackageTargetResolveError { fn code(&self) -> NodeJsErrorCode { match self.as_kind() { @@ -266,6 +241,9 @@ impl NodeJsErrorCoded for PackageTargetResolveError { } } +#[derive(Debug, Boxed)] +pub struct PackageTargetResolveError(pub Box); + #[derive(Debug, Error)] pub enum PackageTargetResolveErrorKind { #[error(transparent)] @@ -280,8 +258,6 @@ pub enum PackageTargetResolveErrorKind { TypesNotFound(#[from] TypesNotFoundError), } -kinded_err!(PackageExportsResolveError, PackageExportsResolveErrorKind); - impl NodeJsErrorCoded for PackageExportsResolveError { fn code(&self) -> NodeJsErrorCode { match self.as_kind() { @@ -291,6 +267,9 @@ impl NodeJsErrorCoded for PackageExportsResolveError { } } +#[derive(Debug, Boxed)] +pub struct PackageExportsResolveError(pub Box); + #[derive(Debug, Error)] pub enum PackageExportsResolveErrorKind { #[error(transparent)] @@ -338,8 +317,6 @@ impl NodeJsErrorCoded for PackageJsonLoadError { } } -kinded_err!(ClosestPkgJsonError, ClosestPkgJsonErrorKind); - impl NodeJsErrorCoded for ClosestPkgJsonError { fn code(&self) -> NodeJsErrorCode { match self.as_kind() { @@ -349,6 +326,9 @@ impl NodeJsErrorCoded for ClosestPkgJsonError { } } +#[derive(Debug, Boxed)] +pub struct ClosestPkgJsonError(pub Box); + #[derive(Debug, Error)] pub enum ClosestPkgJsonErrorKind { #[error(transparent)] @@ -392,7 +372,8 @@ impl NodeJsErrorCoded for PackageImportNotDefinedError { } } -kinded_err!(PackageImportsResolveError, PackageImportsResolveErrorKind); +#[derive(Debug, Boxed)] +pub struct PackageImportsResolveError(pub Box); #[derive(Debug, Error)] pub enum PackageImportsResolveErrorKind { @@ -417,8 +398,6 @@ impl NodeJsErrorCoded for PackageImportsResolveErrorKind { } } -kinded_err!(PackageResolveError, PackageResolveErrorKind); - impl NodeJsErrorCoded for PackageResolveError { fn code(&self) -> NodeJsErrorCode { match self.as_kind() { @@ -431,6 +410,9 @@ impl NodeJsErrorCoded for PackageResolveError { } } +#[derive(Debug, Boxed)] +pub struct PackageResolveError(pub Box); + #[derive(Debug, Error)] pub enum PackageResolveErrorKind { #[error(transparent)] @@ -461,7 +443,8 @@ pub struct DataUrlReferrerError { pub source: url::ParseError, } -kinded_err!(NodeResolveError, NodeResolveErrorKind); +#[derive(Debug, Boxed)] +pub struct NodeResolveError(pub Box); #[derive(Debug, Error)] pub enum NodeResolveErrorKind { @@ -481,7 +464,8 @@ pub enum NodeResolveErrorKind { FinalizeResolution(#[from] FinalizeResolutionError), } -kinded_err!(FinalizeResolutionError, FinalizeResolutionErrorKind); +#[derive(Debug, Boxed)] +pub struct FinalizeResolutionError(pub Box); #[derive(Debug, Error)] pub enum FinalizeResolutionErrorKind { -- cgit v1.2.3