diff --git a/Cargo.toml b/Cargo.toml index d2c1796..4d8aecb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tsify-next" -version = "0.5.0" +version = "0.5.1" edition = "2021" authors = [ "Madono Haru ", @@ -14,7 +14,7 @@ keywords = ["wasm", "wasm-bindgen", "typescript"] categories = ["wasm"] [dependencies] -tsify-next-macros = { path = "tsify-next-macros", version = "^0.5" } +tsify-next-macros = { path = "tsify-next-macros", version = "0.5.1" } wasm-bindgen = { version = "0.2.86", optional = true } serde = { version = "1.0", optional = true } serde_json = { version = "1.0", optional = true } diff --git a/tests-e2e/reference_output/test2/test2.d.ts b/tests-e2e/reference_output/test2/test2.d.ts new file mode 100644 index 0000000..4c996b0 --- /dev/null +++ b/tests-e2e/reference_output/test2/test2.d.ts @@ -0,0 +1,13 @@ +/* tslint:disable */ +/* eslint-disable */ +/** +* @returns {Point} +*/ +export function into_js(): Point; +export interface Point { + x: number; + y: number; +} + +export type NullPoint = void; + diff --git a/tests-e2e/test2/Cargo.toml b/tests-e2e/test2/Cargo.toml new file mode 100644 index 0000000..177ffe8 --- /dev/null +++ b/tests-e2e/test2/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "test2" +publish = false +version = "0.1.0" +edition = "2021" + +[dependencies] +wasm-bindgen = "0.2" +tsify-next = { path = "../..", version = "*" } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" + +[dev-dependencies] +wasm-bindgen-test = "0.3" + +[lib] +path = "entry_point.rs" +crate-type = ["cdylib"] + +[build-dependencies] +wasm-bindgen-cli = "0.2" diff --git a/tests-e2e/test2/entry_point.rs b/tests-e2e/test2/entry_point.rs new file mode 100644 index 0000000..41993ff --- /dev/null +++ b/tests-e2e/test2/entry_point.rs @@ -0,0 +1,19 @@ +use serde::{Deserialize, Serialize}; +use tsify_next::Tsify; +use wasm_bindgen::prelude::*; + +#[derive(Tsify, Serialize, Deserialize)] +#[tsify(into_wasm_abi, from_wasm_abi)] +pub struct Point { + x: i32, + y: i32, +} + +#[derive(Tsify, Serialize, Deserialize)] +#[tsify(into_wasm_abi, from_wasm_abi)] +pub enum NullPoint {} + +#[wasm_bindgen] +pub fn into_js() -> Point { + Point { x: 0, y: 0 } +} diff --git a/tests/enum.rs b/tests/enum.rs index 83f3159..51a7b19 100644 --- a/tests/enum.rs +++ b/tests/enum.rs @@ -38,6 +38,18 @@ fn test_externally_tagged_enum() { assert_eq!(External::DECL, expected); } +#[test] +fn test_empty_enum() { + #[derive(Tsify)] + enum Empty {} + + let expected = indoc! {r#" + export type Empty = void;"# + }; + + assert_eq!(Empty::DECL, expected); +} + #[test] fn test_externally_tagged_enum_with_namespace() { /// Comment for External diff --git a/tsify-next-macros/Cargo.toml b/tsify-next-macros/Cargo.toml index 22a653b..b3f2fdd 100644 --- a/tsify-next-macros/Cargo.toml +++ b/tsify-next-macros/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tsify-next-macros" -version = "0.5.0" +version = "0.5.1" edition = "2021" authors = [ "Madono Haru ", diff --git a/tsify-next-macros/src/decl.rs b/tsify-next-macros/src/decl.rs index de0c69d..26e1288 100644 --- a/tsify-next-macros/src/decl.rs +++ b/tsify-next-macros/src/decl.rs @@ -91,6 +91,7 @@ impl Display for TsInterfaceDecl { } } +/// A Typescript type resulting from an enum declaration. #[derive(Debug)] pub struct TsEnumDecl { pub id: String, @@ -278,6 +279,8 @@ impl Display for TsEnumDecl { } } +/// A typescript type declaration. For example `type Foo = string;` +/// or `interface Bar { baz: number; }` #[allow(clippy::enum_variant_names)] pub enum Decl { TsTypeAlias(TsTypeAliasDecl), diff --git a/tsify-next-macros/src/derive.rs b/tsify-next-macros/src/derive.rs index 85bf49d..624c776 100644 --- a/tsify-next-macros/src/derive.rs +++ b/tsify-next-macros/src/derive.rs @@ -35,6 +35,7 @@ pub fn expand(input: DeriveInput) -> syn::Result { Ok(tokens) } +/// Expand an `enum` or `struct` with `#[derive(Tsify)]`. pub fn expand_by_attr(args: TokenStream, input: DeriveInput) -> syn::Result { let mut cloned_input = input.clone(); let attr: syn::Attribute = parse_quote!(#[tsify(#args)]); diff --git a/tsify-next-macros/src/typescript/ts_type_display.rs b/tsify-next-macros/src/typescript/ts_type_display.rs index 64a6938..d0035c0 100644 --- a/tsify-next-macros/src/typescript/ts_type_display.rs +++ b/tsify-next-macros/src/typescript/ts_type_display.rs @@ -91,23 +91,27 @@ impl Display for TsType { write!(f, "{types}") } - TsType::Union(types) => { - if types.len() == 1 { + TsType::Union(types) => match types.len() { + 0 => { + write!(f, "void") + } + 1 => { let ty = &types[0]; - return write!(f, "{ty}"); + write!(f, "{ty}") } - - let types = types - .iter() - .map(|ty| match ty { - TsType::Intersection(_) => format!("({ty})"), - _ => ty.to_string(), - }) - .collect::>() - .join(" | "); - - write!(f, "{types}") - } + _ => { + let types = types + .iter() + .map(|ty| match ty { + TsType::Intersection(_) => format!("({ty})"), + _ => ty.to_string(), + }) + .collect::>() + .join(" | "); + + write!(f, "{types}") + } + }, TsType::Override { type_override, .. } => f.write_str(type_override), }