fix(napi-derive): js_name
support for getters and setters (#1179)
This commit is contained in:
parent
822f4af1cc
commit
d6c755c4eb
6 changed files with 43 additions and 16 deletions
|
@ -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 {
|
||||
|
|
|
@ -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␊
|
||||
|
|
Binary file not shown.
|
@ -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)
|
||||
})
|
||||
|
|
2
examples/napi/index.d.ts
vendored
2
examples/napi/index.d.ts
vendored
|
@ -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
|
||||
|
|
|
@ -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 🚀
|
||||
|
|
Loading…
Reference in a new issue