2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-11-23 18:38:07 -05:00
|
|
|
|
2024-11-29 18:54:26 -05:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use deno_ast::diagnostics::Diagnostic;
|
|
|
|
use deno_ast::diagnostics::DiagnosticLevel;
|
|
|
|
use deno_ast::diagnostics::DiagnosticLocation;
|
|
|
|
use deno_ast::diagnostics::DiagnosticSnippet;
|
|
|
|
use deno_ast::diagnostics::DiagnosticSnippetHighlight;
|
|
|
|
use deno_ast::diagnostics::DiagnosticSnippetHighlightStyle;
|
|
|
|
use deno_ast::diagnostics::DiagnosticSourcePos;
|
|
|
|
use deno_ast::diagnostics::DiagnosticSourceRange;
|
2023-11-23 18:38:07 -05:00
|
|
|
use deno_ast::ParsedSource;
|
2024-01-24 08:49:33 -05:00
|
|
|
use deno_ast::SourceRange;
|
2024-02-08 20:40:26 -05:00
|
|
|
use deno_ast::SourceTextInfo;
|
2024-11-29 18:54:26 -05:00
|
|
|
use deno_ast::SourceTextProvider;
|
2024-07-03 20:54:33 -04:00
|
|
|
use deno_config::workspace::MappedResolution;
|
2024-07-04 20:41:01 -04:00
|
|
|
use deno_config::workspace::PackageJsonDepResolution;
|
2024-07-03 20:54:33 -04:00
|
|
|
use deno_config::workspace::WorkspaceResolver;
|
2024-11-29 18:54:26 -05:00
|
|
|
use deno_core::anyhow;
|
2023-11-23 18:38:07 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
2023-12-01 15:12:10 -05:00
|
|
|
use deno_graph::DependencyDescriptor;
|
|
|
|
use deno_graph::DynamicTemplatePart;
|
2024-04-11 19:00:17 -04:00
|
|
|
use deno_graph::ParserModuleAnalyzer;
|
2023-11-23 18:38:07 -05:00
|
|
|
use deno_graph::TypeScriptReference;
|
2024-07-23 17:34:46 -04:00
|
|
|
use deno_package_json::PackageJsonDepValue;
|
2024-11-29 18:54:26 -05:00
|
|
|
use deno_package_json::PackageJsonDepWorkspaceReq;
|
2024-11-26 14:38:24 -05:00
|
|
|
use deno_resolver::sloppy_imports::SloppyImportsResolutionKind;
|
2024-02-27 10:13:16 -05:00
|
|
|
use deno_runtime::deno_node::is_builtin_node_module;
|
2024-11-29 18:54:26 -05:00
|
|
|
use deno_semver::Version;
|
|
|
|
use deno_semver::VersionReq;
|
2024-02-23 09:56:49 -05:00
|
|
|
|
2024-09-28 19:17:48 -04:00
|
|
|
use crate::resolver::CliSloppyImportsResolver;
|
2023-11-23 18:38:07 -05:00
|
|
|
|
2024-01-24 08:49:33 -05:00
|
|
|
#[derive(Debug, Clone)]
|
2024-02-27 10:13:16 -05:00
|
|
|
pub enum SpecifierUnfurlerDiagnostic {
|
2024-01-24 08:49:33 -05:00
|
|
|
UnanalyzableDynamicImport {
|
|
|
|
specifier: ModuleSpecifier,
|
2024-02-08 20:40:26 -05:00
|
|
|
text_info: SourceTextInfo,
|
2024-01-24 08:49:33 -05:00
|
|
|
range: SourceRange,
|
|
|
|
},
|
2024-11-29 18:54:26 -05:00
|
|
|
ResolvingNpmWorkspacePackage {
|
|
|
|
specifier: ModuleSpecifier,
|
|
|
|
package_name: String,
|
|
|
|
text_info: SourceTextInfo,
|
|
|
|
range: SourceRange,
|
|
|
|
reason: String,
|
|
|
|
},
|
2024-01-24 08:49:33 -05:00
|
|
|
}
|
|
|
|
|
2024-11-29 18:54:26 -05:00
|
|
|
impl Diagnostic for SpecifierUnfurlerDiagnostic {
|
|
|
|
fn level(&self) -> DiagnosticLevel {
|
|
|
|
match self {
|
|
|
|
SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport { .. } => {
|
|
|
|
DiagnosticLevel::Warning
|
|
|
|
}
|
|
|
|
SpecifierUnfurlerDiagnostic::ResolvingNpmWorkspacePackage { .. } => {
|
|
|
|
DiagnosticLevel::Error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn code(&self) -> Cow<'_, str> {
|
2024-01-24 08:49:33 -05:00
|
|
|
match self {
|
|
|
|
Self::UnanalyzableDynamicImport { .. } => "unanalyzable-dynamic-import",
|
2024-11-29 18:54:26 -05:00
|
|
|
Self::ResolvingNpmWorkspacePackage { .. } => "npm-workspace-package",
|
2024-01-24 08:49:33 -05:00
|
|
|
}
|
2024-11-29 18:54:26 -05:00
|
|
|
.into()
|
2024-01-24 08:49:33 -05:00
|
|
|
}
|
|
|
|
|
2024-11-29 18:54:26 -05:00
|
|
|
fn message(&self) -> Cow<'_, str> {
|
2024-01-24 08:49:33 -05:00
|
|
|
match self {
|
|
|
|
Self::UnanalyzableDynamicImport { .. } => {
|
2024-11-29 18:54:26 -05:00
|
|
|
"unable to analyze dynamic import".into()
|
|
|
|
}
|
|
|
|
Self::ResolvingNpmWorkspacePackage {
|
|
|
|
package_name,
|
|
|
|
reason,
|
|
|
|
..
|
|
|
|
} => format!(
|
|
|
|
"failed resolving npm workspace package '{}': {}",
|
|
|
|
package_name, reason
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn location(&self) -> deno_ast::diagnostics::DiagnosticLocation {
|
|
|
|
match self {
|
|
|
|
SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport {
|
|
|
|
specifier,
|
|
|
|
text_info,
|
|
|
|
range,
|
|
|
|
} => DiagnosticLocation::ModulePosition {
|
|
|
|
specifier: Cow::Borrowed(specifier),
|
|
|
|
text_info: Cow::Borrowed(text_info),
|
|
|
|
source_pos: DiagnosticSourcePos::SourcePos(range.start),
|
|
|
|
},
|
|
|
|
SpecifierUnfurlerDiagnostic::ResolvingNpmWorkspacePackage {
|
|
|
|
specifier,
|
|
|
|
text_info,
|
|
|
|
range,
|
|
|
|
..
|
|
|
|
} => DiagnosticLocation::ModulePosition {
|
|
|
|
specifier: Cow::Borrowed(specifier),
|
|
|
|
text_info: Cow::Borrowed(text_info),
|
|
|
|
source_pos: DiagnosticSourcePos::SourcePos(range.start),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn snippet(&self) -> Option<deno_ast::diagnostics::DiagnosticSnippet<'_>> {
|
|
|
|
match self {
|
|
|
|
SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport {
|
|
|
|
text_info,
|
|
|
|
range,
|
|
|
|
..
|
|
|
|
} => Some(DiagnosticSnippet {
|
|
|
|
source: Cow::Borrowed(text_info),
|
|
|
|
highlights: vec![DiagnosticSnippetHighlight {
|
|
|
|
style: DiagnosticSnippetHighlightStyle::Warning,
|
|
|
|
range: DiagnosticSourceRange {
|
|
|
|
start: DiagnosticSourcePos::SourcePos(range.start),
|
|
|
|
end: DiagnosticSourcePos::SourcePos(range.end),
|
|
|
|
},
|
|
|
|
description: Some("the unanalyzable dynamic import".into()),
|
|
|
|
}],
|
|
|
|
}),
|
|
|
|
SpecifierUnfurlerDiagnostic::ResolvingNpmWorkspacePackage {
|
|
|
|
text_info,
|
|
|
|
range,
|
|
|
|
..
|
|
|
|
} => Some(DiagnosticSnippet {
|
|
|
|
source: Cow::Borrowed(text_info),
|
|
|
|
highlights: vec![DiagnosticSnippetHighlight {
|
|
|
|
style: DiagnosticSnippetHighlightStyle::Warning,
|
|
|
|
range: DiagnosticSourceRange {
|
|
|
|
start: DiagnosticSourcePos::SourcePos(range.start),
|
|
|
|
end: DiagnosticSourcePos::SourcePos(range.end),
|
|
|
|
},
|
|
|
|
description: Some("the unresolved import".into()),
|
|
|
|
}],
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hint(&self) -> Option<Cow<'_, str>> {
|
|
|
|
match self {
|
|
|
|
SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport { .. } => {
|
|
|
|
None
|
2024-01-24 08:49:33 -05:00
|
|
|
}
|
2024-11-29 18:54:26 -05:00
|
|
|
SpecifierUnfurlerDiagnostic::ResolvingNpmWorkspacePackage { .. } => Some(
|
|
|
|
"make sure the npm workspace package is resolvable and has a version field in its package.json".into()
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn snippet_fixed(
|
|
|
|
&self,
|
|
|
|
) -> Option<deno_ast::diagnostics::DiagnosticSnippet<'_>> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn info(&self) -> Cow<'_, [Cow<'_, str>]> {
|
|
|
|
match self {
|
|
|
|
SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport { .. } => Cow::Borrowed(&[
|
|
|
|
Cow::Borrowed("after publishing this package, imports from the local import map / package.json do not work"),
|
|
|
|
Cow::Borrowed("dynamic imports that can not be analyzed at publish time will not be rewritten automatically"),
|
|
|
|
Cow::Borrowed("make sure the dynamic import is resolvable at runtime without an import map / package.json")
|
|
|
|
]),
|
|
|
|
SpecifierUnfurlerDiagnostic::ResolvingNpmWorkspacePackage { .. } => {
|
|
|
|
Cow::Borrowed(&[])
|
|
|
|
},
|
2024-01-24 08:49:33 -05:00
|
|
|
}
|
|
|
|
}
|
2024-11-29 18:54:26 -05:00
|
|
|
|
|
|
|
fn docs_url(&self) -> Option<Cow<'_, str>> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum UnfurlSpecifierError {
|
|
|
|
Workspace {
|
|
|
|
package_name: String,
|
|
|
|
reason: String,
|
|
|
|
},
|
2024-01-24 08:49:33 -05:00
|
|
|
}
|
|
|
|
|
2024-07-04 20:41:01 -04:00
|
|
|
pub struct SpecifierUnfurler {
|
2024-11-29 18:54:26 -05:00
|
|
|
sloppy_imports_resolver: Option<Arc<CliSloppyImportsResolver>>,
|
|
|
|
workspace_resolver: Arc<WorkspaceResolver>,
|
2024-02-27 10:13:16 -05:00
|
|
|
bare_node_builtins: bool,
|
2023-11-23 18:38:07 -05:00
|
|
|
}
|
|
|
|
|
2024-07-04 20:41:01 -04:00
|
|
|
impl SpecifierUnfurler {
|
2024-02-27 10:13:16 -05:00
|
|
|
pub fn new(
|
2024-11-29 18:54:26 -05:00
|
|
|
sloppy_imports_resolver: Option<Arc<CliSloppyImportsResolver>>,
|
|
|
|
workspace_resolver: Arc<WorkspaceResolver>,
|
2024-02-27 10:13:16 -05:00
|
|
|
bare_node_builtins: bool,
|
|
|
|
) -> Self {
|
2024-07-04 20:41:01 -04:00
|
|
|
debug_assert_eq!(
|
|
|
|
workspace_resolver.pkg_json_dep_resolution(),
|
|
|
|
PackageJsonDepResolution::Enabled
|
|
|
|
);
|
2024-02-27 10:13:16 -05:00
|
|
|
Self {
|
|
|
|
sloppy_imports_resolver,
|
2024-07-03 20:54:33 -04:00
|
|
|
workspace_resolver,
|
2024-02-27 10:13:16 -05:00
|
|
|
bare_node_builtins,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-29 18:54:26 -05:00
|
|
|
fn unfurl_specifier_reporting_diagnostic(
|
2024-02-27 10:13:16 -05:00
|
|
|
&self,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
specifier: &str,
|
2024-11-29 18:54:26 -05:00
|
|
|
text_info: &SourceTextInfo,
|
|
|
|
range: &deno_graph::PositionRange,
|
|
|
|
diagnostic_reporter: &mut dyn FnMut(SpecifierUnfurlerDiagnostic),
|
2024-02-27 10:13:16 -05:00
|
|
|
) -> Option<String> {
|
2024-11-29 18:54:26 -05:00
|
|
|
match self.unfurl_specifier(referrer, specifier) {
|
|
|
|
Ok(maybe_unfurled) => maybe_unfurled,
|
|
|
|
Err(diagnostic) => match diagnostic {
|
|
|
|
UnfurlSpecifierError::Workspace {
|
|
|
|
package_name,
|
|
|
|
reason,
|
|
|
|
} => {
|
|
|
|
let range = to_range(text_info, range);
|
|
|
|
diagnostic_reporter(
|
|
|
|
SpecifierUnfurlerDiagnostic::ResolvingNpmWorkspacePackage {
|
|
|
|
specifier: referrer.clone(),
|
|
|
|
package_name,
|
|
|
|
text_info: text_info.clone(),
|
|
|
|
range: SourceRange::new(
|
|
|
|
text_info.start_pos() + range.start,
|
|
|
|
text_info.start_pos() + range.end,
|
|
|
|
),
|
|
|
|
reason,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn unfurl_specifier(
|
|
|
|
&self,
|
|
|
|
referrer: &ModuleSpecifier,
|
|
|
|
specifier: &str,
|
|
|
|
) -> Result<Option<String>, UnfurlSpecifierError> {
|
2024-07-03 20:54:33 -04:00
|
|
|
let resolved = if let Ok(resolved) =
|
|
|
|
self.workspace_resolver.resolve(specifier, referrer)
|
|
|
|
{
|
|
|
|
match resolved {
|
2024-08-20 10:11:43 -04:00
|
|
|
MappedResolution::Normal { specifier, .. }
|
|
|
|
| MappedResolution::ImportMap { specifier, .. } => Some(specifier),
|
2024-08-07 03:43:05 -04:00
|
|
|
MappedResolution::WorkspaceJsrPackage { pkg_req_ref, .. } => {
|
|
|
|
Some(ModuleSpecifier::parse(&pkg_req_ref.to_string()).unwrap())
|
|
|
|
}
|
2024-07-17 09:13:22 -04:00
|
|
|
MappedResolution::WorkspaceNpmPackage {
|
|
|
|
target_pkg_json: pkg_json,
|
|
|
|
pkg_name,
|
|
|
|
sub_path,
|
|
|
|
} => {
|
|
|
|
// todo(#24612): consider warning or error when this is also a jsr package?
|
|
|
|
ModuleSpecifier::parse(&format!(
|
|
|
|
"npm:{}{}{}",
|
|
|
|
pkg_name,
|
|
|
|
pkg_json
|
|
|
|
.version
|
|
|
|
.as_ref()
|
|
|
|
.map(|v| format!("@^{}", v))
|
|
|
|
.unwrap_or_default(),
|
|
|
|
sub_path
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| format!("/{}", s))
|
|
|
|
.unwrap_or_default()
|
|
|
|
))
|
|
|
|
.ok()
|
|
|
|
}
|
2024-07-03 20:54:33 -04:00
|
|
|
MappedResolution::PackageJson {
|
2024-07-17 09:13:22 -04:00
|
|
|
alias,
|
2024-07-03 20:54:33 -04:00
|
|
|
sub_path,
|
|
|
|
dep_result,
|
|
|
|
..
|
|
|
|
} => match dep_result {
|
|
|
|
Ok(dep) => match dep {
|
2024-07-17 09:13:22 -04:00
|
|
|
PackageJsonDepValue::Req(pkg_req) => {
|
|
|
|
// todo(#24612): consider warning or error when this is an npm workspace
|
|
|
|
// member that's also a jsr package?
|
|
|
|
ModuleSpecifier::parse(&format!(
|
|
|
|
"npm:{}{}",
|
|
|
|
pkg_req,
|
|
|
|
sub_path
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| format!("/{}", s))
|
|
|
|
.unwrap_or_default()
|
|
|
|
))
|
|
|
|
.ok()
|
|
|
|
}
|
2024-11-29 18:54:26 -05:00
|
|
|
PackageJsonDepValue::Workspace(workspace_version_req) => {
|
|
|
|
let version_req = match workspace_version_req {
|
|
|
|
PackageJsonDepWorkspaceReq::VersionReq(version_req) => {
|
|
|
|
Cow::Borrowed(version_req)
|
|
|
|
}
|
|
|
|
PackageJsonDepWorkspaceReq::Caret => {
|
|
|
|
let version = self
|
|
|
|
.find_workspace_npm_dep_version(alias)
|
|
|
|
.map_err(|err| UnfurlSpecifierError::Workspace {
|
|
|
|
package_name: alias.to_string(),
|
|
|
|
reason: err.to_string(),
|
|
|
|
})?;
|
|
|
|
// version was validated, so ok to unwrap
|
|
|
|
Cow::Owned(
|
|
|
|
VersionReq::parse_from_npm(&format!("^{}", version))
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
PackageJsonDepWorkspaceReq::Tilde => {
|
|
|
|
let version = self
|
|
|
|
.find_workspace_npm_dep_version(alias)
|
|
|
|
.map_err(|err| UnfurlSpecifierError::Workspace {
|
|
|
|
package_name: alias.to_string(),
|
|
|
|
reason: err.to_string(),
|
|
|
|
})?;
|
|
|
|
// version was validated, so ok to unwrap
|
|
|
|
Cow::Owned(
|
|
|
|
VersionReq::parse_from_npm(&format!("~{}", version))
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// todo(#24612): warn when this is also a jsr package telling
|
|
|
|
// people to map the specifiers in the import map
|
2024-07-17 09:13:22 -04:00
|
|
|
ModuleSpecifier::parse(&format!(
|
|
|
|
"npm:{}@{}{}",
|
|
|
|
alias,
|
|
|
|
version_req,
|
|
|
|
sub_path
|
|
|
|
.as_ref()
|
|
|
|
.map(|s| format!("/{}", s))
|
|
|
|
.unwrap_or_default()
|
|
|
|
))
|
|
|
|
.ok()
|
2024-07-03 20:54:33 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
log::warn!(
|
|
|
|
"Ignoring failed to resolve package.json dependency. {:#}",
|
|
|
|
err
|
|
|
|
);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2024-02-27 10:13:16 -05:00
|
|
|
let resolved = match resolved {
|
|
|
|
Some(resolved) => resolved,
|
|
|
|
None if self.bare_node_builtins && is_builtin_node_module(specifier) => {
|
|
|
|
format!("node:{specifier}").parse().unwrap()
|
|
|
|
}
|
2024-11-29 18:54:26 -05:00
|
|
|
None => match ModuleSpecifier::options()
|
2024-02-27 10:13:16 -05:00
|
|
|
.base_url(Some(referrer))
|
|
|
|
.parse(specifier)
|
2024-11-29 18:54:26 -05:00
|
|
|
.ok()
|
|
|
|
{
|
|
|
|
Some(value) => value,
|
|
|
|
None => return Ok(None),
|
|
|
|
},
|
2024-02-27 10:13:16 -05:00
|
|
|
};
|
|
|
|
// TODO(lucacasonato): this requires integration in deno_graph first
|
|
|
|
// let resolved = if let Ok(specifier) =
|
|
|
|
// NpmPackageReqReference::from_specifier(&resolved)
|
|
|
|
// {
|
|
|
|
// if let Some(scope_name) = specifier.req().name.strip_prefix("@jsr/") {
|
|
|
|
// let (scope, name) = scope_name.split_once("__")?;
|
|
|
|
// let new_specifier = JsrPackageReqReference::new(PackageReqReference {
|
|
|
|
// req: PackageReq {
|
|
|
|
// name: format!("@{scope}/{name}"),
|
|
|
|
// version_req: specifier.req().version_req.clone(),
|
|
|
|
// },
|
|
|
|
// sub_path: specifier.sub_path().map(ToOwned::to_owned),
|
|
|
|
// })
|
|
|
|
// .to_string();
|
|
|
|
// ModuleSpecifier::parse(&new_specifier).unwrap()
|
|
|
|
// } else {
|
|
|
|
// resolved
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// resolved
|
|
|
|
// };
|
|
|
|
let resolved =
|
2024-07-04 20:41:01 -04:00
|
|
|
if let Some(sloppy_imports_resolver) = &self.sloppy_imports_resolver {
|
2024-02-27 10:13:16 -05:00
|
|
|
sloppy_imports_resolver
|
2024-11-26 14:38:24 -05:00
|
|
|
.resolve(&resolved, SloppyImportsResolutionKind::Execution)
|
2024-07-25 09:07:59 -04:00
|
|
|
.map(|res| res.into_specifier())
|
|
|
|
.unwrap_or(resolved)
|
2024-02-27 10:13:16 -05:00
|
|
|
} else {
|
|
|
|
resolved
|
|
|
|
};
|
2024-02-29 11:34:07 -05:00
|
|
|
let relative_resolved = relative_url(&resolved, referrer);
|
|
|
|
if relative_resolved == specifier {
|
2024-11-29 18:54:26 -05:00
|
|
|
Ok(None) // nothing to unfurl
|
2024-02-29 11:34:07 -05:00
|
|
|
} else {
|
2024-07-04 20:41:01 -04:00
|
|
|
log::debug!(
|
|
|
|
"Unfurled specifier: {} from {} -> {}",
|
|
|
|
specifier,
|
|
|
|
referrer,
|
|
|
|
relative_resolved
|
|
|
|
);
|
2024-11-29 18:54:26 -05:00
|
|
|
Ok(Some(relative_resolved))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_workspace_npm_dep_version(
|
|
|
|
&self,
|
|
|
|
pkg_name: &str,
|
|
|
|
) -> Result<Version, anyhow::Error> {
|
|
|
|
// todo(#24612): warn when this is also a jsr package telling
|
|
|
|
// people to map the specifiers in the import map
|
|
|
|
let pkg_json = self
|
|
|
|
.workspace_resolver
|
|
|
|
.package_jsons()
|
|
|
|
.find(|pkg| pkg.name.as_deref() == Some(pkg_name))
|
|
|
|
.ok_or_else(|| {
|
|
|
|
anyhow::anyhow!("unable to find npm package in workspace")
|
|
|
|
})?;
|
|
|
|
if let Some(version) = &pkg_json.version {
|
|
|
|
Ok(Version::parse_from_npm(version)?)
|
|
|
|
} else {
|
|
|
|
Err(anyhow::anyhow!(
|
|
|
|
"missing version in package.json of npm package",
|
|
|
|
))
|
2024-02-29 11:34:07 -05:00
|
|
|
}
|
2024-02-27 10:13:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to unfurl the dynamic dependency returning `true` on success
|
|
|
|
/// or `false` when the import was not analyzable.
|
|
|
|
fn try_unfurl_dynamic_dep(
|
|
|
|
&self,
|
2024-08-23 20:21:21 -04:00
|
|
|
module_url: &ModuleSpecifier,
|
2024-06-05 11:04:16 -04:00
|
|
|
text_info: &SourceTextInfo,
|
2024-02-27 10:13:16 -05:00
|
|
|
dep: &deno_graph::DynamicDependencyDescriptor,
|
|
|
|
text_changes: &mut Vec<deno_ast::TextChange>,
|
2024-11-29 18:54:26 -05:00
|
|
|
diagnostic_reporter: &mut dyn FnMut(SpecifierUnfurlerDiagnostic),
|
2024-02-27 10:13:16 -05:00
|
|
|
) -> bool {
|
|
|
|
match &dep.argument {
|
|
|
|
deno_graph::DynamicArgument::String(specifier) => {
|
2024-06-05 11:04:16 -04:00
|
|
|
let range = to_range(text_info, &dep.argument_range);
|
|
|
|
let maybe_relative_index =
|
|
|
|
text_info.text_str()[range.start..range.end].find(specifier);
|
2024-02-27 10:13:16 -05:00
|
|
|
let Some(relative_index) = maybe_relative_index else {
|
2024-02-29 11:34:07 -05:00
|
|
|
return true; // always say it's analyzable for a string
|
2024-02-27 10:13:16 -05:00
|
|
|
};
|
2024-11-29 18:54:26 -05:00
|
|
|
let maybe_unfurled = self.unfurl_specifier_reporting_diagnostic(
|
|
|
|
module_url,
|
|
|
|
specifier,
|
|
|
|
text_info,
|
|
|
|
&dep.argument_range,
|
|
|
|
diagnostic_reporter,
|
|
|
|
);
|
|
|
|
if let Some(unfurled) = maybe_unfurled {
|
2024-02-29 11:34:07 -05:00
|
|
|
let start = range.start + relative_index;
|
|
|
|
text_changes.push(deno_ast::TextChange {
|
|
|
|
range: start..start + specifier.len(),
|
|
|
|
new_text: unfurled,
|
|
|
|
});
|
|
|
|
}
|
2024-02-27 10:13:16 -05:00
|
|
|
true
|
|
|
|
}
|
|
|
|
deno_graph::DynamicArgument::Template(parts) => match parts.first() {
|
|
|
|
Some(DynamicTemplatePart::String { value: specifier }) => {
|
|
|
|
// relative doesn't need to be modified
|
|
|
|
let is_relative =
|
|
|
|
specifier.starts_with("./") || specifier.starts_with("../");
|
|
|
|
if is_relative {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if !specifier.ends_with('/') {
|
|
|
|
return false;
|
|
|
|
}
|
2024-11-29 18:54:26 -05:00
|
|
|
let unfurled = self.unfurl_specifier_reporting_diagnostic(
|
|
|
|
module_url,
|
|
|
|
specifier,
|
|
|
|
text_info,
|
|
|
|
&dep.argument_range,
|
|
|
|
diagnostic_reporter,
|
|
|
|
);
|
2024-02-27 10:13:16 -05:00
|
|
|
let Some(unfurled) = unfurled else {
|
2024-02-29 11:34:07 -05:00
|
|
|
return true; // nothing to unfurl
|
2024-02-27 10:13:16 -05:00
|
|
|
};
|
2024-06-05 11:04:16 -04:00
|
|
|
let range = to_range(text_info, &dep.argument_range);
|
2024-02-27 10:13:16 -05:00
|
|
|
let maybe_relative_index =
|
2024-06-05 11:04:16 -04:00
|
|
|
text_info.text_str()[range.start..].find(specifier);
|
2024-02-27 10:13:16 -05:00
|
|
|
let Some(relative_index) = maybe_relative_index else {
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
let start = range.start + relative_index;
|
|
|
|
text_changes.push(deno_ast::TextChange {
|
|
|
|
range: start..start + specifier.len(),
|
|
|
|
new_text: unfurled,
|
|
|
|
});
|
|
|
|
true
|
|
|
|
}
|
|
|
|
Some(DynamicTemplatePart::Expr) => {
|
|
|
|
false // failed analyzing
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
true // ignore
|
|
|
|
}
|
|
|
|
},
|
|
|
|
deno_graph::DynamicArgument::Expr => {
|
|
|
|
false // failed analyzing
|
|
|
|
}
|
|
|
|
}
|
2023-11-23 18:38:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unfurl(
|
|
|
|
&self,
|
|
|
|
url: &ModuleSpecifier,
|
2024-01-23 06:40:23 -05:00
|
|
|
parsed_source: &ParsedSource,
|
2024-02-27 10:13:16 -05:00
|
|
|
diagnostic_reporter: &mut dyn FnMut(SpecifierUnfurlerDiagnostic),
|
2024-01-24 08:49:33 -05:00
|
|
|
) -> String {
|
2023-11-23 18:38:07 -05:00
|
|
|
let mut text_changes = Vec::new();
|
2024-06-05 11:04:16 -04:00
|
|
|
let text_info = parsed_source.text_info_lazy();
|
2024-04-11 19:00:17 -04:00
|
|
|
let module_info = ParserModuleAnalyzer::module_info(parsed_source);
|
2023-12-01 15:12:10 -05:00
|
|
|
let analyze_specifier =
|
|
|
|
|specifier: &str,
|
|
|
|
range: &deno_graph::PositionRange,
|
2024-11-29 18:54:26 -05:00
|
|
|
text_changes: &mut Vec<deno_ast::TextChange>,
|
|
|
|
diagnostic_reporter: &mut dyn FnMut(SpecifierUnfurlerDiagnostic)| {
|
|
|
|
if let Some(unfurled) = self.unfurl_specifier_reporting_diagnostic(
|
|
|
|
url,
|
|
|
|
specifier,
|
|
|
|
text_info,
|
|
|
|
range,
|
|
|
|
diagnostic_reporter,
|
|
|
|
) {
|
2024-02-27 10:13:16 -05:00
|
|
|
text_changes.push(deno_ast::TextChange {
|
2024-06-05 11:04:16 -04:00
|
|
|
range: to_range(text_info, range),
|
2024-02-27 10:13:16 -05:00
|
|
|
new_text: unfurled,
|
|
|
|
});
|
2023-11-23 18:38:07 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
for dep in &module_info.dependencies {
|
2023-12-01 15:12:10 -05:00
|
|
|
match dep {
|
|
|
|
DependencyDescriptor::Static(dep) => {
|
|
|
|
analyze_specifier(
|
|
|
|
&dep.specifier,
|
|
|
|
&dep.specifier_range,
|
|
|
|
&mut text_changes,
|
2024-11-29 18:54:26 -05:00
|
|
|
diagnostic_reporter,
|
2023-12-01 15:12:10 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
DependencyDescriptor::Dynamic(dep) => {
|
2024-11-29 18:54:26 -05:00
|
|
|
let success = self.try_unfurl_dynamic_dep(
|
|
|
|
url,
|
|
|
|
text_info,
|
|
|
|
dep,
|
|
|
|
&mut text_changes,
|
|
|
|
diagnostic_reporter,
|
|
|
|
);
|
2023-12-01 15:12:10 -05:00
|
|
|
|
|
|
|
if !success {
|
2024-06-05 11:04:16 -04:00
|
|
|
let start_pos = text_info.line_start(dep.argument_range.start.line)
|
2024-02-21 14:12:36 -05:00
|
|
|
+ dep.argument_range.start.character;
|
2024-06-05 11:04:16 -04:00
|
|
|
let end_pos = text_info.line_start(dep.argument_range.end.line)
|
2024-02-21 14:12:36 -05:00
|
|
|
+ dep.argument_range.end.character;
|
2024-01-24 08:49:33 -05:00
|
|
|
diagnostic_reporter(
|
2024-02-27 10:13:16 -05:00
|
|
|
SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport {
|
2024-01-24 08:49:33 -05:00
|
|
|
specifier: url.to_owned(),
|
|
|
|
range: SourceRange::new(start_pos, end_pos),
|
2024-06-05 11:04:16 -04:00
|
|
|
text_info: text_info.clone(),
|
2024-01-24 08:49:33 -05:00
|
|
|
},
|
2023-12-01 15:12:10 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-23 18:38:07 -05:00
|
|
|
}
|
|
|
|
for ts_ref in &module_info.ts_references {
|
|
|
|
let specifier_with_range = match ts_ref {
|
2024-11-26 14:38:24 -05:00
|
|
|
TypeScriptReference::Path(s) => s,
|
|
|
|
TypeScriptReference::Types { specifier, .. } => specifier,
|
2023-11-23 18:38:07 -05:00
|
|
|
};
|
|
|
|
analyze_specifier(
|
|
|
|
&specifier_with_range.text,
|
|
|
|
&specifier_with_range.range,
|
2023-12-01 15:12:10 -05:00
|
|
|
&mut text_changes,
|
2024-11-29 18:54:26 -05:00
|
|
|
diagnostic_reporter,
|
2023-11-23 18:38:07 -05:00
|
|
|
);
|
|
|
|
}
|
2024-11-26 14:38:24 -05:00
|
|
|
for jsdoc in &module_info.jsdoc_imports {
|
2023-11-23 18:38:07 -05:00
|
|
|
analyze_specifier(
|
2024-11-26 14:38:24 -05:00
|
|
|
&jsdoc.specifier.text,
|
|
|
|
&jsdoc.specifier.range,
|
2023-12-01 15:12:10 -05:00
|
|
|
&mut text_changes,
|
2024-11-29 18:54:26 -05:00
|
|
|
diagnostic_reporter,
|
2023-11-23 18:38:07 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
if let Some(specifier_with_range) = &module_info.jsx_import_source {
|
|
|
|
analyze_specifier(
|
|
|
|
&specifier_with_range.text,
|
|
|
|
&specifier_with_range.range,
|
2023-12-01 15:12:10 -05:00
|
|
|
&mut text_changes,
|
2024-11-29 18:54:26 -05:00
|
|
|
diagnostic_reporter,
|
2023-11-23 18:38:07 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-05 11:04:16 -04:00
|
|
|
let rewritten_text =
|
|
|
|
deno_ast::apply_text_changes(text_info.text_str(), text_changes);
|
2024-01-24 08:49:33 -05:00
|
|
|
rewritten_text
|
2023-11-23 18:38:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 10:13:16 -05:00
|
|
|
fn relative_url(
|
|
|
|
resolved: &ModuleSpecifier,
|
|
|
|
referrer: &ModuleSpecifier,
|
2024-02-29 11:34:07 -05:00
|
|
|
) -> String {
|
|
|
|
if resolved.scheme() == "file" {
|
2024-03-07 11:29:17 -05:00
|
|
|
let relative = referrer.make_relative(resolved).unwrap();
|
|
|
|
if relative.is_empty() {
|
|
|
|
let last = resolved.path_segments().unwrap().last().unwrap();
|
|
|
|
format!("./{last}")
|
|
|
|
} else if relative.starts_with("../") {
|
|
|
|
relative
|
|
|
|
} else {
|
|
|
|
format!("./{relative}")
|
|
|
|
}
|
2023-12-01 15:12:10 -05:00
|
|
|
} else {
|
2024-02-27 10:13:16 -05:00
|
|
|
resolved.to_string()
|
2023-12-01 15:12:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-23 18:38:07 -05:00
|
|
|
fn to_range(
|
2024-06-05 11:04:16 -04:00
|
|
|
text_info: &SourceTextInfo,
|
2023-11-23 18:38:07 -05:00
|
|
|
range: &deno_graph::PositionRange,
|
|
|
|
) -> std::ops::Range<usize> {
|
|
|
|
let mut range = range
|
2024-06-05 11:04:16 -04:00
|
|
|
.as_source_range(text_info)
|
|
|
|
.as_byte_range(text_info.range().start);
|
|
|
|
let text = &text_info.text_str()[range.clone()];
|
2023-11-23 18:38:07 -05:00
|
|
|
if text.starts_with('"') || text.starts_with('\'') {
|
|
|
|
range.start += 1;
|
|
|
|
}
|
|
|
|
if text.ends_with('"') || text.ends_with('\'') {
|
|
|
|
range.end -= 1;
|
|
|
|
}
|
|
|
|
range
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-02-23 09:56:49 -05:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-09-28 19:17:48 -04:00
|
|
|
use crate::resolver::SloppyImportsCachedFs;
|
|
|
|
|
2023-11-23 18:38:07 -05:00
|
|
|
use super::*;
|
2024-01-23 06:40:23 -05:00
|
|
|
use deno_ast::MediaType;
|
2023-11-23 18:38:07 -05:00
|
|
|
use deno_ast::ModuleSpecifier;
|
2024-08-07 03:43:05 -04:00
|
|
|
use deno_config::workspace::ResolverWorkspaceJsrPackage;
|
2023-11-23 18:38:07 -05:00
|
|
|
use deno_core::serde_json::json;
|
2024-01-23 06:40:23 -05:00
|
|
|
use deno_core::url::Url;
|
2024-02-27 10:13:16 -05:00
|
|
|
use deno_runtime::deno_fs::RealFs;
|
|
|
|
use deno_runtime::deno_node::PackageJson;
|
2024-08-07 03:43:05 -04:00
|
|
|
use deno_semver::Version;
|
2023-11-23 18:38:07 -05:00
|
|
|
use import_map::ImportMapWithDiagnostics;
|
2024-08-07 03:43:05 -04:00
|
|
|
use indexmap::IndexMap;
|
2023-11-23 18:38:07 -05:00
|
|
|
use pretty_assertions::assert_eq;
|
2024-02-27 10:13:16 -05:00
|
|
|
use test_util::testdata_path;
|
2023-11-23 18:38:07 -05:00
|
|
|
|
2024-01-23 06:40:23 -05:00
|
|
|
fn parse_ast(specifier: &Url, source_code: &str) -> ParsedSource {
|
|
|
|
let media_type = MediaType::from_specifier(specifier);
|
|
|
|
deno_ast::parse_module(deno_ast::ParseParams {
|
2024-02-08 20:40:26 -05:00
|
|
|
specifier: specifier.clone(),
|
2024-01-23 06:40:23 -05:00
|
|
|
media_type,
|
|
|
|
capture_tokens: false,
|
|
|
|
maybe_syntax: None,
|
|
|
|
scope_analysis: false,
|
2024-06-05 11:04:16 -04:00
|
|
|
text: source_code.into(),
|
2024-01-23 06:40:23 -05:00
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
2023-11-23 18:38:07 -05:00
|
|
|
#[test]
|
|
|
|
fn test_unfurling() {
|
2024-02-27 10:13:16 -05:00
|
|
|
let cwd = testdata_path().join("unfurl").to_path_buf();
|
|
|
|
|
2023-11-23 18:38:07 -05:00
|
|
|
let deno_json_url =
|
2024-02-27 10:13:16 -05:00
|
|
|
ModuleSpecifier::from_file_path(cwd.join("deno.json")).unwrap();
|
2023-11-23 18:38:07 -05:00
|
|
|
let value = json!({
|
|
|
|
"imports": {
|
|
|
|
"express": "npm:express@5",
|
|
|
|
"lib/": "./lib/",
|
2024-02-27 10:13:16 -05:00
|
|
|
"fizz": "./fizz/mod.ts",
|
|
|
|
"@std/fs": "npm:@jsr/std__fs@1",
|
2023-11-23 18:38:07 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
let ImportMapWithDiagnostics { import_map, .. } =
|
2024-02-24 00:21:09 -05:00
|
|
|
import_map::parse_from_value(deno_json_url, value).unwrap();
|
2024-06-26 17:24:10 -04:00
|
|
|
let package_json = PackageJson::load_from_value(
|
|
|
|
cwd.join("package.json"),
|
|
|
|
json!({
|
|
|
|
"dependencies": {
|
|
|
|
"chalk": 5
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
2024-07-03 20:54:33 -04:00
|
|
|
let workspace_resolver = WorkspaceResolver::new_raw(
|
2024-07-17 09:13:22 -04:00
|
|
|
Arc::new(ModuleSpecifier::from_directory_path(&cwd).unwrap()),
|
2024-07-03 20:54:33 -04:00
|
|
|
Some(import_map),
|
2024-08-07 03:43:05 -04:00
|
|
|
vec![ResolverWorkspaceJsrPackage {
|
2024-08-20 10:11:43 -04:00
|
|
|
is_patch: false,
|
2024-08-07 03:43:05 -04:00
|
|
|
base: ModuleSpecifier::from_directory_path(cwd.join("jsr-package"))
|
|
|
|
.unwrap(),
|
|
|
|
name: "@denotest/example".to_string(),
|
|
|
|
version: Some(Version::parse_standard("1.0.0").unwrap()),
|
|
|
|
exports: IndexMap::from([(".".to_string(), "mod.ts".to_string())]),
|
|
|
|
}],
|
2024-07-03 20:54:33 -04:00
|
|
|
vec![Arc::new(package_json)],
|
|
|
|
deno_config::workspace::PackageJsonDepResolution::Enabled,
|
2024-02-27 10:13:16 -05:00
|
|
|
);
|
|
|
|
let fs = Arc::new(RealFs);
|
|
|
|
let unfurler = SpecifierUnfurler::new(
|
2024-11-29 18:54:26 -05:00
|
|
|
Some(Arc::new(CliSloppyImportsResolver::new(
|
|
|
|
SloppyImportsCachedFs::new(fs),
|
2024-09-28 19:17:48 -04:00
|
|
|
))),
|
2024-11-29 18:54:26 -05:00
|
|
|
Arc::new(workspace_resolver),
|
2024-02-27 10:13:16 -05:00
|
|
|
true,
|
2024-02-23 09:56:49 -05:00
|
|
|
);
|
2023-11-23 18:38:07 -05:00
|
|
|
|
|
|
|
// Unfurling TS file should apply changes.
|
|
|
|
{
|
|
|
|
let source_code = r#"import express from "express";"
|
|
|
|
import foo from "lib/foo.ts";
|
|
|
|
import bar from "lib/bar.ts";
|
|
|
|
import fizz from "fizz";
|
2024-02-27 10:13:16 -05:00
|
|
|
import chalk from "chalk";
|
|
|
|
import baz from "./baz";
|
|
|
|
import b from "./b.js";
|
|
|
|
import b2 from "./b";
|
2024-03-07 11:29:17 -05:00
|
|
|
import "./mod.ts";
|
2024-02-27 10:13:16 -05:00
|
|
|
import url from "url";
|
2024-08-07 03:43:05 -04:00
|
|
|
import "@denotest/example";
|
2024-02-27 10:13:16 -05:00
|
|
|
// TODO: unfurl these to jsr
|
|
|
|
// import "npm:@jsr/std__fs@1/file";
|
|
|
|
// import "npm:@jsr/std__fs@1";
|
|
|
|
// import "npm:@jsr/std__fs";
|
|
|
|
// import "@std/fs";
|
2023-12-01 15:12:10 -05:00
|
|
|
|
|
|
|
const test1 = await import("lib/foo.ts");
|
|
|
|
const test2 = await import(`lib/foo.ts`);
|
|
|
|
const test3 = await import(`lib/${expr}`);
|
|
|
|
const test4 = await import(`./lib/${expr}`);
|
2024-02-29 11:34:07 -05:00
|
|
|
const test5 = await import("./lib/something.ts");
|
|
|
|
const test6 = await import(`./lib/something.ts`);
|
2023-12-01 15:12:10 -05:00
|
|
|
// will warn
|
2024-02-29 11:34:07 -05:00
|
|
|
const warn1 = await import(`lib${expr}`);
|
|
|
|
const warn2 = await import(`${expr}`);
|
2023-11-23 18:38:07 -05:00
|
|
|
"#;
|
2024-02-27 10:13:16 -05:00
|
|
|
let specifier =
|
|
|
|
ModuleSpecifier::from_file_path(cwd.join("mod.ts")).unwrap();
|
2024-01-23 06:40:23 -05:00
|
|
|
let source = parse_ast(&specifier, source_code);
|
2024-01-24 08:49:33 -05:00
|
|
|
let mut d = Vec::new();
|
|
|
|
let mut reporter = |diagnostic| d.push(diagnostic);
|
|
|
|
let unfurled_source = unfurler.unfurl(&specifier, &source, &mut reporter);
|
2023-12-12 09:45:45 -05:00
|
|
|
assert_eq!(d.len(), 2);
|
2024-01-24 08:49:33 -05:00
|
|
|
assert!(
|
|
|
|
matches!(
|
|
|
|
d[0],
|
2024-02-27 10:13:16 -05:00
|
|
|
SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport { .. }
|
2024-01-24 08:49:33 -05:00
|
|
|
),
|
|
|
|
"{:?}",
|
|
|
|
d[0]
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
matches!(
|
|
|
|
d[1],
|
2024-02-27 10:13:16 -05:00
|
|
|
SpecifierUnfurlerDiagnostic::UnanalyzableDynamicImport { .. }
|
2024-01-24 08:49:33 -05:00
|
|
|
),
|
|
|
|
"{:?}",
|
|
|
|
d[1]
|
|
|
|
);
|
2023-11-23 18:38:07 -05:00
|
|
|
let expected_source = r#"import express from "npm:express@5";"
|
|
|
|
import foo from "./lib/foo.ts";
|
|
|
|
import bar from "./lib/bar.ts";
|
|
|
|
import fizz from "./fizz/mod.ts";
|
2024-02-27 10:13:16 -05:00
|
|
|
import chalk from "npm:chalk@5";
|
|
|
|
import baz from "./baz/index.js";
|
|
|
|
import b from "./b.ts";
|
|
|
|
import b2 from "./b.ts";
|
2024-03-07 11:29:17 -05:00
|
|
|
import "./mod.ts";
|
2024-02-27 10:13:16 -05:00
|
|
|
import url from "node:url";
|
2024-08-07 03:43:05 -04:00
|
|
|
import "jsr:@denotest/example@^1.0.0";
|
2024-02-27 10:13:16 -05:00
|
|
|
// TODO: unfurl these to jsr
|
|
|
|
// import "npm:@jsr/std__fs@1/file";
|
|
|
|
// import "npm:@jsr/std__fs@1";
|
|
|
|
// import "npm:@jsr/std__fs";
|
|
|
|
// import "@std/fs";
|
2023-12-01 15:12:10 -05:00
|
|
|
|
|
|
|
const test1 = await import("./lib/foo.ts");
|
|
|
|
const test2 = await import(`./lib/foo.ts`);
|
|
|
|
const test3 = await import(`./lib/${expr}`);
|
|
|
|
const test4 = await import(`./lib/${expr}`);
|
2024-02-29 11:34:07 -05:00
|
|
|
const test5 = await import("./lib/something.ts");
|
|
|
|
const test6 = await import(`./lib/something.ts`);
|
2023-12-01 15:12:10 -05:00
|
|
|
// will warn
|
2024-02-29 11:34:07 -05:00
|
|
|
const warn1 = await import(`lib${expr}`);
|
|
|
|
const warn2 = await import(`${expr}`);
|
2023-11-23 18:38:07 -05:00
|
|
|
"#;
|
|
|
|
assert_eq!(unfurled_source, expected_source);
|
|
|
|
}
|
|
|
|
}
|
2024-11-29 18:54:26 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unfurling_npm_dep_workspace_specifier() {
|
|
|
|
let cwd = testdata_path().join("unfurl").to_path_buf();
|
|
|
|
|
|
|
|
let pkg_json_add = PackageJson::load_from_value(
|
|
|
|
cwd.join("add/package.json"),
|
|
|
|
json!({ "name": "add", "version": "0.1.0", }),
|
|
|
|
);
|
|
|
|
let pkg_json_subtract = PackageJson::load_from_value(
|
|
|
|
cwd.join("subtract/package.json"),
|
|
|
|
json!({ "name": "subtract", "version": "0.2.0", }),
|
|
|
|
);
|
|
|
|
let pkg_json_publishing = PackageJson::load_from_value(
|
|
|
|
cwd.join("publish/package.json"),
|
|
|
|
json!({
|
|
|
|
"name": "@denotest/main",
|
|
|
|
"version": "1.0.0",
|
|
|
|
"dependencies": {
|
|
|
|
"add": "workspace:~",
|
|
|
|
"subtract": "workspace:^",
|
|
|
|
"non-existent": "workspace:~",
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
let root_pkg_json = PackageJson::load_from_value(
|
|
|
|
cwd.join("package.json"),
|
|
|
|
json!({ "workspaces": ["./publish", "./subtract", "./add"] }),
|
|
|
|
);
|
|
|
|
let workspace_resolver = WorkspaceResolver::new_raw(
|
|
|
|
Arc::new(ModuleSpecifier::from_directory_path(&cwd).unwrap()),
|
|
|
|
None,
|
|
|
|
vec![ResolverWorkspaceJsrPackage {
|
|
|
|
is_patch: false,
|
|
|
|
base: ModuleSpecifier::from_directory_path(
|
|
|
|
cwd.join("publish/jsr.json"),
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
name: "@denotest/main".to_string(),
|
|
|
|
version: Some(Version::parse_standard("1.0.0").unwrap()),
|
|
|
|
exports: IndexMap::from([(".".to_string(), "mod.ts".to_string())]),
|
|
|
|
}],
|
|
|
|
vec![
|
|
|
|
Arc::new(root_pkg_json),
|
|
|
|
Arc::new(pkg_json_add),
|
|
|
|
Arc::new(pkg_json_subtract),
|
|
|
|
Arc::new(pkg_json_publishing),
|
|
|
|
],
|
|
|
|
deno_config::workspace::PackageJsonDepResolution::Enabled,
|
|
|
|
);
|
|
|
|
let fs = Arc::new(RealFs);
|
|
|
|
let unfurler = SpecifierUnfurler::new(
|
|
|
|
Some(Arc::new(CliSloppyImportsResolver::new(
|
|
|
|
SloppyImportsCachedFs::new(fs),
|
|
|
|
))),
|
|
|
|
Arc::new(workspace_resolver),
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
|
|
|
{
|
|
|
|
let source_code = r#"import add from "add";
|
|
|
|
import subtract from "subtract";
|
|
|
|
|
|
|
|
console.log(add, subtract);
|
|
|
|
"#;
|
|
|
|
let specifier =
|
|
|
|
ModuleSpecifier::from_file_path(cwd.join("publish").join("mod.ts"))
|
|
|
|
.unwrap();
|
|
|
|
let source = parse_ast(&specifier, source_code);
|
|
|
|
let mut d = Vec::new();
|
|
|
|
let mut reporter = |diagnostic| d.push(diagnostic);
|
|
|
|
let unfurled_source = unfurler.unfurl(&specifier, &source, &mut reporter);
|
|
|
|
assert_eq!(d.len(), 0);
|
|
|
|
// it will inline the version
|
|
|
|
let expected_source = r#"import add from "npm:add@~0.1.0";
|
|
|
|
import subtract from "npm:subtract@^0.2.0";
|
|
|
|
|
|
|
|
console.log(add, subtract);
|
|
|
|
"#;
|
|
|
|
assert_eq!(unfurled_source, expected_source);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let source_code = r#"import nonExistent from "non-existent";
|
|
|
|
console.log(nonExistent);
|
|
|
|
"#;
|
|
|
|
let specifier =
|
|
|
|
ModuleSpecifier::from_file_path(cwd.join("publish").join("other.ts"))
|
|
|
|
.unwrap();
|
|
|
|
let source = parse_ast(&specifier, source_code);
|
|
|
|
let mut d = Vec::new();
|
|
|
|
let mut reporter = |diagnostic| d.push(diagnostic);
|
|
|
|
let unfurled_source = unfurler.unfurl(&specifier, &source, &mut reporter);
|
|
|
|
assert_eq!(d.len(), 1);
|
|
|
|
match &d[0] {
|
|
|
|
SpecifierUnfurlerDiagnostic::ResolvingNpmWorkspacePackage {
|
|
|
|
package_name,
|
|
|
|
reason,
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
assert_eq!(package_name, "non-existent");
|
|
|
|
assert_eq!(reason, "unable to find npm package in workspace");
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
// won't make any changes, but the above will be a fatal error
|
|
|
|
assert!(matches!(d[0].level(), DiagnosticLevel::Error));
|
|
|
|
assert_eq!(unfurled_source, source_code);
|
|
|
|
}
|
|
|
|
}
|
2023-11-23 18:38:07 -05:00
|
|
|
}
|