Skip to content

Commit 1beef28

Browse files
committed
feat(types): add JSDoc source links to generated type definitions
Add @see JSDoc comments to generated .d.ts files that link to the exact line in the original Rust/Zig source file where each function is defined. This improves IDE navigation by allowing developers to jump directly to the source implementation from TypeScript definitions. - Capture line numbers during Tree-sitter parsing - Generate JSDoc @see tags with file:// URLs pointing to source:line - Update FunctionType interface to include optional line number - Works with both Rust and Zig loaders
1 parent b34af16 commit 1beef28

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

src/loader.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,18 @@ export default class {
6161
if (types && Object.keys(types).length > 0) {
6262
for (const [symbol, type] of Object.entries(types)) {
6363
const args = type.args.join(", ");
64+
if (type.line) {
65+
// Convert Windows backslashes to forward slashes for proper file:// URL
66+
const filePath = this.config.importPath.replace(/\\/g, '/');
67+
configWriter.write(`\n\t\t/** Source: file:///${filePath}:${type.line} */`);
68+
}
6469
configWriter.write(`\n\t\t${symbol}: {\n\t\t\targs: [${args}],\n\t\t\treturns: ${type.returns}\n\t\t},`);
6570
}
6671
}
6772
configWriter.write(`\n\t}\n} satisfies LoaderConfig.Main;`);
6873
await configWriter.end();
74+
75+
// Generate types.d.ts - simplified since JSDoc is in config.ts
6976
await Bun.write(
7077
`${this.cwd}/@types/${filename}/types.d.ts`,
7178
`declare module "*/${filename}" {\n\tconst symbols: import("bun:ffi").ConvertFns<typeof import("./config.ts").default.symbols>;\n\texport = symbols;\n}`

src/loaders/rs/parse-types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ async function getParser(): Promise<Parser> {
5555
export interface FunctionType {
5656
args: string[];
5757
returns: string;
58+
line?: number;
5859
}
5960

6061
// Parse Rust source code to extract FFI function signatures
@@ -156,7 +157,11 @@ export async function parseRustTypes(
156157
}
157158
}
158159

159-
types[fnName] = { args, returns: returnType };
160+
types[fnName] = {
161+
args,
162+
returns: returnType,
163+
line: fnNameNode.startPosition.row + 1
164+
};
160165
}
161166

162167
return types;

src/loaders/zig/parse-types.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ async function getParser(): Promise<Parser> {
5757
export interface FunctionType {
5858
args: string[];
5959
returns: string;
60+
line?: number;
6061
}
6162

6263
// Parse Zig source code to extract FFI function signatures
@@ -161,9 +162,12 @@ export async function parseZigTypes(
161162
}
162163
}
163164

164-
types[fnName] = { args, returns: returnType };
165+
types[fnName] = {
166+
args,
167+
returns: returnType,
168+
line: fnNameNode.startPosition.row + 1
169+
};
165170
}
166171

167172
return types;
168173
}
169-

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export namespace LoaderConfig {
2121
extension: string,
2222
buildCommand?: (importPath: string, outDir: string) => string[],
2323
outDir?: (importPath: string) => string,
24-
parseTypes?: (importPath: string) => Promise<Record<string, { args: string[], returns: string }> | undefined>,
24+
parseTypes?: (importPath: string) => Promise<Record<string, { args: string[], returns: string, line?: number }> | undefined>,
2525
}
2626

2727
export interface Internal {

0 commit comments

Comments
 (0)