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

Implementing typescript doc_string #102

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 4 additions & 0 deletions macros/src/attr/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct FieldAttr {
pub skip: bool,
pub optional: bool,
pub flatten: bool,
pub doc_string: Option<String>,
}

#[cfg(feature = "serde-compat")]
Expand All @@ -35,6 +36,7 @@ impl FieldAttr {
skip,
optional,
flatten,
doc_string,
}: FieldAttr,
) {
self.rename = self.rename.take().or(rename);
Expand All @@ -43,6 +45,7 @@ impl FieldAttr {
self.skip = self.skip || skip;
self.optional |= optional;
self.flatten |= flatten;
self.doc_string = self.doc_string.take().or(doc_string);
}
}

Expand All @@ -54,6 +57,7 @@ impl_parse! {
"skip" => out.skip = true,
"optional" => out.optional = true,
"flatten" => out.flatten = true,
"doc_string" => out.doc_string = Some(parse_assign_str(input)?),
}
}

Expand Down
23 changes: 15 additions & 8 deletions macros/src/types/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ fn format_variant(
skip,
optional,
flatten,
doc_string,
} = FieldAttr::from_attrs(&variant.attrs)?;

match (skip, &type_override, inline, optional, flatten) {
Expand All @@ -97,25 +98,31 @@ fn format_variant(
)?;
let variant_dependencies = variant_type.dependencies;
let inline_type = variant_type.inline;

let doc_string = match doc_string {
Some(s) => format!("\n/**\n* {}\n*/\n", s),
None => "".to_string(),
};

let formatted = match enum_attr.tagged()? {
Tagged::Untagged => quote!(#inline_type),
Tagged::Externally => match &variant.fields {
Fields::Unit => quote!(format!("\"{}\"", #name)),
_ => quote!(format!("{{ {}: {} }}", #name, #inline_type)),
_ => quote!(format!("{}{{ {}: {} }}", #doc_string, #name, #inline_type)),
},
Tagged::Adjacently { tag, content } => match &variant.fields {
Fields::Unnamed(unnamed) if unnamed.unnamed.len() == 1 => {
let ty = format_type(&unnamed.unnamed[0].ty, dependencies, generics);
quote!(format!("{{ {}: \"{}\", {}: {} }}", #tag, #name, #content, #ty))
quote!(format!("{}{{ {}: \"{}\", {}: {} }}", #doc_string, #tag, #name, #content, #ty))
}
Fields::Unit => quote!(format!("{{ {}: \"{}\" }}", #tag, #name)),
_ => quote!(format!("{{ {}: \"{}\", {}: {} }}", #tag, #name, #content, #inline_type)),
Fields::Unit => quote!(format!("{}{{ {}: \"{}\" }}", #doc_string, #tag, #name)),
_ => quote!(format!("{}{{ {}: \"{}\", {}: {} }}", #doc_string, #tag, #name, #content, #inline_type)),
},
Tagged::Internally { tag } => match variant_type.inline_flattened {
Some(inline_flattened) => quote! {
format!(
"{{ {}: \"{}\", {} }}",
"{}{{ {}: \"{}\", {} }}",
#doc_string,
#tag,
#name,
#inline_flattened
Expand All @@ -124,11 +131,11 @@ fn format_variant(
None => match &variant.fields {
Fields::Unnamed(unnamed) if unnamed.unnamed.len() == 1 => {
let ty = format_type(&unnamed.unnamed[0].ty, dependencies, generics);
quote!(format!("{{ {}: \"{}\" }} & {}", #tag, #name, #ty))
quote!(format!("{}{{ {}: \"{}\" }} & {}", #doc_string, #tag, #name, #ty))
}
Fields::Unit => quote!(format!("{{ {}: \"{}\" }}", #tag, #name)),
Fields::Unit => quote!(format!("{}{{ {}: \"{}\" }}", #doc_string, #tag, #name)),
_ => {
quote!(format!("{{ {}: \"{}\" }} & {}", #tag, #name, #inline_type))
quote!(format!("{}{{ {}: \"{}\" }} & {}", #doc_string, #tag, #name, #inline_type))
}
},
},
Expand Down
8 changes: 7 additions & 1 deletion macros/src/types/named.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ fn format_field(
skip,
optional,
flatten,
doc_string,
} = FieldAttr::from_attrs(&field.attrs)?;

if skip {
Expand Down Expand Up @@ -109,8 +110,13 @@ fn format_field(
};
let valid_name = raw_name_to_ts_field(name);

let doc_string = match doc_string {
Some(s) => format!("\n/**\n* {}\n*/\n", s),
None => "".to_string(),
};

formatted_fields.push(quote! {
format!("{}{}: {},", #valid_name, #optional_annotation, #formatted_ty)
format!("{}{}{}: {},", #doc_string, #valid_name, #optional_annotation, #formatted_ty)
});

Ok(())
Expand Down
8 changes: 7 additions & 1 deletion macros/src/types/newtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub(crate) fn newtype(
skip,
optional,
flatten,
doc_string,
} = FieldAttr::from_attrs(&inner.attrs)?;

match (&rename_inner, skip, optional, flatten) {
Expand All @@ -51,9 +52,14 @@ pub(crate) fn newtype(
None => format_type(inner_ty, &mut dependencies, generics),
};

let doc_string = match doc_string {
Some(s) => format!("\n/**\n* {}\n*/\n", s),
None => "".to_string(),
};

let generic_args = format_generics(&mut dependencies, generics);
Ok(DerivedTS {
decl: quote!(format!("type {}{} = {};", #name, #generic_args, #inline_def)),
decl: quote!(format!("{}type {}{} = {};", #doc_string, #name, #generic_args, #inline_def)),
inline: inline_def,
inline_flattened: None,
name: name.to_owned(),
Expand Down
6 changes: 6 additions & 0 deletions macros/src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub(crate) fn tuple(
}

let generic_args = format_generics(&mut dependencies, generics);

Ok(DerivedTS {
inline: quote! {
format!(
Expand Down Expand Up @@ -66,6 +67,7 @@ fn format_field(
skip,
optional,
flatten,
doc_string,
} = FieldAttr::from_attrs(&field.attrs)?;

if skip {
Expand All @@ -80,6 +82,10 @@ fn format_field(
if flatten {
syn_err!("`flatten` is not applicable to tuple fields")
}
if doc_string.is_some() {
syn_err!("`doc_string` is not applicable to tuple fields")
}


formatted_fields.push(match &type_override {
Some(o) => quote!(#o.to_owned()),
Expand Down
3 changes: 3 additions & 0 deletions ts-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ mod export;
/// - `#[ts(flatten)]`:
/// Flatten this field (only works if the field is a struct)
///
/// - `#[ts(doc_string = "..")]`:
/// Add typescript docstring to this field
///
/// ### enum attributes
///
/// - `#[ts(tag = "..")]`:
Expand Down
16 changes: 16 additions & 0 deletions ts-rs/tests/doc_string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![allow(dead_code)]

use ts_rs::TS;

#[derive(TS)]
struct DocString {
#[ts(doc_string="@mydoc")]
a: i32,
#[ts(doc_string="@mydoc2")]
b: String,
}

#[test]
fn test() {
assert_eq!(DocString::inline(), "{ \n/**\n* @mydoc\n*/\na: number, \n/**\n* @mydoc2\n*/\nb: string, }");
}