Skip to content

Commit

Permalink
Block::parse should propagate warnings from MacroBlocK::parse, but on…
Browse files Browse the repository at this point in the history
…ly if a successful match
  • Loading branch information
scouten committed Sep 20, 2024
1 parent 0fbd431 commit 0960655
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/blocks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
138 changes: 138 additions & 0 deletions src/tests/blocks/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,10 @@ mod r#macro {
attributes::{TAttrlist, TElementAttribute},
blocks::{TBlock, TMacroBlock, TSimpleBlock},
inlines::{TInline, TInlineMacro},
warnings::TWarning,
TSpan,
},
warnings::WarningType,
HasSpan, Span,
};

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0960655

Please sign in to comment.