Skip to content

Commit

Permalink
fix: do not generate empty docstring for Rule
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Mar 4, 2024
1 parent 28c01cb commit 45164fb
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions generator/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 45164fb

Please sign in to comment.