fix(napi-derive): js_name support for getters and setters (#1179)

This commit is contained in:
Devon Govett 2022-05-13 00:55:54 -04:00 committed by GitHub
parent 822f4af1cc
commit d6c755c4eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 16 deletions

View file

@ -540,6 +540,8 @@ fn napi_fn_from_decl(
Diagnostic::from_vec(errors).map(|_| {
let js_name = if let Some(prop_name) = opts.getter() {
opts.js_name().map_or_else(
|| {
if let Some(ident) = prop_name {
ident.to_string()
} else {
@ -548,7 +550,12 @@ fn napi_fn_from_decl(
.trim_start_matches("get_")
.to_case(Case::Camel)
}
},
|(js_name, _)| js_name.to_owned(),
)
} else if let Some(prop_name) = opts.setter() {
opts.js_name().map_or_else(
|| {
if let Some(ident) = prop_name {
ident.to_string()
} else {
@ -557,6 +564,9 @@ fn napi_fn_from_decl(
.trim_start_matches("set_")
.to_case(Case::Camel)
}
},
|(js_name, _)| js_name.to_owned(),
)
} else if opts.constructor().is_some() {
"constructor".to_owned()
} else {

View file

@ -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␊

View file

@ -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)
})

View file

@ -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

View file

@ -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 🚀