Repository navigation
fix(napi-derive): add paren for function types - #3044
Conversation
WalkthroughThis PR adds support for optional callback types in NAPI bindings. It modifies type generation logic to handle Option for struct fields differently, implements a new Rust function accepting optional callbacks, and exports this functionality across multiple JavaScript module systems with TypeScript declarations. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
examples/napi/__tests__/values.spec.ts (1)
294-295: Tests exercise presence and basic calling ofoptionalCallbackTypesThe import and
t.notThrowschecks validate that the function is exported and callable both with and without a callback, which is a good minimal smoke test.You might optionally strengthen this by asserting the callback is actually invoked with the expected
"Hello"argument (e.g., via a spy), but it's not strictly necessary for this fix.Also applies to: 536-539
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
examples/napi/__tests__/__snapshots__/values.spec.ts.snapis excluded by!**/*.snap
📒 Files selected for processing (8)
crates/backend/src/typegen.rs(1 hunks)examples/napi/__tests__/__snapshots__/values.spec.ts.md(1 hunks)examples/napi/__tests__/values.spec.ts(2 hunks)examples/napi/example.wasi-browser.js(1 hunks)examples/napi/example.wasi.cjs(1 hunks)examples/napi/index.cjs(1 hunks)examples/napi/index.d.cts(1 hunks)examples/napi/src/function.rs(2 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: CR
Repo: napi-rs/napi-rs PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-25T09:31:23.877Z
Learning: Applies to /crates/**/*.rs : Use `#[napi]` attribute on impl blocks to define classes exported to JavaScript/TypeScript
Learnt from: CR
Repo: napi-rs/napi-rs PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-25T09:31:23.877Z
Learning: Applies to /crates/**/*.rs : Use `#[napi(object)]` attribute for plain objects that should be mapped to TypeScript interfaces
Learnt from: CR
Repo: napi-rs/napi-rs PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-25T09:31:23.877Z
Learning: Applies to /crates/**/*.rs : Use `#[napi(js_name = "...")]` attribute to rename Rust items in JavaScript/TypeScript
📚 Learning: 2025-11-25T09:31:23.877Z
Learnt from: CR
Repo: napi-rs/napi-rs PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-11-25T09:31:23.877Z
Learning: Applies to /crates/**/*.rs : Use `#[napi]` attribute on impl blocks to define classes exported to JavaScript/TypeScript
Applied to files:
examples/napi/src/function.rsexamples/napi/example.wasi.cjs
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (39)
- GitHub Check: Build only test - riscv64gc-unknown-linux-gnu
- GitHub Check: Build only test - loongarch64-unknown-linux-gnu
- GitHub Check: Build only test - armv7-linux-androideabi
- GitHub Check: Test node wasi target (--cfg tokio_unstable)
- GitHub Check: Build only test - aarch64-unknown-linux-ohos
- GitHub Check: x86_64-unknown-linux-gnu - node@24 - toolchain@ 1.88.0
- GitHub Check: Test node wasi target
- GitHub Check: stable - armv7-unknown-linux-gnueabihf - node@22
- GitHub Check: Build only test - aarch64-linux-android
- GitHub Check: i686-pc-windows-msvc - node@24 - toolchain@ stable
- GitHub Check: build - x86_64-unknown-linux-gnu
- GitHub Check: aarch64-apple-darwin - node@24 - toolchain@ stable
- GitHub Check: x86_64-pc-windows-msvc - node@24 - toolchain@ stable
- GitHub Check: build - aarch64-unknown-linux-gnu
- GitHub Check: build - s390x-unknown-linux-gnu
- GitHub Check: build - powerpc64le-unknown-linux-gnu
- GitHub Check: aarch64-pc-windows-msvc - node@24 - toolchain@ stable
- GitHub Check: i686-pc-windows-msvc - node@22 - toolchain@ stable
- GitHub Check: x86_64-unknown-linux-gnu - node@22 - toolchain@ 1.88.0
- GitHub Check: x86_64-unknown-linux-gnu - node@24 - toolchain@ stable
- GitHub Check: x86_64-pc-windows-msvc - node@22 - toolchain@ stable
- GitHub Check: aarch64-apple-darwin - node@22 - toolchain@ stable
- GitHub Check: aarch64-pc-windows-msvc - node@22 - toolchain@ stable
- GitHub Check: x86_64-pc-windows-msvc - node@20 - toolchain@ stable
- GitHub Check: x86_64-unknown-linux-gnu - node@22 - toolchain@ stable
- GitHub Check: aarch64-apple-darwin - node@20 - toolchain@ stable
- GitHub Check: x86_64-unknown-linux-gnu - node@20 - toolchain@ stable
- GitHub Check: build - x86_64-unknown-linux-musl
- GitHub Check: build - aarch64-unknown-linux-musl
- GitHub Check: Test freebsd target
- GitHub Check: build-and-test-msys2 (UCRT64)
- GitHub Check: build-and-test-msys2 (CLANG64)
- GitHub Check: build-and-test-msys2 (MINGW64)
- GitHub Check: ASAN - windows-latest
- GitHub Check: ASAN - ubuntu-24.04
- GitHub Check: Zig-Cross-Compile-On-Linux (aarch64-unknown-linux-musl)
- GitHub Check: Memory leak detect job
- GitHub Check: Zig-Cross-Compile-On-Linux (armv7-unknown-linux-musleabihf)
- GitHub Check: Zig-Cross-Compile-On-Linux (aarch64-apple-darwin)
🔇 Additional comments (8)
examples/napi/src/function.rs (2)
3-5: ImportingUnknownReturnValueis appropriateThe added import matches its use in
optional_callback_typesand theKNOWN_TYPESmapping; no issues.
152-160:optional_callback_typesimplementation looks correctThe API cleanly accepts an optional JS callback, calls it synchronously with
"Hello"when present, and propagates JS errors via?. UsingFunction<String, UnknownReturnValue>aligns with theindex.d.ctsdeclaration and keeps the return unconstrained.Based on learnings, using
#[napi]here correctly exposes this function to JS/TS.crates/backend/src/typegen.rs (1)
476-500: Verify Option struct-field semantics and function-type parens
- For non-struct uses, wrapping
argin({arg})when it contains=>correctly fixesOption<fn>into((...) => ...) | undefined | nullinstead of a union on the return type.- For struct fields,
Option<T>now yields(arg.to_string(), true)directly, so TS becomesfield?: Tinstead offield?: T | undefined | null. This is more idiomatic but slightly changes generated typings for all structOption<T>fields.Please confirm that this changed struct-field behavior (dropping explicit
| undefined | null) is intentional and covered by snapshots, and thatarg.contains("=>")is sufficient for all function-shaped Option arguments you care about.examples/napi/example.wasi-browser.js (1)
300-304: WASI browser export wiring is consistent
optionalCallbackTypesis exported in the same pattern as neighboring bindings; wiring looks correct.examples/napi/index.cjs (1)
809-813: CJS export foroptionalCallbackTypesis correctly wiredThe new property mirrors existing exports and forwards directly to the native binding; no issues.
examples/napi/index.d.cts (1)
891-899:optionalCallbackTypesdeclaration matches Rust & typegen logicThe declared type
callback?: ((arg: string) => unknown) | undefined | nullcorrectly reflectsOption<Function<String, UnknownReturnValue>>with the fixed parentheses around the function type and avoidreturn forResult<()>.examples/napi/example.wasi.cjs (1)
345-349: WASI CJS export foroptionalCallbackTypesis consistentThe new
module.exports.optionalCallbackTypesbinding follows the established export pattern; looks good.examples/napi/__tests__/__snapshots__/values.spec.ts.md (1)
931-939: Snapshot update correctly reflects new API surfaceThe added snapshot entry for
optionalCallbackTypes(callback?: ((arg: string) => unknown) | undefined | null): voidmatches the declaration inindex.d.ctsand will catch future regressions in Option type rendering.

Summary by CodeRabbit
optionalCallbackTypes()function supporting optional callback parameters that can be undefined or null.✏️ Tip: You can customize this high-level summary in your review settings.