respect $module argument to mixin-exists

This commit is contained in:
Connor Skees 2020-07-30 18:35:34 -04:00
parent a7325436ca
commit cfd2e00ebb
4 changed files with 48 additions and 12 deletions

View File

@ -110,19 +110,39 @@ pub(crate) fn global_variable_exists(
} }
} }
// todo: should check for module arg (as well as several others)
pub(crate) fn mixin_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> { pub(crate) fn mixin_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(2)?; args.max_args(2)?;
match args.get_err(0, "name")? { let name: Identifier = match args.get_err(0, "name")? {
Value::String(s, _) => Ok(Value::bool( Value::String(s, _) => s.into(),
parser.scopes.mixin_exists(s.into(), parser.global_scope), v => {
)), return Err((
v => 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())?
.mixin_exists(name)
} else {
parser.scopes.mixin_exists(name, parser.global_scope)
}))
} }
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> {

View File

@ -65,6 +65,10 @@ impl Module {
self.0.functions.get(&name).cloned() self.0.functions.get(&name).cloned()
} }
pub fn mixin_exists(&self, name: Identifier) -> bool {
self.0.mixin_exists(name)
}
pub fn insert_builtin( pub fn insert_builtin(
&mut self, &mut self,
name: &'static str, name: &'static str,

View File

@ -55,7 +55,7 @@ impl Scope {
self.mixins.insert(s.into(), v) self.mixins.insert(s.into(), v)
} }
fn mixin_exists(&self, name: Identifier) -> bool { pub fn mixin_exists(&self, name: Identifier) -> bool {
self.mixins.contains_key(&name) self.mixins.contains_key(&name)
} }

View File

@ -1,5 +1,7 @@
#![cfg(test)] #![cfg(test)]
use std::io::Write;
#[macro_use] #[macro_use]
mod macros; mod macros;
@ -13,3 +15,13 @@ test!(
"@use 'sass:meta';\n@use 'sass:math';\na {\n color: inspect(meta.module-variables(math));\n}\n", "@use 'sass:meta';\n@use 'sass:math';\na {\n color: inspect(meta.module-variables(math));\n}\n",
"a {\n color: (\"e\": 2.7182818285, \"pi\": 3.1415926536);\n}\n" "a {\n color: (\"e\": 2.7182818285, \"pi\": 3.1415926536);\n}\n"
); );
#[test]
fn mixin_exists_module() {
let input = "@use \"mixin_exists_module\" as module;\na {\n color: mixin-exists(foo, $module: module);\n}";
tempfile!("mixin_exists_module.scss", "@mixin foo {}");
assert_eq!(
"a {\n color: true;\n}\n",
&grass::from_string(input.to_string(), &grass::Options::default()).expect(input)
);
}