From c4de587f4e794e8fb680a6c076d438c335b0fdd7 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 26 Apr 2020 18:27:08 -0400 Subject: [PATCH] investigate feature complete `inspect()` --- src/value/mod.rs | 20 +++++---- tests/inspect.rs | 112 +++++++++++++++++++++++++++++++++++++++++++++++ tests/meta.rs | 101 ------------------------------------------ 3 files changed, 124 insertions(+), 109 deletions(-) create mode 100644 tests/inspect.rs diff --git a/src/value/mod.rs b/src/value/mod.rs index 0351170..f5c0942 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -109,15 +109,16 @@ fn visit_quoted_string(buf: &mut String, force_double_quote: bool, string: &str) impl Value { pub fn is_null(&self, span: Span) -> SassResult { - match self { - &Value::Null => Ok(true), - Value::Ident(i, QuoteKind::None) if i.is_empty() => Ok(true), + Ok(match self { + &Value::Null => true, + Value::Ident(i, QuoteKind::None) if i.is_empty() => true, Self::BinaryOp(..) | Self::Paren(..) | Self::UnaryOp(..) => { - self.clone().eval(span)?.is_null(span) + self.clone().eval(span)?.is_null(span)? } - Self::List(v, _, Brackets::None) => Ok(v.iter().all(|f| f.is_null(span).unwrap())), - _ => Ok(false), - } + Self::List(v, _, Brackets::Bracketed) if v.is_empty() => false, + Self::List(v, ..) => v.iter().all(|f| f.is_null(span).unwrap()), + _ => false, + }) } pub fn to_css_string(&self, span: Span) -> SassResult { @@ -136,6 +137,7 @@ impl Value { ) .into()) } + // TODO: should to_css_string on function fail? Self::Function(func) => format!("get-function(\"{}\")", func.name()), Self::List(vals, sep, brackets) => match brackets { Brackets::None => vals @@ -253,6 +255,8 @@ impl Value { } } + // TODO: + // https://github.com/sass/dart-sass/blob/d4adea7569832f10e3a26d0e420ae51640740cfb/lib/src/ast/sass/expression/list.dart#L39 pub fn inspect(&self, span: Span) -> SassResult { Ok(match self { Value::List(v, _, brackets) if v.is_empty() => match brackets { @@ -296,7 +300,7 @@ impl Value { .collect::>>()? .join(", ") ), - Value::Paren(v) => format!("({})", v.inspect(span)?), + Value::Paren(v) => v.inspect(span)?, v => v.to_css_string(span)?, }) } diff --git a/tests/inspect.rs b/tests/inspect.rs new file mode 100644 index 0000000..39bc4b1 --- /dev/null +++ b/tests/inspect.rs @@ -0,0 +1,112 @@ +#![cfg(test)] + +#[macro_use] +mod macros; + +test!( + inspect_unquoted_string, + "a {\n color: inspect(foo)\n}\n", + "a {\n color: foo;\n}\n" +); +test!( + inspect_dbl_quoted_string, + "a {\n color: inspect(\"foo\")\n}\n", + "a {\n color: \"foo\";\n}\n" +); +test!( + inspect_sgl_quoted_string, + "a {\n color: inspect(\"foo\")\n}\n", + "a {\n color: \"foo\";\n}\n" +); +test!( + inspect_unitless_number, + "a {\n color: inspect(1)\n}\n", + "a {\n color: 1;\n}\n" +); +test!( + inspect_px_number, + "a {\n color: inspect(1px)\n}\n", + "a {\n color: 1px;\n}\n" +); +test!( + inspect_color_3_hex, + "a {\n color: inspect(#fff)\n}\n", + "a {\n color: #fff;\n}\n" +); +test!( + inspect_color_6_hex, + "a {\n color: inspect(#ffffff)\n}\n", + "a {\n color: #ffffff;\n}\n" +); +test!( + inspect_color_name, + "a {\n color: inspect(red)\n}\n", + "a {\n color: red;\n}\n" +); +test!( + inspect_true, + "a {\n color: inspect(true)\n}\n", + "a {\n color: true;\n}\n" +); +test!( + inspect_false, + "a {\n color: inspect(false)\n}\n", + "a {\n color: false;\n}\n" +); +test!( + inspect_null, + "a {\n color: inspect(null)\n}\n", + "a {\n color: null;\n}\n" +); +test!( + inspect_empty_brackets, + "a {\n color: inspect([]);\n}\n", + "a {\n color: [];\n}\n" +); +test!( + inspect_comma_separated_one_val, + "a {\n color: inspect((1, ));\n}\n", + "a {\n color: (1,);\n}\n" +); +test!( + inspect_comma_separated_one_val_bracketed, + "a {\n color: inspect([1, ]);\n}\n", + "a {\n color: [1,];\n}\n" +); +test!( + inspect_space_separated_one_val_bracketed, + "a {\n color: inspect(append((), 1, space));\n}\n", + "a {\n color: 1;\n}\n" +); +test!( + inspect_list_of_empty_list, + "a {\n color: inspect(((), ()));\n}\n", + "a {\n color: (), ();\n}\n" +); +test!( + #[ignore] + inspect_comma_separated_list_of_comma_separated_lists, + "a {\n color: inspect([(1, 2), (3, 4)]);\n}\n", + "a {\n color: [(1, 2), (3, 4)];\n}\n" +); +test!( + inspect_empty_list, + "a {\n color: inspect(())\n}\n", + "a {\n color: ();\n}\n" +); +test!( + inspect_spaced_list, + "a {\n color: inspect(1 2 3)\n}\n", + "a {\n color: 1 2 3;\n}\n" +); +test!( + #[ignore] + inspect_comma_list, + "a {\n color: inspect(1, 2, 3)\n}\n", + "a {\n color: 1, 2, 3;\n}\n" +); +test!( + inspect_parens, + "a {\n color: inspect((((a))));\n}\n", + "a {\n color: a;\n}\n" +); diff --git a/tests/meta.rs b/tests/meta.rs index abb5b5d..bdb489f 100644 --- a/tests/meta.rs +++ b/tests/meta.rs @@ -187,91 +187,6 @@ test!( "a {\n color: unitless(foo)\n}\n", "a {\n color: true;\n}\n" ); -test!( - inspect_unquoted_string, - "a {\n color: inspect(foo)\n}\n", - "a {\n color: foo;\n}\n" -); -test!( - inspect_dbl_quoted_string, - "a {\n color: inspect(\"foo\")\n}\n", - "a {\n color: \"foo\";\n}\n" -); -test!( - inspect_sgl_quoted_string, - "a {\n color: inspect(\"foo\")\n}\n", - "a {\n color: \"foo\";\n}\n" -); -test!( - inspect_unitless_number, - "a {\n color: inspect(1)\n}\n", - "a {\n color: 1;\n}\n" -); -test!( - inspect_px_number, - "a {\n color: inspect(1px)\n}\n", - "a {\n color: 1px;\n}\n" -); -test!( - inspect_color_3_hex, - "a {\n color: inspect(#fff)\n}\n", - "a {\n color: #fff;\n}\n" -); -test!( - inspect_color_6_hex, - "a {\n color: inspect(#ffffff)\n}\n", - "a {\n color: #ffffff;\n}\n" -); -test!( - inspect_color_name, - "a {\n color: inspect(red)\n}\n", - "a {\n color: red;\n}\n" -); -test!( - inspect_true, - "a {\n color: inspect(true)\n}\n", - "a {\n color: true;\n}\n" -); -test!( - inspect_false, - "a {\n color: inspect(false)\n}\n", - "a {\n color: false;\n}\n" -); -test!( - inspect_null, - "a {\n color: inspect(null)\n}\n", - "a {\n color: null;\n}\n" -); -test!( - inspect_empty_brackets, - "a {\n color: inspect([]);\n}\n", - "a {\n color: [];\n}\n" -); -test!( - inspect_comma_separated_one_val, - "a {\n color: inspect((1, ));\n}\n", - "a {\n color: (1,);\n}\n" -); -test!( - inspect_comma_separated_one_val_bracketed, - "a {\n color: inspect([1, ]);\n}\n", - "a {\n color: [1,];\n}\n" -); -test!( - inspect_space_separated_one_val_bracketed, - "a {\n color: inspect(append((), 1, space));\n}\n", - "a {\n color: 1;\n}\n" -); -test!( - inspect_list_of_empty_list, - "a {\n color: inspect(((), ()));\n}\n", - "a {\n color: (), ();\n}\n" -); -test!( - inspect_comma_separated_list_of_comma_separated_lists, - "a {\n color: inspect([(1, 2), (3, 4)]);\n}\n", - "a {\n color: [(1, 2), (3, 4)];\n}\n" -); test!( variable_does_exist, "$a: red; a {\n color: variable-exists(a)\n}\n", @@ -339,19 +254,3 @@ error!( mixin_exists_non_string, "a {color: mixin-exists(12px)}", "Error: $name: 12px is not a string." ); -test!( - inspect_empty_list, - "a {\n color: inspect(())\n}\n", - "a {\n color: ();\n}\n" -); -test!( - inspect_spaced_list, - "a {\n color: inspect(1 2 3)\n}\n", - "a {\n color: 1 2 3;\n}\n" -); -test!( - #[ignore] - inspect_comma_list, - "a {\n color: inspect(1, 2, 3)\n}\n", - "a {\n color: 1, 2, 3;\n}\n" -);