fix(napi-derive): using js_name generating wrong type defs#2700
Merged
Conversation
This commit introduces two main changes:
1. Refactors the variable names used during the creation of
`NapiStruct` in `crates/macro/src/parser/mod.rs`. The goal is to
make the source of `NapiStruct.name` (the Rust identifier) and
`NapiStruct.js_name` (the JavaScript name, potentially from a
`#[napi(js_name = "...")]` attribute) more explicit. This change
is primarily for code readability and maintainability. The core
logic remains the same.
2. Adds a new test case to `examples/napi` to specifically verify
the behavior of `#[napi(js_name = "...")]` on structs. This
includes:
- A new Rust struct `OriginalRustNameForJsNamedStruct` in
`examples/napi/src/class.rs` decorated with
`#[napi(js_name = "MyJsNamedClass")]`.
- Corresponding tests in `examples/napi/__tests__/values.spec.ts`
that check for correct class instantiation, method calls, and
type alias generation (`OriginalRustNameForJsNamedStruct` as an
alias for `MyJsNamedClass`).
Note: I encountered some issues running the full build and tests for the example due to pre-commit hooks in the testing environment that I couldn't bypass. However, I believe the changes are correct based on static analysis and focused checks.
30484a8 to
824d7c5
Compare
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the assignment of JavaScript names to Rust structs in the NAPI binding generator and adds new tests to verify the behavior of the js_name attribute.
- Renames variables for better clarity in NapiStruct construction in crates/macro/src/parser/mod.rs.
- Introduces two new structs in examples/napi/src/class.rs with the js_name attribute and updates related type definitions and module exports.
- Adds comprehensive tests in examples/napi/tests/values.spec.ts and updates snapshot and module declaration files accordingly.
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| examples/napi/src/class.rs | Added new struct definitions with js_name and corresponding methods. |
| examples/napi/index.d.cts | Updated type declarations and type aliases for new structs. |
| examples/napi/index.cjs, example.wasi.* | Updated module exports to include new structs. |
| examples/napi/tests/values.spec.ts | Added tests to verify js_name behavior and type compatibility. |
| crates/macro/src/parser/mod.rs | Refactored variable names related to js_name assignment in NapiStruct. |
Comments suppressed due to low confidence (1)
crates/macro/src/parser/mod.rs:1374
- [nitpick] Consider using a consistent casing function for default js name generation (e.g., using Case::Pascal instead of Case::UpperCamel) across all implementations for clarity, even if they are equivalent in this context.
let mut struct_js_name = struct_name.to_string().to_case(Case::UpperCamel);
Member
Author
|
bugbot run |
There was a problem hiding this comment.
✅ BugBot reviewed your changes and found no bugs!
BugBot free trial expires on June 14, 2025
You have used $0.00 of your $50.00 spend limit so far. Manage your spend limit in the Cursor dashboard.
Was this report helpful? Give feedback by reacting with 👍 or 👎
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

This commit introduces two main changes:
Refactors the variable names used during the creation of
NapiStructincrates/macro/src/parser/mod.rs. The goal is to make the source ofNapiStruct.name(the Rust identifier) andNapiStruct.js_name(the JavaScript name, potentially from a#[napi(js_name = "...")]attribute) more explicit. This change is primarily for code readability and maintainability. The core logic remains the same.Adds a new test case to
examples/napito specifically verify the behavior of#[napi(js_name = "...")]on structs. This includes:OriginalRustNameForJsNamedStructinexamples/napi/src/class.rsdecorated with#[napi(js_name = "MyJsNamedClass")].examples/napi/__tests__/values.spec.tsthat check for correct class instantiation, method calls, and type alias generation (OriginalRustNameForJsNamedStructas an alias forMyJsNamedClass).js_nameremoves type declarations from class #2475