Skip to content

fix: don't remove namespaced imports if they are external #112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/ripe-apes-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'dts-buddy': patch
---

fix: don't remove namespaced imports if they are external
3 changes: 2 additions & 1 deletion src/create-module-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions test/samples/import-external-all-as/input/external.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module 'all-as-external' {
export type BaseType<T> = {
type: T;
};

export function string(): BaseType<string>;

export type Infer<T extends BaseType<any>> = T extends BaseType<infer U> ? U : never;
}
7 changes: 7 additions & 0 deletions test/samples/import-external-all-as/input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as v from 'all-as-external';

export const Schema = v.string();

/**
* @typedef {v.Infer<typeof Schema>} Test
*/
9 changes: 9 additions & 0 deletions test/samples/import-external-all-as/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module 'import-external-all-as' {
import * as v from 'all-as-external';
export const Schema: v.BaseType<string>;
export type Test = v.Infer<typeof Schema>;

export {};
}

//# sourceMappingURL=index.d.ts.map
15 changes: 15 additions & 0 deletions test/samples/import-external-all-as/output/index.d.ts.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": 3,
"file": "index.d.ts",
"names": [
"Schema",
"Test"
],
"sources": [
"../input/index.js"
],
"sourcesContent": [
null
],
"mappings": ";;cAEaA,MAAMA;aAGkBC,IAAIA"
}
Loading