Skip to content

Commit

Permalink
Check the number of type parameters before handling the Range type (#45)
Browse files Browse the repository at this point in the history
Don't assume that `Range` refers to the built-in Range type unless it has exactly one type parameter. This allows for the correct export of custom-defined Range structs.
  • Loading branch information
todays-mitsui authored Nov 18, 2024
1 parent 9cb9563 commit 39acd09
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 1 deletion.
27 changes: 27 additions & 0 deletions tests-e2e/reference_output/test_range1/test_range1.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* tslint:disable */
/* eslint-disable */
/**
* @param {Range} _range
*/
export function consume(_range: Range): void;
/**
* @returns {Range}
*/
export function into_js(): Range;
/**
* @param {(Range)[]} _ranges
*/
export function consume_vector(_ranges: (Range)[]): void;
/**
* @returns {(Range)[]}
*/
export function vector_into_js(): (Range)[];
export interface Range {
foo: number;
bar: string;
}

export interface A {
range: Range;
}

21 changes: 21 additions & 0 deletions tests-e2e/test_range1/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "test_range1"
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"
48 changes: 48 additions & 0 deletions tests-e2e/test_range1/entry_point.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 Range {
foo: u32,
bar: String,
}

#[derive(Tsify, Serialize, Deserialize)]
#[tsify(into_wasm_abi, from_wasm_abi)]
pub struct A {
range: Range,
}

#[wasm_bindgen]
pub fn consume(_range: Range) {}

#[wasm_bindgen]
pub fn into_js() -> Range {
Range {
foo: 42,
bar: "BAR".to_string(),
}
}

#[wasm_bindgen]
pub fn consume_vector(_ranges: Vec<Range>) {}

#[wasm_bindgen]
pub fn vector_into_js() -> Vec<Range> {
vec![
Range {
foo: 42,
bar: "BAR".to_string(),
},
Range {
foo: 42,
bar: "BAR".to_string(),
},
Range {
foo: 42,
bar: "BAR".to_string(),
},
]
}
4 changes: 3 additions & 1 deletion tsify-next-macros/src/typescript/ts_type_from_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ impl TsType {
nanos_since_epoch: Self::NUMBER;
},

"Range" | "RangeInclusive" => {
// Treat as std::ops::Range or std::ops::RangeInclusive only when there is exactly one type parameter.
// Otherwise, consider it a user-defined type and do not perform any conversion.
"Range" | "RangeInclusive" if args.len() == 1 => {
let start = Self::from_syn_type(config, args[0]);
let end = start.clone();

Expand Down

0 comments on commit 39acd09

Please sign in to comment.