more robust handling of is_null

This commit is contained in:
ConnorSkees 2020-04-19 00:39:18 -04:00
parent deff857d59
commit 4ba45bb8c1
3 changed files with 43 additions and 9 deletions

View File

@ -36,7 +36,7 @@ impl Toplevel {
fn push_style(&mut self, mut s: Style) -> SassResult<()> { fn push_style(&mut self, mut s: Style) -> SassResult<()> {
s = s.eval()?; s = s.eval()?;
if s.value.is_null() { if s.value.is_null(s.value.span)? {
return Ok(()); return Ok(());
} }
if let Toplevel::RuleSet(_, entries) = self { if let Toplevel::RuleSet(_, entries) = self {

View File

@ -40,11 +40,15 @@ pub(crate) enum Value {
} }
impl Value { impl Value {
pub fn is_null(&self) -> bool { pub fn is_null(&self, span: Span) -> SassResult<bool> {
match self { match self {
&Value::Null => true, &Value::Null => Ok(true),
Value::Ident(i, QuoteKind::None) if i.is_empty() => true, Value::Ident(i, QuoteKind::None) if i.is_empty() => Ok(true),
_ => false, Self::BinaryOp(..) | Self::Paren(..) | Self::UnaryOp(..) => {
self.clone().eval(span)?.is_null(span)
}
Self::List(v, ..) => Ok(v.into_iter().all(|f| f.is_null(span).unwrap())),
_ => Ok(false),
} }
} }
@ -59,7 +63,7 @@ impl Value {
}, },
Self::Map(..) => { Self::Map(..) => {
return Err(( return Err((
format!("{} isn't a valid CSS value.", dbg!(self.inspect(span)?)), format!("{} isn't a valid CSS value.", self.inspect(span)?),
span, span,
) )
.into()) .into())
@ -69,7 +73,7 @@ impl Value {
Brackets::None => format!( Brackets::None => format!(
"{}", "{}",
vals.iter() vals.iter()
.filter(|x| !x.is_null()) .filter(|x| !x.is_null(span).unwrap())
.map(|x| x.to_css_string(span)) .map(|x| x.to_css_string(span))
.collect::<SassResult<Vec<String>>>()? .collect::<SassResult<Vec<String>>>()?
.join(sep.as_str()), .join(sep.as_str()),
@ -77,7 +81,7 @@ impl Value {
Brackets::Bracketed => format!( Brackets::Bracketed => format!(
"[{}]", "[{}]",
vals.iter() vals.iter()
.filter(|x| !x.is_null()) .filter(|x| !x.is_null(span).unwrap())
.map(|x| x.to_css_string(span)) .map(|x| x.to_css_string(span))
.collect::<SassResult<Vec<String>>>()? .collect::<SassResult<Vec<String>>>()?
.join(sep.as_str()), .join(sep.as_str()),
@ -119,7 +123,7 @@ impl Value {
Self::ArgList(args) => format!( Self::ArgList(args) => format!(
"{}", "{}",
args.iter() args.iter()
.filter(|x| !x.is_null()) .filter(|x| !x.is_null(span).unwrap())
.map(|a| Ok(a.node.to_css_string(span)?)) .map(|a| Ok(a.node.to_css_string(span)?))
.collect::<SassResult<Vec<String>>>()? .collect::<SassResult<Vec<String>>>()?
.join(", "), .join(", "),

30
tests/null.rs Normal file
View File

@ -0,0 +1,30 @@
#![cfg(test)]
#[macro_use]
mod macros;
test!(
null_in_parens_in_list,
"a {\n color: (null), (null), 3, 4;\n}\n",
"a {\n color: 3, 4;\n}\n"
);
test!(
null_counted_in_list_length,
"a {\n color: length(null null null);\n}\n",
"a {\n color: 3;\n}\n"
);
test!(
simple_null_list_not_emitted,
"a {\n color: null null null;\n}\n",
""
);
test!(
paren_null_list_not_emitted,
"a {\n color: (null null null);\n}\n",
""
);
test!(
bracketed_null_list_not_emitted,
"a {\n color: [null null null];\n}\n",
""
);