eagerly evaluate \@if conditions
This commit is contained in:
parent
3c97400935
commit
c4cfb9112e
@ -22,12 +22,12 @@ pub(crate) struct If {
|
|||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) struct Branch {
|
pub(crate) struct Branch {
|
||||||
pub cond: Vec<Token>,
|
pub cond: Spanned<Value>,
|
||||||
pub toks: Vec<Token>,
|
pub toks: Vec<Token>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Branch {
|
impl Branch {
|
||||||
pub fn new(cond: Vec<Token>, toks: Vec<Token>) -> Branch {
|
pub fn new(cond: Spanned<Value>, toks: Vec<Token>) -> Branch {
|
||||||
Branch { cond, toks }
|
Branch { cond, toks }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -35,14 +35,17 @@ 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>,
|
||||||
|
scope: &Scope,
|
||||||
|
super_selector: &Selector,
|
||||||
span_before: Span,
|
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_toks = read_until_open_curly_brace(toks);
|
||||||
if toks.next().is_none() {
|
if init_toks.is_empty() || toks.next().is_none() {
|
||||||
return Err(("Expected expression.", span_before).into());
|
return Err(("Expected expression.", span_before).into());
|
||||||
}
|
}
|
||||||
|
let init_cond = Value::from_vec(init_toks, scope, super_selector)?;
|
||||||
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);
|
||||||
if let Some(tok) = toks.next() {
|
if let Some(tok) = toks.next() {
|
||||||
@ -76,7 +79,7 @@ impl If {
|
|||||||
match tok.kind.to_ascii_lowercase() {
|
match tok.kind.to_ascii_lowercase() {
|
||||||
'i' if toks.next().unwrap().kind.to_ascii_lowercase() == 'f' => {
|
'i' if toks.next().unwrap().kind.to_ascii_lowercase() == 'f' => {
|
||||||
toks.next();
|
toks.next();
|
||||||
let cond = read_until_open_curly_brace(toks);
|
let cond = Value::from_vec(read_until_open_curly_brace(toks), scope, super_selector)?;
|
||||||
toks.next();
|
toks.next();
|
||||||
devour_whitespace(toks);
|
devour_whitespace(toks);
|
||||||
branches.push(Branch::new(cond, read_until_closing_curly_brace(toks)));
|
branches.push(Branch::new(cond, read_until_closing_curly_brace(toks)));
|
||||||
@ -111,8 +114,7 @@ impl If {
|
|||||||
let mut toks = Vec::new();
|
let mut toks = Vec::new();
|
||||||
let mut found_true = false;
|
let mut found_true = false;
|
||||||
for branch in self.branches {
|
for branch in self.branches {
|
||||||
let val = Value::from_vec(branch.cond, scope, super_selector)?;
|
if branch.cond.node.is_true(branch.cond.span)? {
|
||||||
if val.node.is_true(val.span)? {
|
|
||||||
toks = branch.toks;
|
toks = branch.toks;
|
||||||
found_true = true;
|
found_true = true;
|
||||||
break;
|
break;
|
||||||
|
@ -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, kind_span)?),
|
node: AtRule::If(If::from_tokens(toks, scope, super_selector, kind_span)?),
|
||||||
span: kind_span,
|
span: kind_span,
|
||||||
},
|
},
|
||||||
AtRuleKind::For => Spanned {
|
AtRuleKind::For => Spanned {
|
||||||
|
10
tests/if.rs
10
tests/if.rs
@ -113,8 +113,18 @@ error!(
|
|||||||
"@if",
|
"@if",
|
||||||
"Error: Expected expression."
|
"Error: Expected expression."
|
||||||
);
|
);
|
||||||
|
error!(
|
||||||
|
no_condition,
|
||||||
|
"@if{}",
|
||||||
|
"Error: Expected expression."
|
||||||
|
);
|
||||||
error!(
|
error!(
|
||||||
nothing_after_open_curly,
|
nothing_after_open_curly,
|
||||||
"@if foo {",
|
"@if foo {",
|
||||||
"Error: expected \"}\"."
|
"Error: expected \"}\"."
|
||||||
);
|
);
|
||||||
|
error!(
|
||||||
|
condition_is_evaluated_eagerly,
|
||||||
|
"@if 1 + 1 =s {\n}",
|
||||||
|
"Error: expected \"=\"."
|
||||||
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user