From fe8f061f1f77826e5119e935cc9d0c98997f8630 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 11 Nov 2021 16:59:24 +0800 Subject: [PATCH] fix(napi-derive-backend): typegen issue when arg is mut --- crates/backend/src/typegen/fn.rs | 6 ++++++ examples/napi/__test__/typegen.spec.ts.md | 2 +- examples/napi/__test__/typegen.spec.ts.snap | Bin 956 -> 952 bytes examples/napi/index.d.ts | 2 +- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/crates/backend/src/typegen/fn.rs b/crates/backend/src/typegen/fn.rs index 4c93b3e8..48a779e5 100644 --- a/crates/backend/src/typegen/fn.rs +++ b/crates/backend/src/typegen/fn.rs @@ -1,5 +1,6 @@ use convert_case::{Case, Casing}; use quote::ToTokens; +use syn::Pat; use super::{ty_to_ts_type, ToTypeDef, TypeDef}; use crate::{CallbackArg, FnKind, NapiFn}; @@ -49,6 +50,11 @@ impl NapiFn { if path.ty.to_token_stream().to_string() == "Env" { return None; } + let mut path = path.clone(); + // remove mutability from PatIdent + if let Pat::Ident(i) = path.pat.as_mut() { + i.mutability = None; + } let mut arg = path.pat.to_token_stream().to_string().to_case(Case::Camel); arg.push_str(": "); arg.push_str(&ty_to_ts_type(&path.ty, false)); diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 66ac07d0..a5582548 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -43,7 +43,7 @@ Generated by [AVA](https://avajs.dev). export function readPackageJson(): PackageJson␊ export function getPackageJsonName(packageJson: PackageJson): string␊ export function contains(source: string, target: string): boolean␊ - export function concatStr(mutS: string): string␊ + export function concatStr(s: string): string␊ export function concatUtf16(s: string): string␊ export function concatLatin1(s: string): string␊ export function withoutAbortController(a: number, b: number): Promise␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 17956eb968ca791f2c5388439da84a91c119bda9..3b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;tvy(bK4tjgOExLWhZX`5L=T6^Pt{Xs&?E!^B zb9k{{@?A7*J6B3tE~H_GN;Fv{A_Pk_2W?}h&wFScL&7c18CI0`rs2@7Nci`5xPQ#d zw(uXh6>@(Y{FG*aDs4Zew`UZbR(VddtD{UeeQg}uHdG-PkxAD<9)05%TkungtB<7r z(0HrHBsn(*`cf1;_l4@pL&T`GQ>lZ)C=|IM}1fh>@??NJUIQH8yej{zLi7b zjE!*?&4Dqkd(pC22PGjZzLjlvg?MLct!Cm@5_HZjXDK}z;wV1ZO&X(Q@Y9rye| eG?(0w&(=+uhS;E$*{DT95}yG8=tze+3IG6F@WxsI diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 66f9791c..a1e3c170 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -33,7 +33,7 @@ interface PackageJson { export function readPackageJson(): PackageJson export function getPackageJsonName(packageJson: PackageJson): string export function contains(source: string, target: string): boolean -export function concatStr(mutS: string): string +export function concatStr(s: string): string export function concatUtf16(s: string): string export function concatLatin1(s: string): string export function withoutAbortController(a: number, b: number): Promise