Skip to content

fix: handle external wildcard imports #107

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 2 commits 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/eight-geese-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'dts-buddy': patch
---

fix: handle external wildcard imports
2 changes: 1 addition & 1 deletion src/create-module-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions test/samples/import-external-all/input/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as z from 'zod';

export type Params = z.infer<string>;

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test/samples/import-external-all/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
declare module 'import-external-all' {
import * as z from 'zod';
export type Params = z.infer<string>;

export {};
}

//# sourceMappingURL=index.d.ts.map
14 changes: 14 additions & 0 deletions test/samples/import-external-all/output/index.d.ts.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"version": 3,
"file": "index.d.ts",
"names": [
"Params"
],
"sources": [
"../input/index.ts"
],
"sourcesContent": [
null
],
"mappings": ";;aAEYA,MAAMA"
}