From 44040e3bfea2c9bc438df5662be43bd072dd2157 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sat, 6 Nov 2021 11:53:53 +0800 Subject: [PATCH] fix(napi-derive): type generate issue for Factory and Class --- crates/backend/src/typegen.rs | 8 +++++-- crates/backend/src/typegen/fn.rs | 5 +++++ crates/backend/src/typegen/struct.rs | 5 +++++ crates/macro/src/parser/mod.rs | 2 +- examples/napi/__test__/typegen.spec.ts.md | 7 +++++++ examples/napi/__test__/typegen.spec.ts.snap | Bin 841 -> 878 bytes examples/napi/index.d.ts | 7 +++++++ examples/napi/src/class.rs | 22 ++++++++++++++++++++ 8 files changed, 53 insertions(+), 3 deletions(-) diff --git a/crates/backend/src/typegen.rs b/crates/backend/src/typegen.rs index 7b3b9da8..0bbd2e1f 100644 --- a/crates/backend/src/typegen.rs +++ b/crates/backend/src/typegen.rs @@ -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(), } } diff --git a/crates/backend/src/typegen/fn.rs b/crates/backend/src/typegen/fn.rs index 9d7c719a..4c93b3e8 100644 --- a/crates/backend/src/typegen/fn.rs +++ b/crates/backend/src/typegen/fn.rs @@ -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); diff --git a/crates/backend/src/typegen/struct.rs b/crates/backend/src/typegen/struct.rs index 7998e0a2..3790bf75 100644 --- a/crates/backend/src/typegen/struct.rs +++ b/crates/backend/src/typegen/struct.rs @@ -6,10 +6,15 @@ use crate::{ty_to_ts_type, NapiImpl, NapiStruct, NapiStructKind}; thread_local! { pub(crate) static TASK_STRUCTS: RefCell> = Default::default(); + pub(crate) static CLASS_STRUCTS: RefCell> = 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" diff --git a/crates/macro/src/parser/mod.rs b/crates/macro/src/parser/mod.rs index 6708c032..de4da2f6 100644 --- a/crates/macro/src/parser/mod.rs +++ b/crates/macro/src/parser/mod.rs @@ -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)?; } diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index a1601ee9..9408013c 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -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␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/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