fix(napi-derive): type generate issue for Factory and Class
This commit is contained in:
parent
e78cdd3c22
commit
44040e3bfe
8 changed files with 53 additions and 3 deletions
|
@ -7,7 +7,7 @@ use std::collections::HashMap;
|
|||
use once_cell::sync::Lazy;
|
||||
use syn::Type;
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Debug)]
|
||||
pub struct TypeDef {
|
||||
pub kind: String,
|
||||
pub name: String,
|
||||
|
@ -149,6 +149,10 @@ pub fn ty_to_ts_type(ty: &Type, is_return_ty: bool) -> String {
|
|||
} else {
|
||||
ts_ty = Some(known_ty.to_owned());
|
||||
}
|
||||
} else if let Some(t) = crate::typegen::r#struct::CLASS_STRUCTS
|
||||
.with(|c| c.borrow_mut().get(rust_ty.as_str()).cloned())
|
||||
{
|
||||
ts_ty = Some(t);
|
||||
} else {
|
||||
// there should be runtime registered type in else
|
||||
ts_ty = Some(rust_ty);
|
||||
|
@ -157,7 +161,7 @@ pub fn ty_to_ts_type(ty: &Type, is_return_ty: bool) -> String {
|
|||
|
||||
ts_ty.unwrap_or_else(|| "any".to_owned())
|
||||
}
|
||||
|
||||
Type::Group(g) => ty_to_ts_type(&g.elem, is_return_ty),
|
||||
_ => "any".to_owned(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -87,6 +87,11 @@ impl NapiFn {
|
|||
fn gen_ts_func_ret(&self) -> String {
|
||||
match self.kind {
|
||||
FnKind::Constructor | FnKind::Setter => "".to_owned(),
|
||||
FnKind::Factory => self
|
||||
.parent
|
||||
.clone()
|
||||
.map(|i| format!(": {}", i.to_string().to_case(Case::Pascal)))
|
||||
.unwrap_or_else(|| "".to_owned()),
|
||||
_ => {
|
||||
let ret = if let Some(ret) = &self.ret {
|
||||
let ts_type = ty_to_ts_type(ret, true);
|
||||
|
|
|
@ -6,10 +6,15 @@ use crate::{ty_to_ts_type, NapiImpl, NapiStruct, NapiStructKind};
|
|||
|
||||
thread_local! {
|
||||
pub(crate) static TASK_STRUCTS: RefCell<HashMap<String, String>> = Default::default();
|
||||
pub(crate) static CLASS_STRUCTS: RefCell<HashMap<String, String>> = Default::default();
|
||||
}
|
||||
|
||||
impl ToTypeDef for NapiStruct {
|
||||
fn to_type_def(&self) -> TypeDef {
|
||||
CLASS_STRUCTS.with(|c| {
|
||||
c.borrow_mut()
|
||||
.insert(self.name.to_string(), self.js_name.clone());
|
||||
});
|
||||
TypeDef {
|
||||
kind: String::from(if self.kind == NapiStructKind::Object {
|
||||
"interface"
|
||||
|
|
|
@ -782,7 +782,7 @@ impl ConvertToAST for syn::ItemImpl {
|
|||
continue;
|
||||
}
|
||||
|
||||
if opts.constructor().is_some() {
|
||||
if opts.constructor().is_some() || opts.factory().is_some() {
|
||||
struct_js_name = check_recorded_struct_for_impl(&struct_name, &opts)?;
|
||||
}
|
||||
|
||||
|
|
|
@ -54,6 +54,13 @@ Generated by [AVA](https://avajs.dev).
|
|||
whoami(): string␊
|
||||
static getDogKind(): Kind␊
|
||||
}␊
|
||||
export class Blake2BHasher {␊
|
||||
␊
|
||||
static withKey(key: Blake2bKey): Blake2BHasher␊
|
||||
}␊
|
||||
export class Blake2BKey {␊
|
||||
␊
|
||||
}␊
|
||||
export class ClassWithFactory {␊
|
||||
name: string␊
|
||||
static withName(name: string): ClassWithFactory␊
|
||||
|
|
Binary file not shown.
7
examples/napi/index.d.ts
vendored
7
examples/napi/index.d.ts
vendored
|
@ -43,6 +43,13 @@ export class Animal {
|
|||
set name(name: string)
|
||||
whoami(): string
|
||||
static getDogKind(): Kind
|
||||
}
|
||||
export class Blake2BHasher {
|
||||
|
||||
static withKey(key: Blake2bKey): Blake2BHasher
|
||||
}
|
||||
export class Blake2BKey {
|
||||
|
||||
}
|
||||
export class ClassWithFactory {
|
||||
name: string
|
||||
|
|
|
@ -53,3 +53,25 @@ impl Animal {
|
|||
Kind::Dog
|
||||
}
|
||||
}
|
||||
|
||||
/// Smoking test for type generation
|
||||
#[napi]
|
||||
#[repr(transparent)]
|
||||
pub struct Blake2bHasher(u32);
|
||||
|
||||
#[napi]
|
||||
impl Blake2bHasher {
|
||||
#[napi(factory)]
|
||||
pub fn with_key(key: &Blake2bKey) -> Self {
|
||||
Blake2bHasher(key.get_inner())
|
||||
}
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub struct Blake2bKey(u32);
|
||||
|
||||
impl Blake2bKey {
|
||||
fn get_inner(&self) -> u32 {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue