From ab8e98244f2dd34b97279aa6efaa7078680414a5 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sat, 18 Jan 2020 09:03:38 -0500 Subject: [PATCH] Handle positional arguments to mixins --- src/main.rs | 19 +++++++++++++++++-- src/mixin.rs | 7 +++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1155cb9..7d72e61 100644 --- a/src/main.rs +++ b/src/main.rs @@ -384,8 +384,8 @@ fn eat_include>( return Err((pos, "expected identifier")); }; let rules = mixin - .call_with_args(&args) - .eval(super_selector, &mut scope.clone())?; + .args(&args) + .call(super_selector)?; 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", "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" + ); } diff --git a/src/mixin.rs b/src/mixin.rs index 5393c9e..4c64050 100644 --- a/src/mixin.rs +++ b/src/mixin.rs @@ -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() { if arg.is_named() { todo!("keyword args") } else { - // dbg!(&self.args.0[idx].name.clone()); self.scope .vars .insert(self.args.0[idx].name.clone(), arg.val.clone()); @@ -39,6 +38,10 @@ impl Mixin { self } + pub fn call(&mut self, super_selector: &Selector,) -> Result, (Pos, &'static str)> { + self.eval(super_selector, &mut self.scope.clone()) + } + pub fn eval( &mut self, super_selector: &Selector,