Skip to content

Commit

Permalink
Allow empty enums
Browse files Browse the repository at this point in the history
  • Loading branch information
siefkenj committed Apr 11, 2024
1 parent 40c342e commit 4801350
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tsify-next"
version = "0.5.0"
version = "0.5.1"
edition = "2021"
authors = [
"Madono Haru <[email protected]>",
Expand All @@ -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 }
Expand Down
13 changes: 13 additions & 0 deletions tests-e2e/reference_output/test2/test2.d.ts
Original file line number Diff line number Diff line change
@@ -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;

21 changes: 21 additions & 0 deletions tests-e2e/test2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
19 changes: 19 additions & 0 deletions tests-e2e/test2/entry_point.rs
Original file line number Diff line number Diff line change
@@ -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 }
}
12 changes: 12 additions & 0 deletions tests/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tsify-next-macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tsify-next-macros"
version = "0.5.0"
version = "0.5.1"
edition = "2021"
authors = [
"Madono Haru <[email protected]>",
Expand Down
3 changes: 3 additions & 0 deletions tsify-next-macros/src/decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl Display for TsInterfaceDecl {
}
}

/// A Typescript type resulting from an enum declaration.
#[derive(Debug)]
pub struct TsEnumDecl {
pub id: String,
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions tsify-next-macros/src/derive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub fn expand(input: DeriveInput) -> syn::Result<TokenStream> {
Ok(tokens)
}

/// Expand an `enum` or `struct` with `#[derive(Tsify)]`.
pub fn expand_by_attr(args: TokenStream, input: DeriveInput) -> syn::Result<TokenStream> {
let mut cloned_input = input.clone();
let attr: syn::Attribute = parse_quote!(#[tsify(#args)]);
Expand Down
34 changes: 19 additions & 15 deletions tsify-next-macros/src/typescript/ts_type_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>()
.join(" | ");

write!(f, "{types}")
}
_ => {
let types = types
.iter()
.map(|ty| match ty {
TsType::Intersection(_) => format!("({ty})"),
_ => ty.to_string(),
})
.collect::<Vec<_>>()
.join(" | ");

write!(f, "{types}")
}
},

TsType::Override { type_override, .. } => f.write_str(type_override),
}
Expand Down

0 comments on commit 4801350

Please sign in to comment.