fix(napi-derive): correct the aliased type generation

This commit is contained in:
LongYinan 2021-12-02 16:25:20 +08:00
parent 6d4b4af36f
commit b2fea4d5b3
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
10 changed files with 74 additions and 9 deletions

View file

@ -78,6 +78,15 @@ Generated by [AVA](https://avajs.dev).
age?: number | undefined | null␊
}␊
export function receiveAllOptionalObject(obj?: AllOptionalObject | undefined | null): void␊
export enum ALIAS {␊
A = 0,␊
B = 1␊
}␊
export interface AliasedStruct {␊
a: ALIAS␊
b: number␊
}␊
export function fnReceivedAliased(s: AliasedStruct, e: ALIAS): void␊
export function asyncPlus100(p: Promise<number>): Promise<number>
/** This is an interface for package.json */␊
export interface PackageJson {␊

View file

@ -60,6 +60,9 @@ import {
createExternalTypedArray,
mutateTypedArray,
receiveAllOptionalObject,
fnReceivedAliased,
ALIAS,
AliasedStruct,
} from '../'
test('export const', (t) => {
@ -208,6 +211,15 @@ test('option object', (t) => {
t.notThrows(() => receiveAllOptionalObject({}))
})
test('aliased rust struct and enum', (t) => {
const a: ALIAS = ALIAS.A
const b: AliasedStruct = {
a,
b: 1,
}
t.notThrows(() => fnReceivedAliased(b, ALIAS.B))
})
test('serde-json', (t) => {
const packageJson = readPackageJson()
t.is(packageJson.name, 'napi-rs')

View file

@ -68,6 +68,15 @@ export interface AllOptionalObject {
age?: number | undefined | null
}
export function receiveAllOptionalObject(obj?: AllOptionalObject | undefined | null): void
export enum ALIAS {
A = 0,
B = 1
}
export interface AliasedStruct {
a: ALIAS
b: number
}
export function fnReceivedAliased(s: AliasedStruct, e: ALIAS): void
export function asyncPlus100(p: Promise<number>): Promise<number>
/** This is an interface for package.json */
export interface PackageJson {

View file

@ -42,3 +42,20 @@ fn receive_all_optional_object(obj: Option<AllOptionalObject>) -> Result<()> {
}
Ok(())
}
#[napi(js_name = "ALIAS")]
pub enum AliasedEnum {
A,
B,
}
#[napi(object, js_name = "AliasedStruct")]
pub struct StructContainsAliasedEnum {
pub a: AliasedEnum,
pub b: u32,
}
#[napi]
fn fn_received_aliased(mut s: StructContainsAliasedEnum, e: AliasedEnum) {
s.a = e;
}