simplify Scope::get_var

This commit is contained in:
Connor Skees 2020-07-07 17:36:47 -04:00
parent ef25481420
commit 33f81f5bbe
2 changed files with 10 additions and 13 deletions

View File

@ -524,15 +524,13 @@ impl<'a> Parser<'a> {
Ok(v) => v.map_node(|i| i.into()), Ok(v) => v.map_node(|i| i.into()),
Err(e) => return Some(Err(e)), Err(e) => return Some(Err(e)),
}; };
let span = val.span;
IntermediateValue::Value(HigherIntermediateValue::Literal( IntermediateValue::Value(HigherIntermediateValue::Literal(
match self.scopes.last().get_var(&val, self.global_scope) { match self.scopes.last().get_var(&val, self.global_scope) {
Ok(v) => v, Ok(v) => v.clone(),
Err(e) => return Some(Err(e)), Err(e) => return Some(Err(e)),
} },
.node,
)) ))
.span(span) .span(val.span)
} }
'+' => { '+' => {
let span = self.toks.next().unwrap().pos(); let span = self.toks.next().unwrap().pos();

View File

@ -17,7 +17,6 @@ pub(crate) struct Scope {
functions: HashMap<Identifier, Function>, functions: HashMap<Identifier, Function>,
} }
// todo: separate struct for global scope?
impl Scope { impl Scope {
#[must_use] #[must_use]
pub fn new() -> Self { pub fn new() -> Self {
@ -28,20 +27,20 @@ impl Scope {
} }
} }
fn get_var_no_global(&self, name: &Spanned<Identifier>) -> SassResult<Spanned<Value>> { fn get_var_no_global(&self, name: &Spanned<Identifier>) -> SassResult<&Value> {
match self.vars.get(&name.node) { match self.vars.get(&name.node) {
Some(v) => Ok(v.clone()), Some(v) => Ok(&v.node),
None => Err(("Undefined variable.", name.span).into()), None => Err(("Undefined variable.", name.span).into()),
} }
} }
pub fn get_var( pub fn get_var<'a>(
&self, &'a self,
name: &Spanned<Identifier>, name: &Spanned<Identifier>,
global_scope: &Scope, global_scope: &'a Scope,
) -> SassResult<Spanned<Value>> { ) -> SassResult<&Value> {
match self.vars.get(&name.node) { match self.vars.get(&name.node) {
Some(v) => Ok(v.clone()), Some(v) => Ok(&v.node),
None => global_scope.get_var_no_global(name), None => global_scope.get_var_no_global(name),
} }
} }