Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add import-deno as crate feature to import using .ts extension #376

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export type User = { user_id: number, first_name: string, last_name: string, };
| format | Enables formatting of the generated TypeScript bindings. <br/>Currently, this unfortunately adds quite a few dependencies. |
| no-serde-warnings | By default, warnings are printed during build if unsupported serde attributes are encountered. <br/>Enabling this feature silences these warnings. |
| import-esm | When enabled,`import` statements in the generated file will have the `.js` extension in the end of the path to conform to the ES Modules spec. <br/> Example: `import { MyStruct } from "./my_struct.js"` |
| import-deno | When enabled,`import` statements in the generated file will have the `.ts` extension in the end of the path to conform to the Deno spec. <br/> Example: `import { MyStruct } from "./my_struct.ts"` |
| serde-json-impl | Implement `TS` for types from *serde_json* |
| chrono-impl | Implement `TS` for types from *chrono* |
| bigdecimal-impl | Implement `TS` for types from *bigdecimal* |
Expand All @@ -92,8 +93,8 @@ export type User = { user_id: number, first_name: string, last_name: string, };
| ordered-float-impl | Implement `TS` for types from *ordered_float* |
| heapless-impl | Implement `TS` for types from *heapless* |
| semver-impl | Implement `TS` for types from *semver* |
| smol_str-impl | Implement `TS` for types from *smol_str* |
| tokio-impl | Implement `TS` for types from *tokio* |
| smol_str-impl | Implement `TS` for types from *smol_str* |
| tokio-impl | Implement `TS` for types from *tokio* |

<br/>

Expand Down
1 change: 1 addition & 0 deletions ts-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ smol_str-impl = ["smol_str"]
serde-json-impl = ["serde_json"]
no-serde-warnings = ["ts-rs-macros/no-serde-warnings"]
import-esm = []
import-deno = []
tokio-impl = ["tokio"]

[dev-dependencies]
Expand Down
22 changes: 17 additions & 5 deletions ts-rs/src/export.rs
eythaann marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,21 @@ fn generate_imports<T: TS + ?Sized + 'static>(
let dep_path = out_dir.as_ref().join(dep.output_path);
let rel_path = import_path(&path, &dep_path)?;

// Determine the appropriate extension to trim based on the active feature
let trimmed_rel_path = if cfg!(feature = "import-deno") {
rel_path.trim_end_matches(".ts")
} else if cfg!(feature = "import-esm") {
rel_path.trim_end_matches(".js")
} else {
&rel_path
};

// Check if the file is importing itself
let is_same_file = path
.file_name()
.file_stem()
.and_then(std::ffi::OsStr::to_str)
.map(|x| x.trim_end_matches(".ts"))
.map(|x| format!("./{x}"))
.map(|x| x == rel_path.trim_end_matches(".js"))
.unwrap_or(false);
.map(|stem| format!("./{stem}"))
.map_or(false, |formatted_stem| formatted_stem == trimmed_rel_path);

if is_same_file {
continue;
Expand Down Expand Up @@ -347,6 +355,10 @@ fn import_path(from: &Path, import: &Path) -> Result<String, ExportError> {
str_path
};

if cfg!(feature = "import-deno") {
return Ok(path);
}

let path_without_extension = path.trim_end_matches(".ts");

Ok(if cfg!(feature = "import-esm") {
Expand Down
5 changes: 3 additions & 2 deletions ts-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
//! | format | Enables formatting of the generated TypeScript bindings. <br/>Currently, this unfortunately adds quite a few dependencies. |
//! | no-serde-warnings | By default, warnings are printed during build if unsupported serde attributes are encountered. <br/>Enabling this feature silences these warnings. |
//! | import-esm | When enabled,`import` statements in the generated file will have the `.js` extension in the end of the path to conform to the ES Modules spec. <br/> Example: `import { MyStruct } from "./my_struct.js"` |
//! | import-deno | When enabled,`import` statements in the generated file will have the `.ts` extension in the end of the path to conform to the Deno spec. <br/> Example: `import { MyStruct } from "./my_struct.ts"` |
//! | serde-json-impl | Implement `TS` for types from *serde_json* |
//! | chrono-impl | Implement `TS` for types from *chrono* |
//! | bigdecimal-impl | Implement `TS` for types from *bigdecimal* |
Expand All @@ -90,8 +91,8 @@
//! | ordered-float-impl | Implement `TS` for types from *ordered_float* |
//! | heapless-impl | Implement `TS` for types from *heapless* |
//! | semver-impl | Implement `TS` for types from *semver* |
//! | smol_str-impl | Implement `TS` for types from *smol_str* |
//! | tokio-impl | Implement `TS` for types from *tokio* |
//! | smol_str-impl | Implement `TS` for types from *smol_str* |
//! | tokio-impl | Implement `TS` for types from *tokio* |
//!
//! <br/>
//!
Expand Down
56 changes: 29 additions & 27 deletions ts-rs/tests/integration/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,44 @@ fn test_def() {
TestEnum::export_all().unwrap();
let text = std::fs::read_to_string(TestEnum::default_output_path().unwrap()).unwrap();

let expected = match (cfg!(feature = "format"), cfg!(feature = "import-esm")) {
(true, true) => concat!(
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n",
"import type { TestTypeA } from \"./ts_rs_test_type_a.js\";\n",
"import type { TestTypeB } from \"./ts_rs_test_type_b.js\";\n",
"\n",
"export type TestEnum = { \"C\": { value: TestTypeB<number> } } | {\n",
" \"A1\": { value: TestTypeA<number> };\n",
"} | { \"A2\": { value: TestTypeA<number> } };\n",
),
(true, false) => concat!(
let extension = if cfg!(feature = "import-deno") {
".ts"
} else if cfg!(feature = "import-esm") {
".js"
} else {
""
};

let import_a = format!(
"import type {{ TestTypeA }} from \"./ts_rs_test_type_a{}\";\n",
extension
);

let import_b = format!(
"import type {{ TestTypeB }} from \"./ts_rs_test_type_b{}\";\n",
extension
);

let expected = if cfg!(feature = "format") {
vec!(
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n",
"import type { TestTypeA } from \"./ts_rs_test_type_a\";\n",
"import type { TestTypeB } from \"./ts_rs_test_type_b\";\n",
&import_a,
&import_b,
"\n",
"export type TestEnum = { \"C\": { value: TestTypeB<number> } } | {\n",
" \"A1\": { value: TestTypeA<number> };\n",
"} | { \"A2\": { value: TestTypeA<number> } };\n",
),
(false, true) => concat!(
)
} else {
vec!(
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n",
"import type { TestTypeA } from \"./ts_rs_test_type_a.js\";\n",
"import type { TestTypeB } from \"./ts_rs_test_type_b.js\";\n",
&import_a,
&import_b,
"\n",
"export type TestEnum = { \"C\": { value: TestTypeB<number>, } } | { \"A1\": { value: TestTypeA<number>, } } | { \"A2\": { value: TestTypeA<number>, } };",
"\n",
),
(false, false) => concat!(
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n",
"import type { TestTypeA } from \"./ts_rs_test_type_a\";\n",
"import type { TestTypeB } from \"./ts_rs_test_type_b\";\n",
"\n",
"export type TestEnum = { \"C\": { value: TestTypeB<number>, } } | { \"A1\": { value: TestTypeA<number>, } } | { \"A2\": { value: TestTypeA<number>, } };",
"\n",
),
};
)
}.join("").to_string();

assert_eq!(text, expected);
}
2 changes: 1 addition & 1 deletion ts-rs/tests/integration/issue_168.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct Baz {
}

#[test]
#[cfg(not(feature = "import-esm"))]
#[cfg(not(any(feature = "import-esm", feature = "import-deno")))]
fn issue_168() {
assert_eq!(
FooInlined::export_to_string().unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion ts-rs/tests/integration/issue_232.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ enum Enum {
}

#[test]
#[cfg(not(feature = "import-esm"))]
#[cfg(not(any(feature = "import-esm", feature = "import-deno")))]
fn issue_232() {
println!("{}", StateInlinedVec::export_to_string().unwrap());
assert_eq!(
Expand Down
Loading