From 45164fb364e108d238cf8ea77d916a26b2d37dd3 Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Mon, 4 Mar 2024 10:26:49 +0800 Subject: [PATCH] fix: do not generate empty docstring for Rule --- generator/src/generator.rs | 50 +++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/generator/src/generator.rs b/generator/src/generator.rs index 042b2ff9..fd48ec9d 100644 --- a/generator/src/generator.rs +++ b/generator/src/generator.rs @@ -223,10 +223,17 @@ fn generate_enum( }); let grammar_doc = &doc_comment.grammar_doc; - let mut result = quote! { - #[doc = #grammar_doc] - #[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)] - #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] + let mut result = if grammar_doc.is_empty() { + quote! { + #[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)] + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] + } + } else { + quote! { + #[doc = #grammar_doc] + #[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)] + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] + } }; if non_exhaustive { result.append_all(quote! { @@ -865,6 +872,41 @@ mod tests { ); } + #[test] + fn rule_empty_doc() { + let rules = vec![OptimizedRule { + name: "f".to_owned(), + ty: RuleType::Normal, + expr: OptimizedExpr::Ident("g".to_owned()), + }]; + + let mut line_docs = HashMap::new(); + line_docs.insert("f".to_owned(), "This is rule comment".to_owned()); + + let doc_comment = &DocComment { + grammar_doc: "".to_owned(), + line_docs, + }; + + assert_eq!( + generate_enum(&rules, doc_comment, false, false).to_string(), + quote! { + #[allow(dead_code, non_camel_case_types, clippy::upper_case_acronyms)] + #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] + pub enum Rule { + #[doc = "This is rule comment"] + r#f + } + impl Rule { + pub fn all_rules() -> &'static [Rule] { + &[Rule::r#f] + } + } + } + .to_string() + ); + } + #[test] fn sequence() { let expr = OptimizedExpr::Seq(