remove panics on malformed @ if

This commit is contained in:
ConnorSkees 2020-05-23 12:48:34 -04:00
parent 130177d9fe
commit 3c97400935
3 changed files with 21 additions and 4 deletions

View File

@ -1,4 +1,4 @@
use codemap::Spanned;
use codemap::{Spanned, Span};
use peekmore::{PeekMore, PeekMoreIterator};
@ -35,14 +35,21 @@ impl Branch {
impl If {
pub fn from_tokens<I: Iterator<Item = Token>>(
toks: &mut PeekMoreIterator<I>,
span_before: Span,
) -> SassResult<If> {
devour_whitespace_or_comment(toks)?;
let mut branches = Vec::new();
let init_cond = read_until_open_curly_brace(toks);
toks.next();
if toks.next().is_none() {
return Err(("Expected expression.", span_before).into());
}
devour_whitespace_or_comment(toks)?;
let mut init_toks = read_until_closing_curly_brace(toks);
init_toks.push(toks.next().unwrap());
if let Some(tok) = toks.next() {
init_toks.push(tok);
} else {
return Err(("expected \"}\".", span_before).into())
}
devour_whitespace(toks);
branches.push(Branch::new(init_cond, init_toks));

View File

@ -216,7 +216,7 @@ impl AtRule {
span: kind_span,
},
AtRuleKind::If => Spanned {
node: AtRule::If(If::from_tokens(toks)?),
node: AtRule::If(If::from_tokens(toks, kind_span)?),
span: kind_span,
},
AtRuleKind::For => Spanned {

View File

@ -108,3 +108,13 @@ test!(
"@if false {}\n\n@\\45lse {\n a {\n color: red;\n }\n}\n",
"a {\n color: red;\n}\n"
);
error!(
nothing_after_if,
"@if",
"Error: Expected expression."
);
error!(
nothing_after_open_curly,
"@if foo {",
"Error: expected \"}\"."
);