From 84f3092d70b60c10230364d29aa3bc64f819d25a Mon Sep 17 00:00:00 2001 From: LongYinan Date: Wed, 27 Dec 2023 21:18:02 +0800 Subject: [PATCH] fix(napi-derive): proc-macro crash on enum (#1870) --- crates/macro/src/parser/mod.rs | 33 +++++++++++++++++++-------------- examples/napi/src/enum.rs | 1 + 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/crates/macro/src/parser/mod.rs b/crates/macro/src/parser/mod.rs index bfde66b4..5de28f29 100644 --- a/crates/macro/src/parser/mod.rs +++ b/crates/macro/src/parser/mod.rs @@ -273,20 +273,25 @@ fn extract_doc_comments(attrs: &[syn::Attribute]) -> Vec { .filter_map(|a| { // if the path segments include an ident of "doc" we know this // this is a doc comment - if a.path().is_ident("doc") { - Some( - // We want to filter out any Puncts so just grab the Literals - match &a.meta.require_name_value().unwrap().value { - syn::Expr::Lit(ExprLit { - lit: syn::Lit::Str(str), - .. - }) => { - let quoted = str.token().to_string(); - Some(try_unescape("ed).unwrap_or(quoted)) - } - _ => None, - }, - ) + let name_value = a.meta.require_name_value(); + if let Ok(name) = name_value { + if a.path().is_ident("doc") { + Some( + // We want to filter out any Puncts so just grab the Literals + match &name.value { + syn::Expr::Lit(ExprLit { + lit: syn::Lit::Str(str), + .. + }) => { + let quoted = str.token().to_string(); + Some(try_unescape("ed).unwrap_or(quoted)) + } + _ => None, + }, + ) + } else { + None + } } else { None } diff --git a/examples/napi/src/enum.rs b/examples/napi/src/enum.rs index 73806eb7..11435af7 100644 --- a/examples/napi/src/enum.rs +++ b/examples/napi/src/enum.rs @@ -26,6 +26,7 @@ pub enum CustomNumEnum { Two, Three = 3, Four, + #[doc(hidden)] Six = 6, Eight = 8, Nine, // would be 9