From 785c824cd8174e760635f8e1b50e5ddc726cba91 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Mon, 23 Mar 2020 14:46:15 -0400 Subject: [PATCH] check global scope for functions as well --- src/scope.rs | 20 ++++++++++++++++---- tests/functions.rs | 5 +++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/scope.rs b/src/scope.rs index a063b88..dc7e4f9 100644 --- a/src/scope.rs +++ b/src/scope.rs @@ -14,6 +14,13 @@ pub(crate) fn get_global_var(s: &str) -> SassResult { }) } +pub(crate) fn get_global_fn(s: &str) -> SassResult { + GLOBAL_SCOPE.with(|scope| match scope.borrow().functions().get(s) { + Some(v) => Ok(v.clone()), + None => Err("Undefined function.".into()), + }) +} + pub(crate) fn insert_global_var(s: &str, v: Value) -> SassResult> { GLOBAL_SCOPE.with(|scope| scope.borrow_mut().insert_var(s, v)) } @@ -39,6 +46,10 @@ impl Scope { &self.vars } + pub const fn functions(&self) -> &HashMap { + &self.functions + } + pub fn get_var(&self, v: &str) -> SassResult { let name = &v.replace('_', "-"); match self.vars.get(name) { @@ -70,10 +81,11 @@ impl Scope { self.mixins.contains_key(&v.replace('_', "-")) } - pub fn get_fn(&self, v: &str) -> SassResult<&Function> { - match self.functions.get(&v.replace('_', "-")) { - Some(v) => Ok(v), - None => Err("Undefined function.".into()), + pub fn get_fn(&self, v: &str) -> SassResult { + let name = &v.replace('_', "-"); + match self.functions.get(name) { + Some(v) => Ok(v.clone()), + None => get_global_fn(name), } } diff --git a/tests/functions.rs b/tests/functions.rs index 7b494b3..e849911 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -63,3 +63,8 @@ test!( "a {\n @function foo() {\n @return 3;\n }\n}\nb {\n color: foo();\n}\n", "b {\n color: foo();\n}\n" ); +test!( + global_function_in_scope, + "@function f() {\n @return g();\n}\n@function g() {\n @return false;\n}\na {\n color: f();\n color: g();\n}\n", + "a {\n color: false;\n color: false;\n}\n" +);