proper error messages in (mixin|function)-exists

This commit is contained in:
ConnorSkees 2020-03-23 15:21:59 -04:00
parent e6f2c26bc6
commit 4b529a1236
3 changed files with 26 additions and 8 deletions

View File

@ -107,19 +107,22 @@ pub(crate) fn register(f: &mut HashMap<String, Builtin>) {
"mixin-exists".to_owned(), "mixin-exists".to_owned(),
Box::new(|args, scope| { Box::new(|args, scope| {
max_args!(args, 1); max_args!(args, 1);
let value = arg!(args, 0, "name"); match arg!(args, 0, "name") {
Ok(Value::bool(scope.mixin_exists(&value.to_string()))) Value::Ident(s, _) => Ok(Value::bool(scope.mixin_exists(&s))),
v => Err(format!("$name: {} is not a string.", v).into()),
}
}), }),
); );
f.insert( f.insert(
"function-exists".to_owned(), "function-exists".to_owned(),
Box::new(|args, scope| { Box::new(|args, scope| {
max_args!(args, 1); max_args!(args, 1);
let value = arg!(args, 0, "name"); match arg!(args, 0, "name") {
let s = value.unquote().to_string(); Value::Ident(s, _) => Ok(Value::bool(
Ok(Value::bool(
scope.fn_exists(&s) || GLOBAL_FUNCTIONS.contains_key(&s), scope.fn_exists(&s) || GLOBAL_FUNCTIONS.contains_key(&s),
)) )),
v => Err(format!("$name: {} is not a string.", v).into()),
}
}), }),
); );
f.insert("call".to_owned(), Box::new(|_args, _scope| { f.insert("call".to_owned(), Box::new(|_args, _scope| {

View File

@ -111,7 +111,9 @@ impl Value {
pub fn is_true(&self) -> SassResult<bool> { pub fn is_true(&self) -> SassResult<bool> {
match self { match self {
Value::Null | Value::False => Ok(false), Value::Null | Value::False => Ok(false),
Self::BinaryOp(..) | Self::Paren(..) | Self::UnaryOp(..) => self.clone().eval()?.is_true(), Self::BinaryOp(..) | Self::Paren(..) | Self::UnaryOp(..) => {
self.clone().eval()?.is_true()
}
_ => Ok(true), _ => Ok(true),
} }
} }

View File

@ -278,6 +278,11 @@ test!(
"@function a(){} a {\n color: function-exists(a)\n}\n", "@function a(){} a {\n color: function-exists(a)\n}\n",
"a {\n color: true;\n}\n" "a {\n color: true;\n}\n"
); );
test!(
builtin_function_does_exist,
"a {\n color: function-exists(function-exists)\n}\n",
"a {\n color: true;\n}\n"
);
test!( test!(
function_does_not_exist, function_does_not_exist,
"a {\n color: function-exists(a)\n}\n", "a {\n color: function-exists(a)\n}\n",
@ -288,6 +293,14 @@ test!(
"@function a(){} a {\n color: function-exists($name: a)\n}\n", "@function a(){} a {\n color: function-exists($name: a)\n}\n",
"a {\n color: true;\n}\n" "a {\n color: true;\n}\n"
); );
error!(
function_exists_non_string,
"a {color:function-exists(12px)}", "Error: $name: 12px is not a string."
);
error!(
mixin_exists_non_string,
"a {color:mixin-exists(12px)}", "Error: $name: 12px is not a string."
);
// test!( // test!(
// inspect_empty_list, // inspect_empty_list,
// "a {\n color: inspect(())\n}\n", // "a {\n color: inspect(())\n}\n",