mirror of
https://github.com/denoland/deno.git
synced 2025-02-12 16:59:32 -05:00
fix(publish): unfurl sloppy imports in d.ts files and type imports (#27793)
Fixes a bug discovered while trying to publish a package with .js + .d.ts with sloppy imports.
This commit is contained in:
parent
ab18dac09d
commit
c38dbe500b
3 changed files with 61 additions and 2 deletions
|
@ -23,6 +23,7 @@ use deno_core::ModuleSpecifier;
|
||||||
use deno_graph::DependencyDescriptor;
|
use deno_graph::DependencyDescriptor;
|
||||||
use deno_graph::DynamicTemplatePart;
|
use deno_graph::DynamicTemplatePart;
|
||||||
use deno_graph::ParserModuleAnalyzer;
|
use deno_graph::ParserModuleAnalyzer;
|
||||||
|
use deno_graph::StaticDependencyKind;
|
||||||
use deno_graph::TypeScriptReference;
|
use deno_graph::TypeScriptReference;
|
||||||
use deno_package_json::PackageJsonDepValue;
|
use deno_package_json::PackageJsonDepValue;
|
||||||
use deno_package_json::PackageJsonDepWorkspaceReq;
|
use deno_package_json::PackageJsonDepWorkspaceReq;
|
||||||
|
@ -214,11 +215,12 @@ impl SpecifierUnfurler {
|
||||||
&self,
|
&self,
|
||||||
referrer: &ModuleSpecifier,
|
referrer: &ModuleSpecifier,
|
||||||
specifier: &str,
|
specifier: &str,
|
||||||
|
resolution_kind: SloppyImportsResolutionKind,
|
||||||
text_info: &SourceTextInfo,
|
text_info: &SourceTextInfo,
|
||||||
range: &deno_graph::PositionRange,
|
range: &deno_graph::PositionRange,
|
||||||
diagnostic_reporter: &mut dyn FnMut(SpecifierUnfurlerDiagnostic),
|
diagnostic_reporter: &mut dyn FnMut(SpecifierUnfurlerDiagnostic),
|
||||||
) -> Option<String> {
|
) -> Option<String> {
|
||||||
match self.unfurl_specifier(referrer, specifier) {
|
match self.unfurl_specifier(referrer, specifier, resolution_kind) {
|
||||||
Ok(maybe_unfurled) => maybe_unfurled,
|
Ok(maybe_unfurled) => maybe_unfurled,
|
||||||
Err(diagnostic) => match diagnostic {
|
Err(diagnostic) => match diagnostic {
|
||||||
UnfurlSpecifierError::Workspace {
|
UnfurlSpecifierError::Workspace {
|
||||||
|
@ -248,6 +250,7 @@ impl SpecifierUnfurler {
|
||||||
&self,
|
&self,
|
||||||
referrer: &ModuleSpecifier,
|
referrer: &ModuleSpecifier,
|
||||||
specifier: &str,
|
specifier: &str,
|
||||||
|
resolution_kind: SloppyImportsResolutionKind,
|
||||||
) -> Result<Option<String>, UnfurlSpecifierError> {
|
) -> Result<Option<String>, UnfurlSpecifierError> {
|
||||||
let resolved = if let Ok(resolved) =
|
let resolved = if let Ok(resolved) =
|
||||||
self.workspace_resolver.resolve(specifier, referrer)
|
self.workspace_resolver.resolve(specifier, referrer)
|
||||||
|
@ -395,7 +398,7 @@ impl SpecifierUnfurler {
|
||||||
let resolved =
|
let resolved =
|
||||||
if let Some(sloppy_imports_resolver) = &self.sloppy_imports_resolver {
|
if let Some(sloppy_imports_resolver) = &self.sloppy_imports_resolver {
|
||||||
sloppy_imports_resolver
|
sloppy_imports_resolver
|
||||||
.resolve(&resolved, SloppyImportsResolutionKind::Execution)
|
.resolve(&resolved, resolution_kind)
|
||||||
.map(|res| res.into_specifier())
|
.map(|res| res.into_specifier())
|
||||||
.unwrap_or(resolved)
|
.unwrap_or(resolved)
|
||||||
} else {
|
} else {
|
||||||
|
@ -458,6 +461,7 @@ impl SpecifierUnfurler {
|
||||||
let maybe_unfurled = self.unfurl_specifier_reporting_diagnostic(
|
let maybe_unfurled = self.unfurl_specifier_reporting_diagnostic(
|
||||||
module_url,
|
module_url,
|
||||||
specifier,
|
specifier,
|
||||||
|
SloppyImportsResolutionKind::Execution, // dynamic imports are always execution
|
||||||
text_info,
|
text_info,
|
||||||
&dep.argument_range,
|
&dep.argument_range,
|
||||||
diagnostic_reporter,
|
diagnostic_reporter,
|
||||||
|
@ -485,6 +489,7 @@ impl SpecifierUnfurler {
|
||||||
let unfurled = self.unfurl_specifier_reporting_diagnostic(
|
let unfurled = self.unfurl_specifier_reporting_diagnostic(
|
||||||
module_url,
|
module_url,
|
||||||
specifier,
|
specifier,
|
||||||
|
SloppyImportsResolutionKind::Execution, // dynamic imports are always execution
|
||||||
text_info,
|
text_info,
|
||||||
&dep.argument_range,
|
&dep.argument_range,
|
||||||
diagnostic_reporter,
|
diagnostic_reporter,
|
||||||
|
@ -530,11 +535,13 @@ impl SpecifierUnfurler {
|
||||||
let analyze_specifier =
|
let analyze_specifier =
|
||||||
|specifier: &str,
|
|specifier: &str,
|
||||||
range: &deno_graph::PositionRange,
|
range: &deno_graph::PositionRange,
|
||||||
|
resolution_kind: SloppyImportsResolutionKind,
|
||||||
text_changes: &mut Vec<deno_ast::TextChange>,
|
text_changes: &mut Vec<deno_ast::TextChange>,
|
||||||
diagnostic_reporter: &mut dyn FnMut(SpecifierUnfurlerDiagnostic)| {
|
diagnostic_reporter: &mut dyn FnMut(SpecifierUnfurlerDiagnostic)| {
|
||||||
if let Some(unfurled) = self.unfurl_specifier_reporting_diagnostic(
|
if let Some(unfurled) = self.unfurl_specifier_reporting_diagnostic(
|
||||||
url,
|
url,
|
||||||
specifier,
|
specifier,
|
||||||
|
resolution_kind,
|
||||||
text_info,
|
text_info,
|
||||||
range,
|
range,
|
||||||
diagnostic_reporter,
|
diagnostic_reporter,
|
||||||
|
@ -548,9 +555,27 @@ impl SpecifierUnfurler {
|
||||||
for dep in &module_info.dependencies {
|
for dep in &module_info.dependencies {
|
||||||
match dep {
|
match dep {
|
||||||
DependencyDescriptor::Static(dep) => {
|
DependencyDescriptor::Static(dep) => {
|
||||||
|
let resolution_kind = if parsed_source.media_type().is_declaration() {
|
||||||
|
SloppyImportsResolutionKind::Types
|
||||||
|
} else {
|
||||||
|
match dep.kind {
|
||||||
|
StaticDependencyKind::Export
|
||||||
|
| StaticDependencyKind::Import
|
||||||
|
| StaticDependencyKind::ExportEquals
|
||||||
|
| StaticDependencyKind::ImportEquals => {
|
||||||
|
SloppyImportsResolutionKind::Execution
|
||||||
|
}
|
||||||
|
StaticDependencyKind::ExportType
|
||||||
|
| StaticDependencyKind::ImportType => {
|
||||||
|
SloppyImportsResolutionKind::Types
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
analyze_specifier(
|
analyze_specifier(
|
||||||
&dep.specifier,
|
&dep.specifier,
|
||||||
&dep.specifier_range,
|
&dep.specifier_range,
|
||||||
|
resolution_kind,
|
||||||
&mut text_changes,
|
&mut text_changes,
|
||||||
diagnostic_reporter,
|
diagnostic_reporter,
|
||||||
);
|
);
|
||||||
|
@ -588,6 +613,7 @@ impl SpecifierUnfurler {
|
||||||
analyze_specifier(
|
analyze_specifier(
|
||||||
&specifier_with_range.text,
|
&specifier_with_range.text,
|
||||||
&specifier_with_range.range,
|
&specifier_with_range.range,
|
||||||
|
SloppyImportsResolutionKind::Types,
|
||||||
&mut text_changes,
|
&mut text_changes,
|
||||||
diagnostic_reporter,
|
diagnostic_reporter,
|
||||||
);
|
);
|
||||||
|
@ -596,6 +622,7 @@ impl SpecifierUnfurler {
|
||||||
analyze_specifier(
|
analyze_specifier(
|
||||||
&jsdoc.specifier.text,
|
&jsdoc.specifier.text,
|
||||||
&jsdoc.specifier.range,
|
&jsdoc.specifier.range,
|
||||||
|
SloppyImportsResolutionKind::Types,
|
||||||
&mut text_changes,
|
&mut text_changes,
|
||||||
diagnostic_reporter,
|
diagnostic_reporter,
|
||||||
);
|
);
|
||||||
|
@ -604,6 +631,16 @@ impl SpecifierUnfurler {
|
||||||
analyze_specifier(
|
analyze_specifier(
|
||||||
&specifier_with_range.text,
|
&specifier_with_range.text,
|
||||||
&specifier_with_range.range,
|
&specifier_with_range.range,
|
||||||
|
SloppyImportsResolutionKind::Execution,
|
||||||
|
&mut text_changes,
|
||||||
|
diagnostic_reporter,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if let Some(specifier_with_range) = &module_info.jsx_import_source_types {
|
||||||
|
analyze_specifier(
|
||||||
|
&specifier_with_range.text,
|
||||||
|
&specifier_with_range.range,
|
||||||
|
SloppyImportsResolutionKind::Types,
|
||||||
&mut text_changes,
|
&mut text_changes,
|
||||||
diagnostic_reporter,
|
diagnostic_reporter,
|
||||||
);
|
);
|
||||||
|
@ -741,6 +778,8 @@ import baz from "./baz";
|
||||||
import b from "./b.js";
|
import b from "./b.js";
|
||||||
import b2 from "./b";
|
import b2 from "./b";
|
||||||
import "./mod.ts";
|
import "./mod.ts";
|
||||||
|
import { } from "./c";
|
||||||
|
import type { } from "./c";
|
||||||
import url from "url";
|
import url from "url";
|
||||||
import "@denotest/example";
|
import "@denotest/example";
|
||||||
// TODO: unfurl these to jsr
|
// TODO: unfurl these to jsr
|
||||||
|
@ -791,6 +830,8 @@ import baz from "./baz/index.js";
|
||||||
import b from "./b.ts";
|
import b from "./b.ts";
|
||||||
import b2 from "./b.ts";
|
import b2 from "./b.ts";
|
||||||
import "./mod.ts";
|
import "./mod.ts";
|
||||||
|
import { } from "./c.js";
|
||||||
|
import type { } from "./c.d.ts";
|
||||||
import url from "node:url";
|
import url from "node:url";
|
||||||
import "jsr:@denotest/example@^1.0.0";
|
import "jsr:@denotest/example@^1.0.0";
|
||||||
// TODO: unfurl these to jsr
|
// TODO: unfurl these to jsr
|
||||||
|
@ -808,6 +849,24 @@ const test6 = await import(`./lib/something.ts`);
|
||||||
// will warn
|
// will warn
|
||||||
const warn1 = await import(`lib${expr}`);
|
const warn1 = await import(`lib${expr}`);
|
||||||
const warn2 = await import(`${expr}`);
|
const warn2 = await import(`${expr}`);
|
||||||
|
"#;
|
||||||
|
assert_eq!(unfurled_source, expected_source);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unfurling .d.ts file should use types resolution.
|
||||||
|
{
|
||||||
|
let source_code = r#"import express from "express";"
|
||||||
|
export type * from "./c";
|
||||||
|
"#;
|
||||||
|
let specifier =
|
||||||
|
ModuleSpecifier::from_file_path(cwd.join("mod.d.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);
|
||||||
|
let expected_source = r#"import express from "npm:express@5";"
|
||||||
|
export type * from "./c.d.ts";
|
||||||
"#;
|
"#;
|
||||||
assert_eq!(unfurled_source, expected_source);
|
assert_eq!(unfurled_source, expected_source);
|
||||||
}
|
}
|
||||||
|
|
0
tests/testdata/unfurl/c.d.ts
vendored
Normal file
0
tests/testdata/unfurl/c.d.ts
vendored
Normal file
0
tests/testdata/unfurl/c.js
vendored
Normal file
0
tests/testdata/unfurl/c.js
vendored
Normal file
Loading…
Add table
Reference in a new issue