remove panics on malformed @ if
This commit is contained in:
parent
130177d9fe
commit
3c97400935
@ -1,4 +1,4 @@
|
|||||||
use codemap::Spanned;
|
use codemap::{Spanned, Span};
|
||||||
|
|
||||||
use peekmore::{PeekMore, PeekMoreIterator};
|
use peekmore::{PeekMore, PeekMoreIterator};
|
||||||
|
|
||||||
@ -35,14 +35,21 @@ impl Branch {
|
|||||||
impl If {
|
impl If {
|
||||||
pub fn from_tokens<I: Iterator<Item = Token>>(
|
pub fn from_tokens<I: Iterator<Item = Token>>(
|
||||||
toks: &mut PeekMoreIterator<I>,
|
toks: &mut PeekMoreIterator<I>,
|
||||||
|
span_before: Span,
|
||||||
) -> SassResult<If> {
|
) -> SassResult<If> {
|
||||||
devour_whitespace_or_comment(toks)?;
|
devour_whitespace_or_comment(toks)?;
|
||||||
let mut branches = Vec::new();
|
let mut branches = Vec::new();
|
||||||
let init_cond = read_until_open_curly_brace(toks);
|
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)?;
|
devour_whitespace_or_comment(toks)?;
|
||||||
let mut init_toks = read_until_closing_curly_brace(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);
|
devour_whitespace(toks);
|
||||||
|
|
||||||
branches.push(Branch::new(init_cond, init_toks));
|
branches.push(Branch::new(init_cond, init_toks));
|
||||||
|
@ -216,7 +216,7 @@ impl AtRule {
|
|||||||
span: kind_span,
|
span: kind_span,
|
||||||
},
|
},
|
||||||
AtRuleKind::If => Spanned {
|
AtRuleKind::If => Spanned {
|
||||||
node: AtRule::If(If::from_tokens(toks)?),
|
node: AtRule::If(If::from_tokens(toks, kind_span)?),
|
||||||
span: kind_span,
|
span: kind_span,
|
||||||
},
|
},
|
||||||
AtRuleKind::For => Spanned {
|
AtRuleKind::For => Spanned {
|
||||||
|
10
tests/if.rs
10
tests/if.rs
@ -108,3 +108,13 @@ test!(
|
|||||||
"@if false {}\n\n@\\45lse {\n a {\n color: red;\n }\n}\n",
|
"@if false {}\n\n@\\45lse {\n a {\n color: red;\n }\n}\n",
|
||||||
"a {\n color: red;\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 \"}\"."
|
||||||
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user