fix(napi-derive): simplify the optional values in generated declaration file (#1141)
This commit is contained in:
parent
c39060984d
commit
d56c9c56a8
7 changed files with 45 additions and 32 deletions
|
@ -213,9 +213,9 @@ fn fill_ty(template: &str, args: Vec<String>) -> String {
|
|||
ret
|
||||
}
|
||||
|
||||
pub fn ty_to_ts_type(ty: &Type, is_return_ty: bool) -> (String, bool) {
|
||||
pub fn ty_to_ts_type(ty: &Type, is_return_ty: bool, is_struct_field: bool) -> (String, bool) {
|
||||
match ty {
|
||||
Type::Reference(r) => ty_to_ts_type(&r.elem, is_return_ty),
|
||||
Type::Reference(r) => ty_to_ts_type(&r.elem, is_return_ty, is_struct_field),
|
||||
Type::Tuple(tuple) => {
|
||||
if tuple.elems.is_empty() {
|
||||
("undefined".to_owned(), false)
|
||||
|
@ -226,7 +226,7 @@ pub fn ty_to_ts_type(ty: &Type, is_return_ty: bool) -> (String, bool) {
|
|||
tuple
|
||||
.elems
|
||||
.iter()
|
||||
.map(|elem| ty_to_ts_type(elem, false).0)
|
||||
.map(|elem| ty_to_ts_type(elem, false, false).0)
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
),
|
||||
|
@ -244,7 +244,9 @@ pub fn ty_to_ts_type(ty: &Type, is_return_ty: bool) -> (String, bool) {
|
|||
.args
|
||||
.iter()
|
||||
.filter_map(|arg| match arg {
|
||||
syn::GenericArgument::Type(generic_ty) => Some(ty_to_ts_type(generic_ty, false)),
|
||||
syn::GenericArgument::Type(generic_ty) => {
|
||||
Some(ty_to_ts_type(generic_ty, false, false))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
|
@ -255,9 +257,18 @@ pub fn ty_to_ts_type(ty: &Type, is_return_ty: bool) -> (String, bool) {
|
|||
if rust_ty == "Result" && is_return_ty {
|
||||
ts_ty = Some(args.first().unwrap().to_owned());
|
||||
} else if rust_ty == "Option" {
|
||||
ts_ty = args
|
||||
.first()
|
||||
.map(|(arg, _)| (format!("{} | undefined | null", arg), true));
|
||||
ts_ty = args.first().map(|(arg, _)| {
|
||||
(
|
||||
if is_struct_field {
|
||||
arg.to_string()
|
||||
} else if is_return_ty {
|
||||
format!("{}?", arg)
|
||||
} else {
|
||||
format!("{} | undefined | null", arg)
|
||||
},
|
||||
true,
|
||||
)
|
||||
});
|
||||
} else if rust_ty == "AsyncTask" {
|
||||
ts_ty = r#struct::TASK_STRUCTS.with(|t| {
|
||||
let (output_type, _) = args.first().unwrap().to_owned();
|
||||
|
@ -299,7 +310,7 @@ pub fn ty_to_ts_type(ty: &Type, is_return_ty: bool) -> (String, bool) {
|
|||
|
||||
ts_ty.unwrap_or_else(|| ("any".to_owned(), false))
|
||||
}
|
||||
Type::Group(g) => ty_to_ts_type(&g.elem, is_return_ty),
|
||||
Type::Group(g) => ty_to_ts_type(&g.elem, is_return_ty, is_struct_field),
|
||||
_ => ("any".to_owned(), false),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ impl ToTypeDef for NapiConst {
|
|||
def: format!(
|
||||
"export const {}: {}",
|
||||
&self.js_name,
|
||||
ty_to_ts_type(&self.type_name, false).0
|
||||
ty_to_ts_type(&self.type_name, false, false).0
|
||||
),
|
||||
js_mod: self.js_mod.to_owned(),
|
||||
js_doc: js_doc_from_comments(&self.comments),
|
||||
|
|
|
@ -97,7 +97,7 @@ fn gen_callback_type(callback: &CallbackArg) -> String {
|
|||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, arg)| {
|
||||
let (ts_type, is_optional) = ty_to_ts_type(arg, false);
|
||||
let (ts_type, is_optional) = ty_to_ts_type(arg, false, false);
|
||||
FnArg {
|
||||
arg: format!("arg{}", i),
|
||||
ts_type,
|
||||
|
@ -106,7 +106,7 @@ fn gen_callback_type(callback: &CallbackArg) -> String {
|
|||
})
|
||||
.collect::<FnArgList>(),
|
||||
ret = match &callback.ret {
|
||||
Some(ty) => ty_to_ts_type(ty, true).0,
|
||||
Some(ty) => ty_to_ts_type(ty, true, false).0,
|
||||
None => "void".to_owned(),
|
||||
}
|
||||
)
|
||||
|
@ -131,7 +131,7 @@ impl NapiFn {
|
|||
i.mutability = None;
|
||||
}
|
||||
let arg = path.pat.to_token_stream().to_string().to_case(Case::Camel);
|
||||
let (ts_type, is_optional) = ty_to_ts_type(&path.ty, false);
|
||||
let (ts_type, is_optional) = ty_to_ts_type(&path.ty, false, false);
|
||||
|
||||
Some(FnArg {
|
||||
arg,
|
||||
|
@ -181,7 +181,7 @@ impl NapiFn {
|
|||
.unwrap_or_else(|| "".to_owned()),
|
||||
_ => {
|
||||
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, false);
|
||||
if ts_type == "undefined" {
|
||||
"void".to_owned()
|
||||
} else if ts_type == "Self" {
|
||||
|
|
|
@ -36,8 +36,10 @@ impl ToTypeDef for NapiImpl {
|
|||
fn to_type_def(&self) -> Option<TypeDef> {
|
||||
if let Some(output_type) = &self.task_output_type {
|
||||
TASK_STRUCTS.with(|t| {
|
||||
t.borrow_mut()
|
||||
.insert(self.js_name.clone(), ty_to_ts_type(output_type, false).0);
|
||||
t.borrow_mut().insert(
|
||||
self.js_name.clone(),
|
||||
ty_to_ts_type(output_type, false, true).0,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -90,7 +92,7 @@ impl NapiStruct {
|
|||
field_str.push_str("readonly ")
|
||||
}
|
||||
|
||||
let (arg, is_optional) = ty_to_ts_type(&f.ty, false);
|
||||
let (arg, is_optional) = ty_to_ts_type(&f.ty, false, true);
|
||||
let arg = f.ts_type.as_ref().map(|ty| ty.to_string()).unwrap_or(arg);
|
||||
|
||||
let sep = if is_optional { "?" } else { "" };
|
||||
|
|
|
@ -46,7 +46,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
export function chronoDateAdd1Minute(input: Date): Date␊
|
||||
export interface Dates {␊
|
||||
start: Date␊
|
||||
end?: Date | undefined | null␊
|
||||
end?: Date␊
|
||||
}␊
|
||||
export function eitherStringOrNumber(input: string | number): number␊
|
||||
export function returnEither(input: number): string | number␊
|
||||
|
@ -106,7 +106,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
export function xxh64Alias(input: Buffer): bigint␊
|
||||
export function getMapping(): Record<string, number>␊
|
||||
export function sumMapping(nums: Record<string, number>): number␊
|
||||
export function mapOption(val?: number | undefined | null): number | undefined | null␊
|
||||
export function mapOption(val?: number | undefined | null): number?␊
|
||||
export function returnNull(): null␊
|
||||
export function returnUndefined(): void␊
|
||||
export function add(a: number, b: number): number␊
|
||||
|
@ -117,8 +117,8 @@ Generated by [AVA](https://avajs.dev).
|
|||
export function getUndefined(): void␊
|
||||
export function getNull(): JsNull␊
|
||||
export interface AllOptionalObject {␊
|
||||
name?: string | undefined | null␊
|
||||
age?: number | undefined | null␊
|
||||
name?: string␊
|
||||
age?: number␊
|
||||
}␊
|
||||
export function receiveAllOptionalObject(obj?: AllOptionalObject | undefined | null): void␊
|
||||
export const enum ALIAS {␊
|
||||
|
@ -147,8 +147,8 @@ Generated by [AVA](https://avajs.dev).
|
|||
name: string␊
|
||||
/** The version of the package */␊
|
||||
version: string␊
|
||||
dependencies?: Record<string, any> | undefined | null␊
|
||||
devDependencies?: Record<string, any> | undefined | null␊
|
||||
dependencies?: Record<string, any>␊
|
||||
devDependencies?: Record<string, any>␊
|
||||
}␊
|
||||
export function readPackageJson(): PackageJson␊
|
||||
export function getPackageJsonName(packageJson: PackageJson): string␊
|
||||
|
@ -221,7 +221,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
export type Blake2bKey = Blake2BKey␊
|
||||
export class Blake2BKey { }␊
|
||||
export class Context {␊
|
||||
maybeNeed?: boolean | undefined | null␊
|
||||
maybeNeed?: boolean␊
|
||||
constructor()␊
|
||||
static withData(data: string): Context␊
|
||||
method(): string␊
|
||||
|
@ -241,7 +241,7 @@ Generated by [AVA](https://avajs.dev).
|
|||
export type JsAssets = Assets␊
|
||||
export class Assets {␊
|
||||
constructor()␊
|
||||
get(id: number): JsAsset | undefined | null␊
|
||||
get(id: number): JsAsset?␊
|
||||
}␊
|
||||
export type JsAsset = Asset␊
|
||||
export class Asset {␊
|
||||
|
|
Binary file not shown.
16
examples/napi/index.d.ts
vendored
16
examples/napi/index.d.ts
vendored
|
@ -36,7 +36,7 @@ export function chronoDateToMillis(input: Date): number
|
|||
export function chronoDateAdd1Minute(input: Date): Date
|
||||
export interface Dates {
|
||||
start: Date
|
||||
end?: Date | undefined | null
|
||||
end?: Date
|
||||
}
|
||||
export function eitherStringOrNumber(input: string | number): number
|
||||
export function returnEither(input: number): string | number
|
||||
|
@ -96,7 +96,7 @@ export function tsRename(a: { foo: number }): string[]
|
|||
export function xxh64Alias(input: Buffer): bigint
|
||||
export function getMapping(): Record<string, number>
|
||||
export function sumMapping(nums: Record<string, number>): number
|
||||
export function mapOption(val?: number | undefined | null): number | undefined | null
|
||||
export function mapOption(val?: number | undefined | null): number?
|
||||
export function returnNull(): null
|
||||
export function returnUndefined(): void
|
||||
export function add(a: number, b: number): number
|
||||
|
@ -107,8 +107,8 @@ export function getGlobal(): typeof global
|
|||
export function getUndefined(): void
|
||||
export function getNull(): JsNull
|
||||
export interface AllOptionalObject {
|
||||
name?: string | undefined | null
|
||||
age?: number | undefined | null
|
||||
name?: string
|
||||
age?: number
|
||||
}
|
||||
export function receiveAllOptionalObject(obj?: AllOptionalObject | undefined | null): void
|
||||
export const enum ALIAS {
|
||||
|
@ -137,8 +137,8 @@ export interface PackageJson {
|
|||
name: string
|
||||
/** The version of the package */
|
||||
version: string
|
||||
dependencies?: Record<string, any> | undefined | null
|
||||
devDependencies?: Record<string, any> | undefined | null
|
||||
dependencies?: Record<string, any>
|
||||
devDependencies?: Record<string, any>
|
||||
}
|
||||
export function readPackageJson(): PackageJson
|
||||
export function getPackageJsonName(packageJson: PackageJson): string
|
||||
|
@ -211,7 +211,7 @@ export class Blake2BHasher {
|
|||
export type Blake2bKey = Blake2BKey
|
||||
export class Blake2BKey { }
|
||||
export class Context {
|
||||
maybeNeed?: boolean | undefined | null
|
||||
maybeNeed?: boolean
|
||||
constructor()
|
||||
static withData(data: string): Context
|
||||
method(): string
|
||||
|
@ -231,7 +231,7 @@ export class NinjaTurtle {
|
|||
export type JsAssets = Assets
|
||||
export class Assets {
|
||||
constructor()
|
||||
get(id: number): JsAsset | undefined | null
|
||||
get(id: number): JsAsset?
|
||||
}
|
||||
export type JsAsset = Asset
|
||||
export class Asset {
|
||||
|
|
Loading…
Reference in a new issue