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/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..cfc8c11 --- /dev/null +++ b/test/samples/import-external-all/output/index.d.ts @@ -0,0 +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 new file mode 100644 index 0000000..92d276c --- /dev/null +++ b/test/samples/import-external-all/output/index.d.ts.map @@ -0,0 +1,14 @@ +{ + "version": 3, + "file": "index.d.ts", + "names": [ + "Params" + ], + "sources": [ + "../input/index.ts" + ], + "sourcesContent": [ + null + ], + "mappings": ";;aAEYA,MAAMA" +}