From e6f341f6323ff9bfdb8c99989233e302002802be Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 25 Nov 2021 23:42:40 +0800 Subject: [PATCH] chore: add example for return this --- examples/napi/__test__/typegen.spec.ts.md | 1 + examples/napi/__test__/typegen.spec.ts.snap | Bin 1366 -> 1374 bytes examples/napi/__test__/values.spec.ts | 4 ++++ examples/napi/index.d.ts | 1 + examples/napi/src/class_factory.rs | 6 ++++++ 5 files changed, 12 insertions(+) diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index aebed60f..31303f56 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -97,6 +97,7 @@ Generated by [AVA](https://avajs.dev). export class ClassWithFactory {␊ name: string␊ static withName(name: string): ClassWithFactory␊ + setName(name: string): this␊ }␊ export namespace xxh3 {␊ export const ALIGNMENT: number␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 82a6b45bdefa0228511e88b935b989b75bdcea63..1357fa794d6020d01417bb220adc8bb6bb958bd9 100644 GIT binary patch literal 1374 zcmV-k1)=&uRzV>rB=00000000B6 zSIur4HxQN^6a{=rPrVhqWfg23OLpCK;ntFB+8 zuh2e3Z$*LLd+sar*mH+mtwd>eEhDH8@{s)Ihcm;OSyHJ~Rw@sE{^GAc|M}s!U%vYK z-@pI({nwR>{(JJUQh8AM%+M>JRzCma%ZE=N6Oa+(>Pg5X^&=pUpRTUL^-#o$1QC^z zG_MuJoJObqdkEBaXNTOZ5)uOq1&_w$(Id}nENms5eMo@F@xT|6Iev5GO_pyx(!s6U z)!V5i!KESx%~s>$xbtrR^t|H{o(z15GekGxfntJ_9;jC$4y9A|NF$Evcw4HN@!n1m zvYiYpU@9>Wl*zyV@mwxotEG16+qPH)4LRn4Go-4I*E5zN&si)6Ov3hF((M*$q?+j@ zi4^OMghS(ArpDqev({%lj8`KJ9qN%i);r|NC4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e!mY-$foh%!^k?>3J99q0e4)Q?Zu;9>4bG^_vMsH6v}`f({_dX;YsdR;a?+iC_q5vj`a|4EMJXV)LFj49mJ0 z?-s?^@LeB%5Np0xm`3B+Z9L;FiyY`2xAB75-KXn0-f7j(M;@O$WkPuhYh5^XQH^Nq zWZ;&8Nii;x5K@iR!&45rTpfZhb+^8KfA|U2Ow{j#SD-KKo;~XL8|P%>YS|3tWSl({~)hLHRY!=f)5dg$(ojQI|y28 z{_~(A*nsl+Z-OhF9(+4P;I5tR<0RC0pV>ieu5?rpr?5k@(8kXI9IUCcRtuug9?R)f zog$;M%|)4`zN7;-k4@E}92{<}qUyxdyZMXlb9x0E``=K7)0Dm6f{`%xu3+p>fB~+n z{ybNSNJOUc7LHBEVrRBz13C_%4G``5&b(GoC|&n1xbYAJ??w3Iyzq$*qXF>QMUHP4 ztuenZQA~>(;p3uu? { const duck = ClassWithFactory.withName('Default') t.is(duck.name, 'Default') + const ret = duck.setName('D') + t.is(ret.name, 'D') + t.is(ret, duck) + duck.name = '周黑鸭' t.is(duck.name, '周黑鸭') diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 93e7c921..d9afc5fd 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -87,6 +87,7 @@ export class Context { export class ClassWithFactory { name: string static withName(name: string): ClassWithFactory + setName(name: string): this } export namespace xxh3 { export const ALIGNMENT: number diff --git a/examples/napi/src/class_factory.rs b/examples/napi/src/class_factory.rs index 116f9772..50daac52 100644 --- a/examples/napi/src/class_factory.rs +++ b/examples/napi/src/class_factory.rs @@ -9,4 +9,10 @@ impl ClassWithFactory { pub fn with_name(name: String) -> Self { Self { name } } + + #[napi] + pub fn set_name(&mut self, name: String) -> &Self { + self.name = name; + self + } }