Skip to content

Commit

Permalink
Merge branch 'main' into export_same_file
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavo-shigueo committed Jul 25, 2024
2 parents d124d7b + e08161c commit 6ec0871
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 20 deletions.
46 changes: 29 additions & 17 deletions macros/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::collections::HashMap;
use proc_macro2::{Ident, TokenStream};
use quote::quote;
use syn::{
spanned::Spanned, Attribute, Error, Expr, ExprLit, GenericParam, Generics, Lit, Meta, Path,
Result, Type,
spanned::Spanned, Attribute, Error, Expr, ExprLit, GenericParam, Generics, Lit, Path, Result,
Type,
};

use super::attr::{Attr, Serde};
Expand Down Expand Up @@ -140,30 +140,42 @@ where

/// Return doc comments parsed and formatted as JSDoc.
pub fn parse_docs(attrs: &[Attribute]) -> Result<String> {
let lines = attrs
let doc_attrs = attrs
.iter()
.filter_map(|a| match a.meta {
Meta::NameValue(ref x) if x.path.is_ident("doc") => Some(x),
_ => None,
})
.filter_map(|attr| attr.meta.require_name_value().ok())
.filter(|attr| attr.path.is_ident("doc"))
.map(|attr| match attr.value {
Expr::Lit(ExprLit {
lit: Lit::Str(ref str),
..
}) => Ok(str.value()),
_ => syn_err!(attr.span(); "doc attribute with non literal expression found"),
})
.map(|attr| {
attr.map(|line| match line.trim() {
"" => " *".to_owned(),
_ => format!(" *{}", line.trim_end()),
})
_ => syn_err!(attr.span(); "doc with non literal expression found"),
})
.collect::<Result<Vec<_>>>()?;

Ok(match lines.is_empty() {
true => "".to_owned(),
false => format!("/**\n{}\n */\n", lines.join("\n")),
Ok(match doc_attrs.len() {
// No docs
0 => String::new(),

// Multi-line block doc comment (/** ... */)
1 if doc_attrs[0].contains('\n') => format!("/**{}*/\n", &doc_attrs[0]),

// Regular doc comment(s) (///) or single line block doc comment
_ => {
let mut buffer = String::from("/**\n");
let mut lines = doc_attrs.iter().peekable();

while let Some(line) = lines.next() {
buffer.push_str(" *");
buffer.push_str(line);

if lines.peek().is_some() {
buffer.push('\n');
}
}
buffer.push_str("\n */\n");
buffer
}
})
}

Expand Down
44 changes: 41 additions & 3 deletions ts-rs/tests/integration/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use std::{concat, fs};

use ts_rs::TS;

/* ============================================================================================== */

/// Doc comment.
/// Supports new lines.
///
Expand Down Expand Up @@ -93,7 +91,16 @@ struct G {
f: F,
}

/* ============================================================================================== */
#[derive(TS)]
#[ts(export_to = "docs/")]
/**
* Block doc comment
*
* works
*/
struct H {
foo: i32,
}

#[test]
fn export_a() {
Expand Down Expand Up @@ -387,3 +394,34 @@ fn export_g() {

assert_eq!(actual_content, expected_content);
}

#[test]
fn export_h() {
H::export().unwrap();

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\n",
"/**\n",
" * Block doc comment\n",
" *\n",
" * works\n",
" */\n",
"export type H = { foo: number };\n",
)
} else {
concat!(
"// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.\n\n",
"/**\n",
" * Block doc comment\n",
" *\n",
" * works\n",
" */\n",
"export type H = { foo: number, };\n",
)
};

let actual_content = fs::read_to_string(H::default_output_path().unwrap()).unwrap();

assert_eq!(actual_content, expected_content);
}

0 comments on commit 6ec0871

Please sign in to comment.