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