feat(napi-derive): support renmae function args and return type

This commit is contained in:
LongYinan 2021-11-26 17:26:14 +08:00
parent e6f341f632
commit e2e3ef95f8
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
11 changed files with 54 additions and 5 deletions
crates
backend/src
macro/src/parser

View file

@ -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)]

View file

@ -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<{}, {}>"),

View file

@ -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 {

View file

@ -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.

View file

@ -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