fix(napi-derive): type generate issue for Factory and Class

This commit is contained in:
LongYinan 2021-11-06 11:53:53 +08:00
parent e78cdd3c22
commit 44040e3bfe
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
8 changed files with 53 additions and 3 deletions

View file

@ -7,7 +7,7 @@ use std::collections::HashMap;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use syn::Type; use syn::Type;
#[derive(Default)] #[derive(Default, Debug)]
pub struct TypeDef { pub struct TypeDef {
pub kind: String, pub kind: String,
pub name: String, pub name: String,
@ -149,6 +149,10 @@ pub fn ty_to_ts_type(ty: &Type, is_return_ty: bool) -> String {
} else { } else {
ts_ty = Some(known_ty.to_owned()); 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 { } else {
// there should be runtime registered type in else // there should be runtime registered type in else
ts_ty = Some(rust_ty); 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()) ts_ty.unwrap_or_else(|| "any".to_owned())
} }
Type::Group(g) => ty_to_ts_type(&g.elem, is_return_ty),
_ => "any".to_owned(), _ => "any".to_owned(),
} }
} }

View file

@ -87,6 +87,11 @@ impl NapiFn {
fn gen_ts_func_ret(&self) -> String { fn gen_ts_func_ret(&self) -> String {
match self.kind { match self.kind {
FnKind::Constructor | FnKind::Setter => "".to_owned(), 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 ret = if let Some(ret) = &self.ret {
let ts_type = ty_to_ts_type(ret, true); let ts_type = ty_to_ts_type(ret, true);

View file

@ -6,10 +6,15 @@ use crate::{ty_to_ts_type, NapiImpl, NapiStruct, NapiStructKind};
thread_local! { thread_local! {
pub(crate) static TASK_STRUCTS: RefCell<HashMap<String, String>> = Default::default(); 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 { impl ToTypeDef for NapiStruct {
fn to_type_def(&self) -> TypeDef { fn to_type_def(&self) -> TypeDef {
CLASS_STRUCTS.with(|c| {
c.borrow_mut()
.insert(self.name.to_string(), self.js_name.clone());
});
TypeDef { TypeDef {
kind: String::from(if self.kind == NapiStructKind::Object { kind: String::from(if self.kind == NapiStructKind::Object {
"interface" "interface"

View file

@ -782,7 +782,7 @@ impl ConvertToAST for syn::ItemImpl {
continue; 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)?; struct_js_name = check_recorded_struct_for_impl(&struct_name, &opts)?;
} }

View file

@ -54,6 +54,13 @@ Generated by [AVA](https://avajs.dev).
whoami(): string␊ whoami(): string␊
static getDogKind(): Kind␊ static getDogKind(): Kind␊
}␊ }␊
export class Blake2BHasher {␊
static withKey(key: Blake2bKey): Blake2BHasher␊
}␊
export class Blake2BKey {␊
}␊
export class ClassWithFactory {␊ export class ClassWithFactory {␊
name: string␊ name: string␊
static withName(name: string): ClassWithFactory␊ static withName(name: string): ClassWithFactory␊

View file

@ -43,6 +43,13 @@ export class Animal {
set name(name: string) set name(name: string)
whoami(): string whoami(): string
static getDogKind(): Kind static getDogKind(): Kind
}
export class Blake2BHasher {
static withKey(key: Blake2bKey): Blake2BHasher
}
export class Blake2BKey {
} }
export class ClassWithFactory { export class ClassWithFactory {
name: string name: string

View file

@ -53,3 +53,25 @@ impl Animal {
Kind::Dog 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
}
}