diff --git a/crates/macro/src/parser/mod.rs b/crates/macro/src/parser/mod.rs index 2f898072..aae59dc9 100644 --- a/crates/macro/src/parser/mod.rs +++ b/crates/macro/src/parser/mod.rs @@ -540,23 +540,33 @@ fn napi_fn_from_decl( Diagnostic::from_vec(errors).map(|_| { let js_name = if let Some(prop_name) = opts.getter() { - if let Some(ident) = prop_name { - ident.to_string() - } else { - ident - .to_string() - .trim_start_matches("get_") - .to_case(Case::Camel) - } + opts.js_name().map_or_else( + || { + if let Some(ident) = prop_name { + ident.to_string() + } else { + ident + .to_string() + .trim_start_matches("get_") + .to_case(Case::Camel) + } + }, + |(js_name, _)| js_name.to_owned(), + ) } else if let Some(prop_name) = opts.setter() { - if let Some(ident) = prop_name { - ident.to_string() - } else { - ident - .to_string() - .trim_start_matches("set_") - .to_case(Case::Camel) - } + opts.js_name().map_or_else( + || { + if let Some(ident) = prop_name { + ident.to_string() + } else { + ident + .to_string() + .trim_start_matches("set_") + .to_case(Case::Camel) + } + }, + |(js_name, _)| js_name.to_owned(), + ) } else if opts.constructor().is_some() { "constructor".to_owned() } else { diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index d22d65b1..8b46b46a 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -191,6 +191,8 @@ Generated by [AVA](https://avajs.dev). static withKind(kind: Kind): Animal␊ get name(): string␊ set name(name: string)␊ + get type(): Kind␊ + set type(kind: Kind)␊ /**␊ * This is a␊ * multi-line comment␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 6c9e2049..ca1b89b0 100644 Binary files a/examples/napi/__test__/typegen.spec.ts.snap and b/examples/napi/__test__/typegen.spec.ts.snap differ diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index f1ad6054..64f38521 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -158,6 +158,9 @@ test('class', (t) => { t.deepEqual(dog.returnOtherClass(), new Dog('Doge')) t.deepEqual(dog.returnOtherClassWithCustomConstructor(), new Bird('parrot')) t.is(dog.returnOtherClassWithCustomConstructor().getCount(), 1234) + t.is(dog.type, Kind.Dog) + dog.type = Kind.Cat + t.is(dog.type, Kind.Cat) const assets = new Assets() t.is(assets.get(1)?.filePath, 1) }) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index d3adb048..cd564af2 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -181,6 +181,8 @@ export class Animal { static withKind(kind: Kind): Animal get name(): string set name(name: string) + get type(): Kind + set type(kind: Kind) /** * This is a * multi-line comment diff --git a/examples/napi/src/class.rs b/examples/napi/src/class.rs index 8d1a2a98..63405e02 100644 --- a/examples/napi/src/class.rs +++ b/examples/napi/src/class.rs @@ -41,6 +41,16 @@ impl Animal { self.name = name; } + #[napi(getter, js_name = "type")] + pub fn kind(&self) -> Kind { + self.kind + } + + #[napi(setter, js_name = "type")] + pub fn set_kind(&mut self, kind: Kind) { + self.kind = kind; + } + /// This is a /// multi-line comment /// with an emoji 🚀