From 4ba45bb8c177b8f4cf86a8f70904d17032afd604 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 19 Apr 2020 00:39:18 -0400 Subject: [PATCH] more robust handling of is_null --- src/output.rs | 2 +- src/value/mod.rs | 20 ++++++++++++-------- tests/null.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 tests/null.rs diff --git a/src/output.rs b/src/output.rs index ccb2ccd..81e1af5 100644 --- a/src/output.rs +++ b/src/output.rs @@ -36,7 +36,7 @@ impl Toplevel { fn push_style(&mut self, mut s: Style) -> SassResult<()> { s = s.eval()?; - if s.value.is_null() { + if s.value.is_null(s.value.span)? { return Ok(()); } if let Toplevel::RuleSet(_, entries) = self { diff --git a/src/value/mod.rs b/src/value/mod.rs index a38d76e..ab9fe28 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -40,11 +40,15 @@ pub(crate) enum Value { } impl Value { - pub fn is_null(&self) -> bool { + pub fn is_null(&self, span: Span) -> SassResult { match self { - &Value::Null => true, - Value::Ident(i, QuoteKind::None) if i.is_empty() => true, - _ => false, + &Value::Null => Ok(true), + Value::Ident(i, QuoteKind::None) if i.is_empty() => Ok(true), + 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(..) => { return Err(( - format!("{} isn't a valid CSS value.", dbg!(self.inspect(span)?)), + format!("{} isn't a valid CSS value.", self.inspect(span)?), span, ) .into()) @@ -69,7 +73,7 @@ impl Value { Brackets::None => format!( "{}", vals.iter() - .filter(|x| !x.is_null()) + .filter(|x| !x.is_null(span).unwrap()) .map(|x| x.to_css_string(span)) .collect::>>()? .join(sep.as_str()), @@ -77,7 +81,7 @@ impl Value { Brackets::Bracketed => format!( "[{}]", vals.iter() - .filter(|x| !x.is_null()) + .filter(|x| !x.is_null(span).unwrap()) .map(|x| x.to_css_string(span)) .collect::>>()? .join(sep.as_str()), @@ -119,7 +123,7 @@ impl Value { Self::ArgList(args) => format!( "{}", args.iter() - .filter(|x| !x.is_null()) + .filter(|x| !x.is_null(span).unwrap()) .map(|a| Ok(a.node.to_css_string(span)?)) .collect::>>()? .join(", "), diff --git a/tests/null.rs b/tests/null.rs new file mode 100644 index 0000000..e3d3cf8 --- /dev/null +++ b/tests/null.rs @@ -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", + "" +);