Handle positional arguments to mixins

This commit is contained in:
ConnorSkees 2020-01-18 09:03:38 -05:00
parent d1ea6d2dd6
commit ab8e98244f
2 changed files with 22 additions and 4 deletions

View File

@ -384,8 +384,8 @@ fn eat_include<I: Iterator<Item = Token>>(
return Err((pos, "expected identifier")); return Err((pos, "expected identifier"));
}; };
let rules = mixin let rules = mixin
.call_with_args(&args) .args(&args)
.eval(super_selector, &mut scope.clone())?; .call(super_selector)?;
Ok(rules) Ok(rules)
} }
@ -1121,4 +1121,19 @@ mod css_mixins {
"@mixin a {\n $a: blue;\nb {\n $a: red;\n} color: $a\n}\nd {\n @include a;\n}\n", "@mixin a {\n $a: blue;\nb {\n $a: red;\n} color: $a\n}\nd {\n @include a;\n}\n",
"d {\n color: red;\n}\n" "d {\n color: red;\n}\n"
); );
test!(
mixin_single_arg,
"@mixin a($b) {\n color: $b;\n}\nd {\n @include a(red);\n}\n",
"d {\n color: red;\n}\n"
);
test!(
mixin_two_args,
"@mixin a($b, $c) {\n color: $b;\n color: $c\n}\nd {\n @include a(red, blue);\n}\n",
"d {\n color: red;\n color: blue;\n}\n"
);
test!(
mixin_arg_trailing_comma,
"@mixin a($b, $c,) {\n color: $b;\n color: $c\n}\nd {\n @include a(red, blue);\n}\n",
"d {\n color: red;\n color: blue;\n}\n"
);
} }

View File

@ -25,12 +25,11 @@ impl Mixin {
} }
} }
pub fn call_with_args(&mut self, args: &CallArgs) -> &mut Mixin { pub fn args(&mut self, args: &CallArgs) -> &mut Mixin {
for (idx, arg) in args.0.iter().enumerate() { for (idx, arg) in args.0.iter().enumerate() {
if arg.is_named() { if arg.is_named() {
todo!("keyword args") todo!("keyword args")
} else { } else {
// dbg!(&self.args.0[idx].name.clone());
self.scope self.scope
.vars .vars
.insert(self.args.0[idx].name.clone(), arg.val.clone()); .insert(self.args.0[idx].name.clone(), arg.val.clone());
@ -39,6 +38,10 @@ impl Mixin {
self self
} }
pub fn call(&mut self, super_selector: &Selector,) -> Result<Vec<Stmt>, (Pos, &'static str)> {
self.eval(super_selector, &mut self.scope.clone())
}
pub fn eval( pub fn eval(
&mut self, &mut self,
super_selector: &Selector, super_selector: &Selector,