From 586c0ef9211e99a32833aebe71e265fd6f545dbe Mon Sep 17 00:00:00 2001 From: Moritz Bischof Date: Sun, 29 May 2022 12:49:53 +0200 Subject: [PATCH] clean up past PRs --- CONTRIBUTING.md | 2 +- macros/src/utils.rs | 11 +++++++++-- ts-rs/src/export.rs | 24 ++++++++++++------------ ts-rs/src/lib.rs | 5 ++++- ts-rs/tests/export_manually.rs | 4 ++-- ts-rs/tests/generics.rs | 2 +- ts-rs/tests/imports.rs | 22 +++++++++++----------- 7 files changed, 40 insertions(+), 30 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3afb77a..f1d62df 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,7 +4,7 @@ If you are unsure what to work on or want to discuss your idea, feel free to ope ### Documentation After implementing a new feature, please document it in the doc comment on `TS` in `ts_rs/lib.rs`. `README.md` is generated from the module doc comment in `ts_rs/lib.rs`. If you added/updated documentation there, run -`cargo readme > ../README.md` from the `ts_rs/` directory. +`cargo readme > ../README.md` from the `ts-rs/` directory. You can install `cargo readme` by running `cargo install cargo-readme`. diff --git a/macros/src/utils.rs b/macros/src/utils.rs index c02ba8f..0f32f2c 100644 --- a/macros/src/utils.rs +++ b/macros/src/utils.rs @@ -59,8 +59,15 @@ pub fn to_ts_ident(ident: &Ident) -> String { /// /// If the name contains special characters it will be wrapped in quotes. pub fn raw_name_to_ts_field(value: String) -> String { - let has_invalid_chars = value.chars().any(|c| !c.is_alphanumeric()); - if has_invalid_chars { + let valid = value + .chars() + .all(|c| c.is_alphanumeric() || c == '_' || c == '$') + && value + .chars() + .next() + .map(|first| !first.is_numeric()) + .unwrap_or(true); + if !valid { format!(r#""{value}""#) } else { value diff --git a/ts-rs/src/export.rs b/ts-rs/src/export.rs index 0b023c6..ed968ed 100644 --- a/ts-rs/src/export.rs +++ b/ts-rs/src/export.rs @@ -26,8 +26,7 @@ pub enum ExportError { ManifestDirNotSet, } -/// Export `T` to the file specified by the `#[ts(export = ..)]` attribute and/or the `out_dir` -/// setting in the `ts.toml` config file. +/// Export `T` to the file specified by the `#[ts(export_to = ..)]` attribute pub(crate) fn export_type() -> Result<(), ExportError> { let path = output_path::()?; export_type_to::(&path) @@ -35,7 +34,17 @@ pub(crate) fn export_type() -> Result<(), ExportError> { /// Export `T` to the file specified by the `path` argument. pub(crate) fn export_type_to>(path: P) -> Result<(), ExportError> { - let buffer = export_type_to_string::()?; + #[allow(unused_mut)] + let mut buffer = export_type_to_string::()?; + + // format output + #[cfg(feature = "format")] + { + use dprint_plugin_typescript::{configuration::ConfigurationBuilder, format_text}; + + let fmt_cfg = ConfigurationBuilder::new().deno().build(); + buffer = format_text(path.as_ref(), &buffer, &fmt_cfg).map_err(Formatting)?; + } if let Some(parent) = path.as_ref().parent() { std::fs::create_dir_all(parent)?; @@ -50,15 +59,6 @@ pub(crate) fn export_type_to_string() -> Result(&mut buffer)?; generate_decl::(&mut buffer); - // format output - #[cfg(feature = "format")] - { - use dprint_plugin_typescript::{configuration::ConfigurationBuilder, format_text}; - - let fmt_cfg = ConfigurationBuilder::new().deno().build(); - buffer = format_text(path.as_ref(), &buffer, &fmt_cfg).map_err(Formatting)?; - } - Ok(buffer) } diff --git a/ts-rs/src/lib.rs b/ts-rs/src/lib.rs index 8e34808..36eafbc 100644 --- a/ts-rs/src/lib.rs +++ b/ts-rs/src/lib.rs @@ -173,7 +173,8 @@ mod export; /// `cargo test` /// /// - `#[ts(export_to = "..")]`: -/// Specifies where the type should be exported to. Defaults to `bindings/.ts`. +/// Specifies where the type should be exported to. Defaults to `bindings/.ts`. +/// If the provided path ends in a trailing `/`, it is interpreted as a directory. /// /// - `#[ts(rename = "..")]`: /// Sets the typescript name of the generated type @@ -283,6 +284,8 @@ pub trait TS: 'static { export::export_type_to::(path) } + /// Manually generate bindings for this type, returning a [`String`]. + /// This function does not format the output, even if the `format` feature is enabled. fn export_to_string() -> Result { export::export_type_to_string::() } diff --git a/ts-rs/tests/export_manually.rs b/ts-rs/tests/export_manually.rs index c41f9d2..aaae5cf 100644 --- a/ts-rs/tests/export_manually.rs +++ b/ts-rs/tests/export_manually.rs @@ -26,7 +26,7 @@ fn export_manually() { let expected_content = if cfg!(feature = "format") { concat!( - "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n", + "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n", "export interface User {\n", " name: string;\n", " age: number;\n", @@ -51,7 +51,7 @@ fn export_manually_dir() { let expected_content = if cfg!(feature = "format") { concat!( - "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n", + "// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n", "export interface UserDir {\n", " name: string;\n", " age: number;\n", diff --git a/ts-rs/tests/generics.rs b/ts-rs/tests/generics.rs index 77b0a79..93bd709 100644 --- a/ts-rs/tests/generics.rs +++ b/ts-rs/tests/generics.rs @@ -126,7 +126,7 @@ fn generic_struct() { #[test] #[ignore] -// https://github.com/Aleph-Alpha/ts-rs/issues/56 +// https://github.com/Aleph-Alpha/ts-rs/issues/56 TODO fn inline() { #[derive(TS)] struct Generic { diff --git a/ts-rs/tests/imports.rs b/ts-rs/tests/imports.rs index 134f486..805f066 100644 --- a/ts-rs/tests/imports.rs +++ b/ts-rs/tests/imports.rs @@ -28,19 +28,19 @@ pub enum TestEnum { fn test_def() { // The only way to get access to how the imports look is to export the type and load the exported file TestEnum::export().unwrap(); - let text = String::from_utf8(std::fs::read(TestEnum::EXPORT_TO.unwrap()).unwrap()).unwrap(); + let text = std::fs::read_to_string(TestEnum::EXPORT_TO.unwrap()).unwrap(); // Checks to make sure imports are ordered and deduplicated - assert_eq!( - text, - "\ -import type { TestTypeA } from \"./ts_rs_test_type_a\"; -import type { TestTypeB } from \"./ts_rs_test_type_b\"; - -export type TestEnum = { C: { value: TestTypeB } } | { - A1: { value: TestTypeA }; -} | { A2: { value: TestTypeA } }; -" + assert_eq!(text, + 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 } } | {\n", + " A1: { value: TestTypeA };\n", + "} | { A2: { value: TestTypeA } };\n" + ) ); std::fs::remove_file(TestEnum::EXPORT_TO.unwrap()).unwrap();