From eaa54f69186c2f8ff5c7a5779d02b091967a6b3a Mon Sep 17 00:00:00 2001 From: Jeremiasz Major Date: Tue, 17 Jun 2025 13:21:32 +0200 Subject: [PATCH 1/2] add failing test --- test/samples/import-external-all/input/index.ts | 3 +++ .../import-external-all/node_modules/zod/package.json | 4 ++++ .../import-external-all/node_modules/zod/types.d.ts | 1 + test/samples/import-external-all/output/index.d.ts | 7 +++++++ test/samples/import-external-all/output/index.d.ts.map | 8 ++++++++ 5 files changed, 23 insertions(+) create mode 100644 test/samples/import-external-all/input/index.ts create mode 100644 test/samples/import-external-all/node_modules/zod/package.json create mode 100644 test/samples/import-external-all/node_modules/zod/types.d.ts create mode 100644 test/samples/import-external-all/output/index.d.ts create mode 100644 test/samples/import-external-all/output/index.d.ts.map diff --git a/test/samples/import-external-all/input/index.ts b/test/samples/import-external-all/input/index.ts new file mode 100644 index 0000000..29774e4 --- /dev/null +++ b/test/samples/import-external-all/input/index.ts @@ -0,0 +1,3 @@ +import * as z from 'zod'; + +export type Params = z.infer; diff --git a/test/samples/import-external-all/node_modules/zod/package.json b/test/samples/import-external-all/node_modules/zod/package.json new file mode 100644 index 0000000..0cb30ed --- /dev/null +++ b/test/samples/import-external-all/node_modules/zod/package.json @@ -0,0 +1,4 @@ +{ + "type": "module", + "types": "types.d.ts" +} diff --git a/test/samples/import-external-all/node_modules/zod/types.d.ts b/test/samples/import-external-all/node_modules/zod/types.d.ts new file mode 100644 index 0000000..385a20e --- /dev/null +++ b/test/samples/import-external-all/node_modules/zod/types.d.ts @@ -0,0 +1 @@ +export type infer = T; diff --git a/test/samples/import-external-all/output/index.d.ts b/test/samples/import-external-all/output/index.d.ts new file mode 100644 index 0000000..e1ff1e5 --- /dev/null +++ b/test/samples/import-external-all/output/index.d.ts @@ -0,0 +1,7 @@ +declare module 'import-external-all' { + import * as z from 'zod'; + + export type Params = z.infer; +} + +//# sourceMappingURL=index.d.ts.map diff --git a/test/samples/import-external-all/output/index.d.ts.map b/test/samples/import-external-all/output/index.d.ts.map new file mode 100644 index 0000000..2fd1ead --- /dev/null +++ b/test/samples/import-external-all/output/index.d.ts.map @@ -0,0 +1,8 @@ +{ + "version": 3, + "file": "index.d.ts", + "names": [], + "sources": [], + "sourcesContent": [], + "mappings": "" +} From 7c0b7eee8ac49799f25226cdfca01ea1d8dfaa5f Mon Sep 17 00:00:00 2001 From: Jeremiasz Major Date: Tue, 17 Jun 2025 20:51:38 +0200 Subject: [PATCH 2/2] handle external wildcard imports --- .changeset/eight-geese-argue.md | 5 +++++ src/create-module-declaration.js | 2 +- src/utils.js | 8 +++++--- test/samples/import-external-all/output/index.d.ts | 3 ++- .../import-external-all/output/index.d.ts.map | 14 ++++++++++---- 5 files changed, 23 insertions(+), 9 deletions(-) create mode 100644 .changeset/eight-geese-argue.md diff --git a/.changeset/eight-geese-argue.md b/.changeset/eight-geese-argue.md new file mode 100644 index 0000000..b8451b8 --- /dev/null +++ b/.changeset/eight-geese-argue.md @@ -0,0 +1,5 @@ +--- +'dts-buddy': patch +--- + +fix: handle external wildcard imports diff --git a/src/create-module-declaration.js b/src/create-module-declaration.js index 8cbefe7..b0abd63 100644 --- a/src/create-module-declaration.js +++ b/src/create-module-declaration.js @@ -246,7 +246,7 @@ export function create_module_declaration(id, entry, created, resolve, options) walk(module.ast, (node) => { if (is_reference(node) && ts.isQualifiedName(node.parent)) { const binding = module.import_all.get(node.getText(module.ast)); - if (binding) { + if (binding && !binding.external) { result.remove(node.pos, result.original.indexOf('.', node.end) + 1); const declaration = bundle .get(binding.id) diff --git a/src/utils.js b/src/utils.js index 17f85e2..0378da1 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,4 +1,4 @@ -/** @import { Binding, Declaration, Module, Namespace } from './types' */ +/** @import { Declaration, Module, Namespace } from './types' */ import fs from 'node:fs'; import path from 'node:path'; import { globSync } from 'tinyglobby'; @@ -447,10 +447,12 @@ export function get_dts(file, created, resolve, options) { current.references.add(name); if (name !== declaration.name) { + const import_all = module.import_all.get(name); + // If this references an import * as X statement, we add a dependency to Y of the X.Y access - if (module.import_all.has(name) && ts.isQualifiedName(node.parent)) { + if (import_all && !import_all.external && ts.isQualifiedName(node.parent)) { declaration.dependencies.push({ - module: /** @type {Binding} */ (module.import_all.get(name)).id, + module: import_all.id, name: node.parent.right.getText(module.ast) }); } else { diff --git a/test/samples/import-external-all/output/index.d.ts b/test/samples/import-external-all/output/index.d.ts index e1ff1e5..cfc8c11 100644 --- a/test/samples/import-external-all/output/index.d.ts +++ b/test/samples/import-external-all/output/index.d.ts @@ -1,7 +1,8 @@ declare module 'import-external-all' { import * as z from 'zod'; - export type Params = z.infer; + + export {}; } //# sourceMappingURL=index.d.ts.map diff --git a/test/samples/import-external-all/output/index.d.ts.map b/test/samples/import-external-all/output/index.d.ts.map index 2fd1ead..92d276c 100644 --- a/test/samples/import-external-all/output/index.d.ts.map +++ b/test/samples/import-external-all/output/index.d.ts.map @@ -1,8 +1,14 @@ { "version": 3, "file": "index.d.ts", - "names": [], - "sources": [], - "sourcesContent": [], - "mappings": "" + "names": [ + "Params" + ], + "sources": [ + "../input/index.ts" + ], + "sourcesContent": [ + null + ], + "mappings": ";;aAEYA,MAAMA" }