summaryrefslogtreecommitdiff
path: root/cli/util/extract.rs
diff options
context:
space:
mode:
authorhaturau <135221985+haturatu@users.noreply.github.com>2024-11-20 01:20:47 +0900
committerGitHub <noreply@github.com>2024-11-20 01:20:47 +0900
commit85719a67e59c7aa45bead26e4942d7df8b1b42d4 (patch)
treeface0aecaac53e93ce2f23b53c48859bcf1a36ec /cli/util/extract.rs
parent67697bc2e4a62a9670699fd18ad0dd8efc5bd955 (diff)
parent186b52731c6bb326c4d32905c5e732d082e83465 (diff)
Merge branch 'denoland:main' into main
Diffstat (limited to 'cli/util/extract.rs')
-rw-r--r--cli/util/extract.rs127
1 files changed, 121 insertions, 6 deletions
diff --git a/cli/util/extract.rs b/cli/util/extract.rs
index 841cf6eb0..be68202aa 100644
--- a/cli/util/extract.rs
+++ b/cli/util/extract.rs
@@ -64,7 +64,7 @@ fn extract_inner(
}) {
Ok(parsed) => {
let mut c = ExportCollector::default();
- c.visit_program(parsed.program_ref());
+ c.visit_program(parsed.program().as_ref());
c
}
Err(_) => ExportCollector::default(),
@@ -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,
@@ -566,14 +570,14 @@ fn generate_pseudo_file(
})?;
let top_level_atoms = swc_utils::collect_decls_with_ctxt::<Atom, _>(
- parsed.program_ref(),
+ &parsed.program_ref(),
parsed.top_level_context(),
);
let transformed =
parsed
.program_ref()
- .clone()
+ .to_owned()
.fold_with(&mut as_folder(Transform {
specifier: &file.specifier,
base_file_specifier,
@@ -582,7 +586,10 @@ fn generate_pseudo_file(
wrap_kind,
}));
- let source = deno_ast::swc::codegen::to_code(&transformed);
+ let source = deno_ast::swc::codegen::to_code_with_comments(
+ Some(&parsed.comments().as_single_threaded()),
+ &transformed,
+ );
log::debug!("{}:\n{}", file.specifier, source);
@@ -1137,6 +1144,57 @@ 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,
+ }],
+ },
+ // https://github.com/denoland/deno/issues/26728
+ Test {
+ input: Input {
+ source: r#"
+/**
+ * ```ts
+ * // @ts-expect-error: can only add numbers
+ * add('1', '2');
+ * ```
+ */
+export function add(first: number, second: number) {
+ return first + second;
+}
+"#,
+ specifier: "file:///main.ts",
+ },
+ expected: vec![Expected {
+ source: r#"import { add } from "file:///main.ts";
+Deno.test("file:///main.ts$3-7.ts", async ()=>{
+ // @ts-expect-error: can only add numbers
+ add('1', '2');
+});
+"#,
+ specifier: "file:///main.ts$3-7.ts",
+ media_type: MediaType::TypeScript,
+ }],
+ },
];
for test in tests {
@@ -1326,6 +1384,53 @@ 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,
+ }],
+ },
+ // https://github.com/denoland/deno/issues/26728
+ Test {
+ input: Input {
+ source: r#"
+/**
+ * ```ts
+ * // @ts-expect-error: can only add numbers
+ * add('1', '2');
+ * ```
+ */
+export function add(first: number, second: number) {
+ return first + second;
+}
+"#,
+ specifier: "file:///main.ts",
+ },
+ expected: vec![Expected {
+ source: r#"import { add } from "file:///main.ts";
+// @ts-expect-error: can only add numbers
+add('1', '2');
+"#,
+ specifier: "file:///main.ts$3-7.ts",
+ media_type: MediaType::TypeScript,
+ }],
+ },
];
for test in tests {
@@ -1366,7 +1471,7 @@ assertEquals(add(1, 2), 3);
})
.unwrap();
- collector.visit_program(parsed.program_ref());
+ parsed.program_ref().visit_with(&mut collector);
collector
}
@@ -1581,6 +1686,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 {