feat(napi-derive): support generic types on fn

This commit is contained in:
LongYinan 2022-04-13 23:55:45 +08:00
parent 6b7cd185a1
commit 89cce5752b
4 changed files with 9 additions and 1 deletions

View file

@ -16,6 +16,7 @@ pub struct NapiFn {
pub parent: Option<Ident>, pub parent: Option<Ident>,
pub strict: bool, pub strict: bool,
pub js_mod: Option<String>, pub js_mod: Option<String>,
pub ts_generic_types: Option<String>,
pub ts_args_type: Option<String>, pub ts_args_type: Option<String>,
pub ts_return_type: Option<String>, pub ts_return_type: Option<String>,
pub skip_typescript: bool, pub skip_typescript: bool,

View file

@ -59,9 +59,14 @@ impl ToTypeDef for NapiFn {
} }
let def = format!( let def = format!(
r#"{prefix} {name}({args}){ret}"#, r#"{prefix} {name}{generic}({args}){ret}"#,
prefix = self.gen_ts_func_prefix(), prefix = self.gen_ts_func_prefix(),
name = &self.js_name, name = &self.js_name,
generic = &self
.ts_generic_types
.as_ref()
.map(|g| format!("<{}>", g))
.unwrap_or("".to_string()),
args = self args = self
.ts_args_type .ts_args_type
.clone() .clone()

View file

@ -55,6 +55,7 @@ macro_rules! attrgen {
(ts_args_type, TsArgsType(Span, String, Span)), (ts_args_type, TsArgsType(Span, String, Span)),
(ts_return_type, TsReturnType(Span, String, Span)), (ts_return_type, TsReturnType(Span, String, Span)),
(ts_type, TsType(Span, String, Span)), (ts_type, TsType(Span, String, Span)),
(ts_generic_types, TsGenericTypes(Span, String, Span)),
// impl later // impl later
// (inspectable, Inspectable(Span)), // (inspectable, Inspectable(Span)),

View file

@ -577,6 +577,7 @@ fn napi_fn_from_decl(
attrs, attrs,
strict: opts.strict().is_some(), strict: opts.strict().is_some(),
js_mod: opts.namespace().map(|(m, _)| m.to_owned()), js_mod: opts.namespace().map(|(m, _)| m.to_owned()),
ts_generic_types: opts.ts_generic_types().map(|(m, _)| m.to_owned()),
ts_args_type: opts.ts_args_type().map(|(m, _)| m.to_owned()), ts_args_type: opts.ts_args_type().map(|(m, _)| m.to_owned()),
ts_return_type: opts.ts_return_type().map(|(m, _)| m.to_owned()), ts_return_type: opts.ts_return_type().map(|(m, _)| m.to_owned()),
skip_typescript: opts.skip_typescript().is_some(), skip_typescript: opts.skip_typescript().is_some(),