diff --git a/.changeset/ripe-apes-sleep.md b/.changeset/ripe-apes-sleep.md new file mode 100644 index 0000000..ae5849a --- /dev/null +++ b/.changeset/ripe-apes-sleep.md @@ -0,0 +1,5 @@ +--- +'dts-buddy': patch +--- + +fix: don't remove namespaced imports if they are external diff --git a/src/create-module-declaration.js b/src/create-module-declaration.js index 8cbefe7..eabf2b4 100644 --- a/src/create-module-declaration.js +++ b/src/create-module-declaration.js @@ -243,10 +243,11 @@ export function create_module_declaration(id, entry, created, resolve, options) if (module.import_all.size > 0) { // remove the leading `Foo.` from references to `import * as Foo` namespace imports + // but only for internal imports, not external ones 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/test/samples/import-external-all-as/input/external.d.ts b/test/samples/import-external-all-as/input/external.d.ts new file mode 100644 index 0000000..1b550d2 --- /dev/null +++ b/test/samples/import-external-all-as/input/external.d.ts @@ -0,0 +1,9 @@ +declare module 'all-as-external' { + export type BaseType = { + type: T; + }; + + export function string(): BaseType; + + export type Infer> = T extends BaseType ? U : never; +} diff --git a/test/samples/import-external-all-as/input/index.js b/test/samples/import-external-all-as/input/index.js new file mode 100644 index 0000000..0265dd9 --- /dev/null +++ b/test/samples/import-external-all-as/input/index.js @@ -0,0 +1,7 @@ +import * as v from 'all-as-external'; + +export const Schema = v.string(); + +/** + * @typedef {v.Infer} Test + */ diff --git a/test/samples/import-external-all-as/output/index.d.ts b/test/samples/import-external-all-as/output/index.d.ts new file mode 100644 index 0000000..9406e13 --- /dev/null +++ b/test/samples/import-external-all-as/output/index.d.ts @@ -0,0 +1,9 @@ +declare module 'import-external-all-as' { + import * as v from 'all-as-external'; + export const Schema: v.BaseType; + export type Test = v.Infer; + + export {}; +} + +//# sourceMappingURL=index.d.ts.map \ No newline at end of file diff --git a/test/samples/import-external-all-as/output/index.d.ts.map b/test/samples/import-external-all-as/output/index.d.ts.map new file mode 100644 index 0000000..43c1004 --- /dev/null +++ b/test/samples/import-external-all-as/output/index.d.ts.map @@ -0,0 +1,15 @@ +{ + "version": 3, + "file": "index.d.ts", + "names": [ + "Schema", + "Test" + ], + "sources": [ + "../input/index.js" + ], + "sourcesContent": [ + null + ], + "mappings": ";;cAEaA,MAAMA;aAGkBC,IAAIA" +} \ No newline at end of file