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,6 +540,8 @@ fn napi_fn_from_decl(
|
||||||
|
|
||||||
Diagnostic::from_vec(errors).map(|_| {
|
Diagnostic::from_vec(errors).map(|_| {
|
||||||
let js_name = if let Some(prop_name) = opts.getter() {
|
let js_name = if let Some(prop_name) = opts.getter() {
|
||||||
|
opts.js_name().map_or_else(
|
||||||
|
|| {
|
||||||
if let Some(ident) = prop_name {
|
if let Some(ident) = prop_name {
|
||||||
ident.to_string()
|
ident.to_string()
|
||||||
} else {
|
} else {
|
||||||
|
@ -548,7 +550,12 @@ fn napi_fn_from_decl(
|
||||||
.trim_start_matches("get_")
|
.trim_start_matches("get_")
|
||||||
.to_case(Case::Camel)
|
.to_case(Case::Camel)
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|(js_name, _)| js_name.to_owned(),
|
||||||
|
)
|
||||||
} else if let Some(prop_name) = opts.setter() {
|
} else if let Some(prop_name) = opts.setter() {
|
||||||
|
opts.js_name().map_or_else(
|
||||||
|
|| {
|
||||||
if let Some(ident) = prop_name {
|
if let Some(ident) = prop_name {
|
||||||
ident.to_string()
|
ident.to_string()
|
||||||
} else {
|
} else {
|
||||||
|
@ -557,6 +564,9 @@ fn napi_fn_from_decl(
|
||||||
.trim_start_matches("set_")
|
.trim_start_matches("set_")
|
||||||
.to_case(Case::Camel)
|
.to_case(Case::Camel)
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|(js_name, _)| js_name.to_owned(),
|
||||||
|
)
|
||||||
} else if opts.constructor().is_some() {
|
} else if opts.constructor().is_some() {
|
||||||
"constructor".to_owned()
|
"constructor".to_owned()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -191,6 +191,8 @@ Generated by [AVA](https://avajs.dev).
|
||||||
static withKind(kind: Kind): Animal␊
|
static withKind(kind: Kind): Animal␊
|
||||||
get name(): string␊
|
get name(): string␊
|
||||||
set name(name: string)␊
|
set name(name: string)␊
|
||||||
|
get type(): Kind␊
|
||||||
|
set type(kind: Kind)␊
|
||||||
/**␊
|
/**␊
|
||||||
* This is a␊
|
* This is a␊
|
||||||
* multi-line comment␊
|
* multi-line comment␊
|
||||||
|
|
Binary file not shown.
|
@ -158,6 +158,9 @@ test('class', (t) => {
|
||||||
t.deepEqual(dog.returnOtherClass(), new Dog('Doge'))
|
t.deepEqual(dog.returnOtherClass(), new Dog('Doge'))
|
||||||
t.deepEqual(dog.returnOtherClassWithCustomConstructor(), new Bird('parrot'))
|
t.deepEqual(dog.returnOtherClassWithCustomConstructor(), new Bird('parrot'))
|
||||||
t.is(dog.returnOtherClassWithCustomConstructor().getCount(), 1234)
|
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()
|
const assets = new Assets()
|
||||||
t.is(assets.get(1)?.filePath, 1)
|
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
|
static withKind(kind: Kind): Animal
|
||||||
get name(): string
|
get name(): string
|
||||||
set name(name: string)
|
set name(name: string)
|
||||||
|
get type(): Kind
|
||||||
|
set type(kind: Kind)
|
||||||
/**
|
/**
|
||||||
* This is a
|
* This is a
|
||||||
* multi-line comment
|
* multi-line comment
|
||||||
|
|
|
@ -41,6 +41,16 @@ impl Animal {
|
||||||
self.name = name;
|
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
|
/// This is a
|
||||||
/// multi-line comment
|
/// multi-line comment
|
||||||
/// with an emoji 🚀
|
/// with an emoji 🚀
|
||||||
|
|
Loading…
Reference in a new issue