feat(napi-derive): support renmae function args and return type
This commit is contained in:
parent
e6f341f632
commit
e2e3ef95f8
11 changed files with 54 additions and 5 deletions
crates
|
@ -16,6 +16,8 @@ pub struct NapiFn {
|
|||
pub parent: Option<Ident>,
|
||||
pub strict: bool,
|
||||
pub js_mod: Option<String>,
|
||||
pub ts_args_type: Option<String>,
|
||||
pub ts_return_type: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
|
@ -67,6 +67,7 @@ static KNOWN_TYPES: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
|
|||
("char", "string"),
|
||||
("JsObject", "object"),
|
||||
("Object", "object"),
|
||||
("Array", "unknown[]"),
|
||||
("Value", "any"),
|
||||
("Map", "Record<string, any>"),
|
||||
("HashMap", "Record<{}, {}>"),
|
||||
|
|
|
@ -11,8 +11,15 @@ impl ToTypeDef for NapiFn {
|
|||
r#"{prefix} {name}({args}){ret}"#,
|
||||
prefix = self.gen_ts_func_prefix(),
|
||||
name = &self.js_name,
|
||||
args = self.gen_ts_func_args(),
|
||||
ret = self.gen_ts_func_ret(),
|
||||
args = self
|
||||
.ts_args_type
|
||||
.clone()
|
||||
.unwrap_or_else(|| self.gen_ts_func_args()),
|
||||
ret = self
|
||||
.ts_return_type
|
||||
.clone()
|
||||
.map(|t| format!(": {}", t))
|
||||
.unwrap_or_else(|| self.gen_ts_func_ret()),
|
||||
);
|
||||
|
||||
TypeDef {
|
||||
|
|
|
@ -53,12 +53,13 @@ macro_rules! attrgen {
|
|||
(strict, Strict(Span)),
|
||||
(object, Object(Span)),
|
||||
(namespace, Namespace(Span, String, Span)),
|
||||
(ts_args_type, TsArgsType(Span, String, Span)),
|
||||
(ts_return_type, TsReturnType(Span, String, Span)),
|
||||
|
||||
// impl later
|
||||
// (inspectable, Inspectable(Span)),
|
||||
// (typescript_custom_section, TypescriptCustomSection(Span)),
|
||||
// (skip_typescript, SkipTypescript(Span)),
|
||||
// (typescript_type, TypeScriptType(Span, String, Span)),
|
||||
// (getter_with_clone, GetterWithClone(Span)),
|
||||
|
||||
// For testing purposes only.
|
||||
|
|
|
@ -575,6 +575,8 @@ fn napi_fn_from_decl(
|
|||
attrs,
|
||||
strict: opts.strict().is_some(),
|
||||
js_mod: opts.namespace().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()),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -603,6 +605,12 @@ impl ParseNapi for syn::ItemFn {
|
|||
}
|
||||
impl ParseNapi for syn::ItemStruct {
|
||||
fn parse_napi(&mut self, tokens: &mut TokenStream, opts: BindgenAttrs) -> BindgenResult<Napi> {
|
||||
if opts.ts_args_type().is_some() || opts.ts_return_type().is_some() {
|
||||
bail_span!(
|
||||
self,
|
||||
"#[napi] can't be applied to a struct with #[napi(ts_args_type)] or #[napi(ts_return_type)]"
|
||||
);
|
||||
}
|
||||
let napi = self.convert_to_ast(opts);
|
||||
self.to_tokens(tokens);
|
||||
|
||||
|
@ -611,6 +619,12 @@ impl ParseNapi for syn::ItemStruct {
|
|||
}
|
||||
impl ParseNapi for syn::ItemImpl {
|
||||
fn parse_napi(&mut self, tokens: &mut TokenStream, opts: BindgenAttrs) -> BindgenResult<Napi> {
|
||||
if opts.ts_args_type().is_some() || opts.ts_return_type().is_some() {
|
||||
bail_span!(
|
||||
self,
|
||||
"#[napi] can't be applied to impl with #[napi(ts_args_type)] or #[napi(ts_return_type)]"
|
||||
);
|
||||
}
|
||||
// #[napi] macro will be remove from impl items after converted to ast
|
||||
let napi = self.convert_to_ast(opts);
|
||||
self.to_tokens(tokens);
|
||||
|
@ -618,18 +632,28 @@ impl ParseNapi for syn::ItemImpl {
|
|||
napi
|
||||
}
|
||||
}
|
||||
|
||||
impl ParseNapi for syn::ItemEnum {
|
||||
fn parse_napi(&mut self, tokens: &mut TokenStream, opts: BindgenAttrs) -> BindgenResult<Napi> {
|
||||
if opts.ts_args_type().is_some() || opts.ts_return_type().is_some() {
|
||||
bail_span!(
|
||||
self,
|
||||
"#[napi] can't be applied to a enum with #[napi(ts_args_type)] or #[napi(ts_return_type)]"
|
||||
);
|
||||
}
|
||||
let napi = self.convert_to_ast(opts);
|
||||
self.to_tokens(tokens);
|
||||
|
||||
napi
|
||||
}
|
||||
}
|
||||
|
||||
impl ParseNapi for syn::ItemConst {
|
||||
fn parse_napi(&mut self, tokens: &mut TokenStream, opts: BindgenAttrs) -> BindgenResult<Napi> {
|
||||
if opts.ts_args_type().is_some() || opts.ts_return_type().is_some() {
|
||||
bail_span!(
|
||||
self,
|
||||
"#[napi] can't be applied to a const with #[napi(ts_args_type)] or #[napi(ts_return_type)]"
|
||||
);
|
||||
}
|
||||
let napi = self.convert_to_ast(opts);
|
||||
self.to_tokens(tokens);
|
||||
napi
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue