implement short circuiting for and

This commit is contained in:
ConnorSkees 2020-06-20 22:29:35 -04:00
parent 218d73c982
commit 52bf3f0091
2 changed files with 25 additions and 5 deletions

View File

@ -895,11 +895,26 @@ impl<'a, 'b: 'a> IntermediateValueIterator<'a, 'b> {
.push(Value::String(op.to_string(), QuoteKind::None).span(op.span));
} else if let Some(left) = space_separated.pop() {
self.whitespace();
let right = self.single_value()?;
space_separated.push(
Value::BinaryOp(Box::new(left.node), op.node, Box::new(right.node))
.span(left.span.merge(right.span)),
);
if left.node.is_true(left.span)? {
let right = self.single_value()?;
space_separated.push(
Value::BinaryOp(Box::new(left.node), op.node, Box::new(right.node))
.span(left.span.merge(right.span)),
);
} else {
// we explicitly ignore errors here as a workaround for short circuiting
while let Some(value) = self.peek() {
if let Ok(Spanned {
node: IntermediateValue::Comma,
..
}) = value
{
break;
}
self.next();
}
space_separated.push(left);
}
} else {
return Err(("Expected expression.", op.span).into());
}

View File

@ -51,3 +51,8 @@ test!(
"a {\n color: 1 - AND;\n}\n",
"a {\n color: 1-AND;\n}\n"
);
test!(
short_circuits_when_lhs_is_false,
"a {\n color: false and comparable(\"a\", \"b\");\n}\n",
"a {\n color: false;\n}\n"
);