From 4ec44007031411240aa49d98088db3652f6e66e6 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sat, 18 Dec 2021 13:36:45 +0800 Subject: [PATCH] fix(napi-derive-backend): Null and Undefined return type --- crates/backend/src/typegen.rs | 2 ++ examples/napi/__test__/typegen.spec.ts.md | 2 ++ examples/napi/__test__/typegen.spec.ts.snap | Bin 1972 -> 1979 bytes examples/napi/__test__/values.spec.ts | 10 ++++++++++ examples/napi/index.d.ts | 2 ++ examples/napi/src/nullable.rs | 10 ++++++++++ 6 files changed, 26 insertions(+) diff --git a/crates/backend/src/typegen.rs b/crates/backend/src/typegen.rs index 86aa6092..527e46d7 100644 --- a/crates/backend/src/typegen.rs +++ b/crates/backend/src/typegen.rs @@ -98,6 +98,7 @@ static KNOWN_TYPES: Lazy> = Lazy::new(|| { map.extend([ ("JsUndefined", "undefined"), ("()", "undefined"), + ("Undefined", "undefined"), ("JsNumber", "number"), ("i8", "number"), ("i16", "number"), @@ -152,6 +153,7 @@ static KNOWN_TYPES: Lazy> = Lazy::new(|| { ("Either4", "{} | {} | {} | {}"), ("Either5", "{} | {} | {} | {} | {}"), ("unknown", "unknown"), + ("Null", "null"), ("null", "null"), ("Symbol", "symbol"), ("JsSymbol", "symbol"), diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 4424ebd3..106ed2e6 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -66,6 +66,8 @@ Generated by [AVA](https://avajs.dev). export function tsRename(a: { foo: number }): string[]␊ export function xxh64Alias(input: Buffer): BigInt␊ export function mapOption(val?: number | undefined | null): number | undefined | null␊ + export function returnNull(): null␊ + export function returnUndefined(): void␊ export function add(a: number, b: number): number␊ export function fibonacci(n: number): number␊ export function listObjKeys(obj: object): Array␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index d392610085ed0b9a492f6c2b7aadf401768ad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+VWbq7Kf1Fimhu$MB+qR{!a|_3=R~EhYCxKP?slI;+WIo*I41(Kpz&Y= zntr252q`xY1&=x?M&a;s4tRycG9o-lffOqT@x&wDJ4ZN-2HtM_c){=O?T;lr$rCCj zgNU6{^mZZE%GLi-M=ev3VQ8l(REaW7>0qD1ovB3}cco$PlrU z1jCf!k$vVU!jp#KwUz=EK+MVx+FmWv)96F#HXzmu~bp3BT-z*?B}>~-3OPuAas|_6G*54>B}Ok!(aY^*npVGg@Mw6glal*uBuMiLNRE$A8A>HOx)a68Y+OH?u9 zxbqaSQS_Ol_^Rl$DHhLTK7uzB_$FL7_-@troqwTKY`(9V5bJ&V?Ai(Ua^YX&!}cBQ G761SYY0f?X diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index daefa0ca..b080e2d5 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -65,6 +65,8 @@ import { ALIAS, AliasedStruct, appendBuffer, + returnNull, + returnUndefined, } from '../' test('export const', (t) => { @@ -192,6 +194,14 @@ test('get null', (t) => { } }) +test('return Null', (t) => { + t.is(returnNull(), null) +}) + +test('return Undefined', (t) => { + t.is(returnUndefined(), undefined) +}) + test('pass symbol in', (t) => { const sym = Symbol('test') const obj = setSymbolInObj(sym) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index ab36c034..5ee6c2fc 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -56,6 +56,8 @@ export function mutateExternal(external: ExternalObject, newVal: number) export function tsRename(a: { foo: number }): string[] export function xxh64Alias(input: Buffer): BigInt export function mapOption(val?: number | undefined | null): number | undefined | null +export function returnNull(): null +export function returnUndefined(): void export function add(a: number, b: number): number export function fibonacci(n: number): number export function listObjKeys(obj: object): Array diff --git a/examples/napi/src/nullable.rs b/examples/napi/src/nullable.rs index eb52c12c..18a532c8 100644 --- a/examples/napi/src/nullable.rs +++ b/examples/napi/src/nullable.rs @@ -1,4 +1,14 @@ +use napi::bindgen_prelude::*; + #[napi] fn map_option(val: Option) -> Option { val.map(|v| v + 1) } + +#[napi] +fn return_null() -> Null { + Null +} + +#[napi] +fn return_undefined() -> Undefined {}