respect $module argument to global-variable-exists

This commit is contained in:
Connor Skees 2020-07-30 18:52:16 -04:00
parent cfd2e00ebb
commit a8046b0d83
3 changed files with 42 additions and 9 deletions

View File

@ -99,15 +99,39 @@ pub(crate) fn global_variable_exists(
mut args: CallArgs,
parser: &mut Parser<'_>,
) -> SassResult<Value> {
args.max_args(1)?;
match args.get_err(0, "name")? {
Value::String(s, _) => Ok(Value::bool(parser.global_scope.var_exists(s.into()))),
v => Err((
format!("$name: {} is not a string.", v.inspect(args.span())?),
args.span(),
)
.into()),
}
args.max_args(2)?;
let name: Identifier = match args.get_err(0, "name")? {
Value::String(s, _) => s.into(),
v => {
return Err((
format!("$name: {} is not a string.", v.inspect(args.span())?),
args.span(),
)
.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())?
.var_exists(name)
} else {
parser.global_scope.var_exists(name)
}))
}
pub(crate) fn mixin_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {

View File

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

View File

@ -15,6 +15,11 @@ test!(
"@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"
);
test!(
global_var_exists_module,
"@use 'sass:math';\na {\n color: global-variable-exists(pi, $module: math);\n}\n",
"a {\n color: true;\n}\n"
);
#[test]
fn mixin_exists_module() {