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

Fix JSDoc formatting for Rust block comments #342

Merged
merged 2 commits into from
Jul 25, 2024
Merged
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
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);
}