diff --git a/src/blocks/block.rs b/src/blocks/block.rs index 6e03f9c..4e5680b 100644 --- a/src/blocks/block.rs +++ b/src/blocks/block.rs @@ -51,11 +51,15 @@ impl<'src> Block<'src> { let line = source.take_normalized_line(); if line.item.contains("::") { let mut macro_block_maw = MacroBlock::parse(source); - if !macro_block_maw.warnings.is_empty() { - warnings.append(&mut macro_block_maw.warnings); - } if let Some(macro_block) = macro_block_maw.item { + // Only propagate warnings from macro block parsing if we think this + // *is* a macro block. Otherwise, there would likely be too many false + // positives. + if !macro_block_maw.warnings.is_empty() { + warnings.append(&mut macro_block_maw.warnings); + } + return MatchAndWarnings { item: Some(MatchedItem { item: Self::Macro(macro_block.item), diff --git a/src/tests/blocks/block.rs b/src/tests/blocks/block.rs index bff2bbd..f6c07d6 100644 --- a/src/tests/blocks/block.rs +++ b/src/tests/blocks/block.rs @@ -181,8 +181,10 @@ mod r#macro { attributes::{TAttrlist, TElementAttribute}, blocks::{TBlock, TMacroBlock, TSimpleBlock}, inlines::{TInline, TInlineMacro}, + warnings::TWarning, TSpan, }, + warnings::WarningType, HasSpan, Span, }; @@ -586,6 +588,142 @@ mod r#macro { } ); } + + #[test] + fn warn_attrlist_has_extra_comma() { + let maw = Block::parse(Span::new("foo::bar[alt=Sunset,width=300,,height=400]")); + + let mi = maw.item.as_ref().unwrap().clone(); + + assert_eq!( + mi.item, + TBlock::Macro(TMacroBlock { + name: TSpan { + data: "foo", + line: 1, + col: 1, + offset: 0, + }, + target: Some(TSpan { + data: "bar", + line: 1, + col: 6, + offset: 5, + }), + attrlist: TAttrlist { + attributes: vec!( + TElementAttribute { + name: Some(TSpan { + data: "alt", + line: 1, + col: 10, + offset: 9, + }), + shorthand_items: vec![], + value: TSpan { + data: "Sunset", + line: 1, + col: 14, + offset: 13, + }, + source: TSpan { + data: "alt=Sunset", + line: 1, + col: 10, + offset: 9, + }, + }, + TElementAttribute { + name: Some(TSpan { + data: "width", + line: 1, + col: 21, + offset: 20, + }), + shorthand_items: vec![], + value: TSpan { + data: "300", + line: 1, + col: 27, + offset: 26, + }, + source: TSpan { + data: "width=300", + line: 1, + col: 21, + offset: 20, + }, + }, + TElementAttribute { + name: Some(TSpan { + data: "height", + line: 1, + col: 32, + offset: 31, + }), + shorthand_items: vec![], + value: TSpan { + data: "400", + line: 1, + col: 39, + offset: 38, + }, + source: TSpan { + data: "height=400", + line: 1, + col: 32, + offset: 31, + }, + } + ), + source: TSpan { + data: "alt=Sunset,width=300,,height=400", + line: 1, + col: 10, + offset: 9, + } + }, + source: TSpan { + data: "foo::bar[alt=Sunset,width=300,,height=400]", + line: 1, + col: 1, + offset: 0, + }, + }) + ); + + assert_eq!( + mi.item.span(), + TSpan { + data: "foo::bar[alt=Sunset,width=300,,height=400]", + line: 1, + col: 1, + offset: 0, + } + ); + + assert_eq!( + mi.after, + TSpan { + data: "", + line: 1, + col: 43, + offset: 42 + } + ); + assert_eq!( + maw.warnings, + vec![TWarning { + source: TSpan { + data: ",", + line: 1, + col: 30, + offset: 29, + }, + warning: WarningType::EmptyAttributeValue, + }] + ); + } } mod section {