clean up past PRs

This commit is contained in:
Moritz Bischof
2022-05-29 12:49:53 +02:00
parent 147e152276
commit 586c0ef921
7 changed files with 40 additions and 30 deletions
+1 -1
View File
@@ -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`.
+9 -2
View File
@@ -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
+12 -12
View File
@@ -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<T: TS + ?Sized>() -> Result<(), ExportError> {
let path = output_path::<T>()?;
export_type_to::<T, _>(&path)
@@ -35,7 +34,17 @@ pub(crate) fn export_type<T: TS + ?Sized>() -> Result<(), ExportError> {
/// Export `T` to the file specified by the `path` argument.
pub(crate) fn export_type_to<T: TS + ?Sized, P: AsRef<Path>>(path: P) -> Result<(), ExportError> {
let buffer = export_type_to_string::<T>()?;
#[allow(unused_mut)]
let mut buffer = export_type_to_string::<T>()?;
// 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<T: TS + ?Sized>() -> Result<String, ExportEr
buffer.push_str(NOTE);
generate_imports::<T>(&mut buffer)?;
generate_decl::<T>(&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)
}
+4 -1
View File
@@ -173,7 +173,8 @@ mod export;
/// `cargo test`
///
/// - `#[ts(export_to = "..")]`:
/// Specifies where the type should be exported to. Defaults to `bindings/<name>.ts`.
/// Specifies where the type should be exported to. Defaults to `bindings/<name>.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::<Self, _>(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<String, ExportError> {
export::export_type_to_string::<Self>()
}
+2 -2
View File
@@ -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",
+1 -1
View File
@@ -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<T> {
+11 -11
View File
@@ -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<number> } } | {
A1: { value: TestTypeA<number> };
} | { A2: { value: TestTypeA<number> } };
"
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<number> } } | {\n",
" A1: { value: TestTypeA<number> };\n",
"} | { A2: { value: TestTypeA<number> } };\n"
)
);
std::fs::remove_file(TestEnum::EXPORT_TO.unwrap()).unwrap();