From ee7a146ea18adbd36f0d10ffa67ec6562ff19c64 Mon Sep 17 00:00:00 2001 From: forehalo Date: Fri, 24 Sep 2021 13:00:35 +0800 Subject: [PATCH] update class demo with custom constructor --- crates/backend/src/typegen/fn.rs | 2 +- crates/macro/src/parser/mod.rs | 2 ++ examples/napi/__test__/typegen.spec.ts.md | 4 ++-- examples/napi/__test__/typegen.spec.ts.snap | Bin 586 -> 580 bytes examples/napi/index.d.ts | 4 ++-- examples/napi/src/class.rs | 19 ++++++++++++++++--- 6 files changed, 23 insertions(+), 8 deletions(-) diff --git a/crates/backend/src/typegen/fn.rs b/crates/backend/src/typegen/fn.rs index 08caaa29..2d6a0ae3 100644 --- a/crates/backend/src/typegen/fn.rs +++ b/crates/backend/src/typegen/fn.rs @@ -74,7 +74,7 @@ impl NapiFn { Some(_) => "", None => "static", }, - crate::FnKind::Constructor => "constructor", + crate::FnKind::Constructor => "", crate::FnKind::Getter => "get", crate::FnKind::Setter => "set", } diff --git a/crates/macro/src/parser/mod.rs b/crates/macro/src/parser/mod.rs index 0326ac55..13a1ae8f 100644 --- a/crates/macro/src/parser/mod.rs +++ b/crates/macro/src/parser/mod.rs @@ -542,6 +542,8 @@ fn napi_fn_from_decl( .trim_start_matches("set_") .to_case(Case::Camel) } + } else if opts.constructor().is_some() { + "constructor".to_owned() } else { opts.js_name().map_or_else( || ident.to_string().to_case(Case::Camel), diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index ae4d3aaf..9584e886 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -28,9 +28,9 @@ Generated by [AVA](https://avajs.dev). export function concatLatin1(s: Latin1String): string␊ export class Animal {␊ readonly kind: Kind␊ - name: string␊ constructor(kind: Kind, name: string)␊ - static new(kind: Kind, name: string): Animal␊ + get name(): string␊ + set name(name: string)␊ whoami(): string␊ }␊ ` diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 39980d0c783c255ac03ba96c0c393609f8f7c631..55643bb7169a953b54e1583e178ca1f230db0c25 100644 GIT binary patch literal 580 zcmV-K0=xY|RzVRw5!UPQC`Yz4LEKW&V|Zg4ySquN3a(|hdKo97*0JKBie`Cd;E^K zPKOzE!)gva#Y_I>R0leOk>MeB5W|`F2GR2v`r-z`VGLa{9EA?AVz>~6_BBel3Ptvo zy%s+onqKO+2m6F4_nZHB-o<3Xan?puW9M|zn*>CzsjO;Nqgz|o#5BtYtw=GX72LkL zfk?HYj0vJ1CQD&mFTFbFGfQ-Ol>ojta}WBNp=TUNWf(!JsdKb)R47X;KPdOax|k5R0NI?o*XWSNLT(R?wB7V>ZuMT{&P0oW?I&Yq4m S&BY4$w)qc*+iAtc1ONa|-w^Ep literal 586 zcmV-Q0=4}?RzVLhwF29~AZDHUfvqDK2a{Er{)R0yL$6C|bD`T~g!=vT~^4#Sspmce{ zbsaP=FF@QkcUbh6YE$*@G$o8?Swh)30z&n$9YJ}T791YIl@ciy@P9SC<}^JO8Lt=U zX{|KefRia72+7kBaHCUI#vSRhY+C@wlA#|xl(Fx(iu`#MWxLxDB z&RFGp(DSP~^d&F(i>dNBijn62-hK$D%4t+jLx{ySih~e(VmR^%FG4sMh4&>|xb#KZ z%dEx2`_K#ZdVi1bXg&PD{VqD=UuUgFH9Du0{=_SCMYF1A)w(nTKBc~(|ijBltFvg4hliZ{ox zj2Z*SQsgv)c@v87N6BmkW1RJ Self { Animal { kind, name } } + #[napi(getter)] + pub fn get_name(&self) -> &str { + self.name.as_str() + } + + #[napi(setter)] + pub fn set_name(&mut self, name: String) { + self.name = name; + } + #[napi] pub fn whoami(&self) -> String { match self.kind {