deny non-numeric argument passed to unitless

This commit is contained in:
Connor Skees 2020-08-16 20:13:42 -04:00
parent 253bc3b35f
commit c35fa2b8eb
3 changed files with 14 additions and 13 deletions

View File

@ -7,6 +7,7 @@ use crate::{
common::{Identifier, QuoteKind},
error::SassResult,
parse::Parser,
unit::Unit,
value::{SassFunction, Value},
};
@ -72,7 +73,17 @@ pub(crate) fn type_of(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult
pub(crate) fn unitless(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
Ok(Value::bool(args.get_err(0, "number")?.unitless()))
Ok(match args.get_err(0, "number")? {
Value::Dimension(_, Unit::None, _) => Value::True,
Value::Dimension(..) => Value::False,
v => {
return Err((
format!("$number: {} is not a number.", v.inspect(args.span())?),
args.span(),
)
.into())
}
})
}
pub(crate) fn inspect(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {

View File

@ -380,15 +380,6 @@ impl Value {
})
}
pub fn unitless(&self) -> bool {
#[allow(clippy::match_same_arms)]
match self {
Value::Dimension(_, Unit::None, _) => true,
Value::Dimension(..) => false,
_ => true,
}
}
pub fn not_equals(&self, other: &Self) -> bool {
match self {
Value::String(s1, ..) => match other {

View File

@ -213,10 +213,9 @@ test!(
"a {\n color: unitless(1)\n}\n",
"a {\n color: true;\n}\n"
);
test!(
error!(
unitless_string,
"a {\n color: unitless(foo)\n}\n",
"a {\n color: true;\n}\n"
"a {\n color: unitless(foo)\n}\n", "Error: $number: foo is not a number."
);
test!(
variable_does_exist,