implement builtin function meta.module-variables

This commit is contained in:
Connor Skees 2020-07-27 18:06:00 -04:00
parent 2b9cad5971
commit 36d7b5d920
4 changed files with 55 additions and 5 deletions

View File

@ -378,12 +378,12 @@ pub(crate) fn declare(f: &mut Module) {
#[cfg(feature = "random")]
f.insert_builtin("random", random);
f.insert_builtin_var(
"pi",
Value::Dimension(Some(Number::from(std::f64::consts::PI)), Unit::None, true),
);
f.insert_builtin_var(
"e",
Value::Dimension(Some(Number::from(std::f64::consts::E)), Unit::None, true),
);
f.insert_builtin_var(
"pi",
Value::Dimension(Some(Number::from(std::f64::consts::PI)), Unit::None, true),
);
}

View File

@ -45,7 +45,28 @@ fn module_functions(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<V
fn module_variables(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value> {
args.max_args(1)?;
todo!()
let module = match args.get_err(0, "module")? {
Value::String(s, ..) => s,
v => {
return Err((
format!("$module: {} is not a string.", v.inspect(args.span())?),
args.span(),
)
.into())
}
};
Ok(Value::Map(
parser
.modules
.get(&module)
.ok_or((
format!("There is no module with the namespace \"{}\".", module),
args.span(),
))?
.variables(),
))
}
pub(crate) fn declare(f: &mut Module) {

View File

@ -68,6 +68,20 @@ impl Module {
.collect::<Vec<(Value, Value)>>(),
)
}
pub fn variables(&self) -> SassMap {
SassMap::new_with(
self.vars
.iter()
.map(|(key, value)| {
(
Value::String(key.to_string(), QuoteKind::Quoted),
value.clone(),
)
})
.collect::<Vec<(Value, Value)>>(),
)
}
}
pub(crate) fn declare_module_color() -> Module {

15
tests/meta-module.rs Normal file
View File

@ -0,0 +1,15 @@
#![cfg(test)]
#[macro_use]
mod macros;
test!(
module_functions_builtin,
"@use 'sass:meta';\na {\n color: inspect(meta.module-functions(meta));\n}\n",
"a {\n color: (\"feature-exists\": get-function(\"feature-exists\"), \"inspect\": get-function(\"inspect\"), \"type-of\": get-function(\"type-of\"), \"keywords\": get-function(\"keywords\"), \"global-variable-exists\": get-function(\"global-variable-exists\"), \"variable-exists\": get-function(\"variable-exists\"), \"function-exists\": get-function(\"function-exists\"), \"mixin-exists\": get-function(\"mixin-exists\"), \"content-exists\": get-function(\"content-exists\"), \"module-variables\": get-function(\"module-variables\"), \"module-functions\": get-function(\"module-functions\"), \"get-function\": get-function(\"get-function\"), \"call\": get-function(\"call\"));\n}\n"
);
test!(
module_variables_builtin,
"@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"
);