summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYusuke Tanaka <yusuktan@maguro.dev>2024-10-15 11:21:47 +0900
committerGitHub <noreply@github.com>2024-10-14 19:21:47 -0700
commit9f0a447f7cbcdf1e38cc0d52a5da70a50ea0179b (patch)
tree6db0756826626d71090cb5232fee0aae5ab44e21
parent1a0cb5b5312941521ab021cfe9eaed498f35900b (diff)
fix(cli): named export takes precedence over default export in doc testing (#26112)
This commit fixes the issue of import name conflict in case the named export and default export have the same name by letting named export take precedence over default export. Fixes #26009
-rw-r--r--cli/util/extract.rs62
1 files changed, 61 insertions, 1 deletions
diff --git a/cli/util/extract.rs b/cli/util/extract.rs
index 841cf6eb0..873b7e7f2 100644
--- a/cli/util/extract.rs
+++ b/cli/util/extract.rs
@@ -254,7 +254,11 @@ impl ExportCollector {
let mut import_specifiers = vec![];
if let Some(default_export) = &self.default_export {
- if !symbols_to_exclude.contains(default_export) {
+ // If the default export conflicts with a named export, a named one
+ // takes precedence.
+ if !symbols_to_exclude.contains(default_export)
+ && !self.named_exports.contains(default_export)
+ {
import_specifiers.push(ast::ImportSpecifier::Default(
ast::ImportDefaultSpecifier {
span: DUMMY_SP,
@@ -1137,6 +1141,30 @@ Deno.test("file:///README.md$6-12.js", async ()=>{
media_type: MediaType::JavaScript,
}],
},
+ // https://github.com/denoland/deno/issues/26009
+ Test {
+ input: Input {
+ source: r#"
+/**
+ * ```ts
+ * console.log(Foo)
+ * ```
+ */
+export class Foo {}
+export default Foo
+"#,
+ specifier: "file:///main.ts",
+ },
+ expected: vec![Expected {
+ source: r#"import { Foo } from "file:///main.ts";
+Deno.test("file:///main.ts$3-6.ts", async ()=>{
+ console.log(Foo);
+});
+"#,
+ specifier: "file:///main.ts$3-6.ts",
+ media_type: MediaType::TypeScript,
+ }],
+ },
];
for test in tests {
@@ -1326,6 +1354,28 @@ assertEquals(add(1, 2), 3);
media_type: MediaType::JavaScript,
}],
},
+ // https://github.com/denoland/deno/issues/26009
+ Test {
+ input: Input {
+ source: r#"
+/**
+ * ```ts
+ * console.log(Foo)
+ * ```
+ */
+export class Foo {}
+export default Foo
+"#,
+ specifier: "file:///main.ts",
+ },
+ expected: vec![Expected {
+ source: r#"import { Foo } from "file:///main.ts";
+console.log(Foo);
+"#,
+ specifier: "file:///main.ts$3-6.ts",
+ media_type: MediaType::TypeScript,
+ }],
+ },
];
for test in tests {
@@ -1581,6 +1631,16 @@ declare global {
named_expected: atom_set!(),
default_expected: None,
},
+ // The identifier `Foo` conflicts, but `ExportCollector` doesn't do
+ // anything about it. It is handled by `to_import_specifiers` method.
+ Test {
+ input: r#"
+export class Foo {}
+export default Foo
+"#,
+ named_expected: atom_set!("Foo"),
+ default_expected: Some("Foo".into()),
+ },
];
for test in tests {