Skip to content

Commit

Permalink
Fix doc comment behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavo-shigueo committed Jul 25, 2024
1 parent 9d63d93 commit 5827f9d
Showing 1 changed file with 29 additions and 17 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

0 comments on commit 5827f9d

Please sign in to comment.