Skip to content
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

build(bindgen): check for corresponding .zig file #15896

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions src/codegen/bindgen-lib-internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// always produce correct code, or bail with an error.
import { expect } from "bun:test";
import type { FuncOptions, Type, t } from "./bindgen-lib";
import fs from "node:fs";
import * as path from "node:path";
import assert from "node:assert";

Expand Down Expand Up @@ -806,6 +807,12 @@ export function registerFunction(opts: FuncOptions) {
const filename = stackTraceFileName(snapshot);
expect(filename).toEndWith(".bind.ts");
const zigFile = path.relative(src, filename.replace(/\.bind\.ts$/, ".zig"));
if (!fs.existsSync(zigFile)) {
const bindName = path.basename(filename);
throw new Error(
`$[bindName] is missing a corresponding Zig file at ${zigFile}. Please create it and make sure it matches signatures in ${bindName}.`,
);
}
let file = files.get(zigFile);
if (!file) {
file = { functions: [], typedefs: [] };
Expand Down
25 changes: 23 additions & 2 deletions src/codegen/bindgen-lib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// This is the public API for `bind.ts` files
// It is aliased as `import {} from 'bindgen'`
/**
* This is the public API for `bind.ts` files
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does jsdoc make this a comment for the file or the next declared symbol (Type)? i could be totally wrong and this form is correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for //? no. It should make one for /** */ but I'm not seeing it. Maybe there's a problem with the tsconfig?

* It is aliased as `import {} from 'bindgen'`
* @see https://bun.sh/docs/project/bindgen
*/

import {
isType,
dictionaryImpl,
Expand Down Expand Up @@ -196,6 +200,23 @@ export namespace t {
export const ByteString = builtinType<string>()("ByteString");
/**
* DOMString but encoded as `[]const u8`
*
* @example
* ```ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think simple examples like these aren't worth their line count, especially when my laptop screen can only fit 70 lines of code at once. this is why i try to write extremely concise comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes it appear nicely when hovering over the symbol.
image

If this was ts-only, VSCode would still perform syntax highlighting.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah the hover doesn't appear in screenshots :(

* // foo.bind.ts
* import { fn, t } from "bindgen";
*
* export const foo = fn({
* args: { bar: t.UTF8String },
* })
* ```
*
* ```zig
* // foo.zig
* pub fn foo(bar: []const u8) void {
* // ...
* }
* ```
*/
export const UTF8String = builtinType<string>()("UTF8String");

Expand Down
Loading