Evaluate binary ops when inserting variables

This commit is contained in:
ConnorSkees 2020-02-29 20:09:41 -05:00
parent b62b9240c0
commit 7d39b0c86a
5 changed files with 12 additions and 12 deletions

View File

@ -181,7 +181,7 @@ impl AtRule {
let mut scope = scope.clone();
if from < to {
for i in from..(to + through) {
scope.insert_var(&var, Value::Dimension(Number::from(i), Unit::None));
scope.insert_var(&var, Value::Dimension(Number::from(i), Unit::None))?;
stmts.extend(eat_unknown_atrule_body(
&mut body.clone().into_iter().peekable(),
&scope,
@ -190,7 +190,7 @@ impl AtRule {
}
} else if from > to {
for i in ((to - through)..(from + 1)).skip(1).rev() {
scope.insert_var(&var, Value::Dimension(Number::from(i), Unit::None));
scope.insert_var(&var, Value::Dimension(Number::from(i), Unit::None))?;
stmts.extend(eat_unknown_atrule_body(
&mut body.clone().into_iter().peekable(),
&scope,
@ -283,7 +283,7 @@ fn eat_unknown_atrule_body<I: Iterator<Item = Token>>(
}));
}
Expr::VariableDecl(name, val) => {
scope.insert_var(&name, *val);
scope.insert_var(&name, *val)?;
}
Expr::MultilineComment(s) => stmts.push(Stmt::MultilineComment(s)),
}

View File

@ -377,8 +377,8 @@ impl Scope {
}
}
pub fn insert_var(&mut self, s: &str, v: Value) -> Option<Value> {
self.vars.insert(s.replace('_', "-"), v)
pub fn insert_var(&mut self, s: &str, v: Value) -> SassResult<Option<Value>> {
Ok(self.vars.insert(s.replace('_', "-"), v.eval()?))
}
pub fn var_exists(&self, v: &str) -> bool {

View File

@ -78,7 +78,7 @@ impl Function {
},
},
};
self.scope.insert_var(&arg.name, val);
self.scope.insert_var(&arg.name, val)?;
}
Ok(self)
}

View File

@ -391,7 +391,7 @@ impl<'a> StyleSheetParser<'a> {
let VariableDecl { val, default } =
eat_variable_value(&mut self.lexer, &self.global_scope)?;
if !default || self.global_scope.get_var(&name).is_err() {
self.global_scope.insert_var(&name, val);
self.global_scope.insert_var(&name, val)?;
}
}
TokenKind::MultilineComment(_) => {
@ -524,10 +524,10 @@ impl<'a> StyleSheetParser<'a> {
}
Expr::VariableDecl(name, val) => {
if self.scope == 0 {
scope.insert_var(&name, *val.clone());
self.global_scope.insert_var(&name, *val);
scope.insert_var(&name, *val.clone())?;
self.global_scope.insert_var(&name, *val)?;
} else {
scope.insert_var(&name, *val);
scope.insert_var(&name, *val)?;
}
}
Expr::Include(rules) => stmts.extend(rules),

View File

@ -81,7 +81,7 @@ impl Mixin {
},
},
};
self.scope.insert_var(&arg.name, val);
self.scope.insert_var(&arg.name, val)?;
}
Ok(self)
}
@ -114,7 +114,7 @@ impl Mixin {
}));
}
Expr::VariableDecl(name, val) => {
self.scope.insert_var(&name, *val);
self.scope.insert_var(&name, *val)?;
}
Expr::MultilineComment(s) => stmts.push(Stmt::MultilineComment(s)),
}