respect $module argument passed to function-exists

This commit is contained in:
Connor Skees 2020-08-16 19:47:18 -04:00
parent f60fb26ca0
commit 28a0a33d85
4 changed files with 47 additions and 11 deletions

View File

@ -173,16 +173,38 @@ pub(crate) fn mixin_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> SassR
pub(crate) fn function_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> { pub(crate) fn function_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(2)?; args.max_args(2)?;
match args.get_err(0, "name")? {
Value::String(s, _) => Ok(Value::bool( let name: Identifier = match args.get_err(0, "name")? {
parser.scopes.fn_exists(s.into(), parser.global_scope), Value::String(s, _) => s.into(),
)), v => {
v => Err(( return Err((
format!("$name: {} is not a string.", v.inspect(args.span())?), format!("$name: {} is not a string.", v.inspect(args.span())?),
args.span(), args.span(),
) )
.into()), .into())
} }
};
let module = match args.default_arg(1, "module", Value::Null)? {
Value::String(s, _) => Some(s),
Value::Null => None,
v => {
return Err((
format!("$module: {} is not a string.", v.inspect(args.span())?),
args.span(),
)
.into())
}
};
Ok(Value::bool(if let Some(module_name) = module {
parser
.modules
.get(module_name.into(), args.span())?
.fn_exists(name)
} else {
parser.scopes.fn_exists(name, parser.global_scope)
}))
} }
pub(crate) fn get_function(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> { pub(crate) fn get_function(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {

View File

@ -191,6 +191,10 @@ impl Module {
!name.as_str().starts_with('-') && self.scope.mixin_exists(name) !name.as_str().starts_with('-') && self.scope.mixin_exists(name)
} }
pub fn fn_exists(&self, name: Identifier) -> bool {
!name.as_str().starts_with('-') && self.scope.fn_exists(name)
}
pub fn insert_builtin( pub fn insert_builtin(
&mut self, &mut self,
name: &'static str, name: &'static str,

View File

@ -67,7 +67,7 @@ impl Scope {
self.functions.insert(s, v) self.functions.insert(s, v)
} }
fn fn_exists(&self, name: Identifier) -> bool { pub fn fn_exists(&self, name: Identifier) -> bool {
if self.functions.is_empty() { if self.functions.is_empty() {
return false; return false;
} }

View File

@ -18,6 +18,16 @@ test!(
"@use 'sass:math';\na {\n color: global-variable-exists(pi, $module: math);\n}\n", "@use 'sass:math';\na {\n color: global-variable-exists(pi, $module: math);\n}\n",
"a {\n color: true;\n}\n" "a {\n color: true;\n}\n"
); );
test!(
fn_exists_builtin,
"@use 'sass:math';\na {\n color: function-exists(acos, $module: math);\n}\n",
"a {\n color: true;\n}\n"
);
error!(
fn_exists_module_dne,
"a {\n color: function-exists(c, d);\n}\n",
"Error: There is no module with the namespace \"d\"."
);
#[test] #[test]
fn mixin_exists_module() { fn mixin_exists_module() {