Implement keyword arguments in mixins

This commit is contained in:
ConnorSkees 2020-01-19 11:50:30 -05:00
parent 31506c7ce4
commit 1f1f43bf7c
2 changed files with 66 additions and 10 deletions

View File

@ -133,13 +133,44 @@ pub fn eat_call_args<I: Iterator<Item = Token>>(toks: &mut Peekable<I>) -> CallA
match kind {
TokenKind::Variable(v) => name = Some(v),
TokenKind::Symbol(Symbol::Colon) => {
todo!("handle default values")
// let mut val: Vec<Token> = Vec::new();
// while let Some(Token { kind, .. }) = toks.next() {
// match &kind {
// _ => {}
// }
// }
devour_whitespace(toks);
while let Some(tok) = toks.peek() {
match &tok.kind {
TokenKind::Symbol(Symbol::Comma) => {
toks.next();
args.insert(
name.clone().unwrap(),
CallArg {
name: name.clone(),
val: val.clone(),
},
);
if let Some(ref mut s) = name {
s.clear();
}
val.clear();
break;
}
TokenKind::Symbol(Symbol::CloseParen) => {
args.insert(
name.clone().unwrap(),
CallArg {
name: name.clone(),
val: val.clone(),
},
);
if let Some(ref mut s) = name {
s.clear();
}
val.clear();
break;
}
_ => {
let tok = toks.next().expect("we know this exists!");
val.push(tok)
}
}
}
}
TokenKind::Symbol(Symbol::CloseParen) => {
if name.is_some() {
@ -150,11 +181,11 @@ pub fn eat_call_args<I: Iterator<Item = Token>>(toks: &mut Peekable<I>) -> CallA
break;
}
TokenKind::Symbol(Symbol::Comma) => {
if name.is_some() {
if let Some(ref name) = name {
args.insert(
name.clone().unwrap(),
name.clone(),
CallArg {
name: name.clone(),
name: Some(name.clone()),
val: val.clone(),
},
);

View File

@ -1154,6 +1154,31 @@ mod test_mixins {
"@mixin a($a: red) {\n color: $a;\n}\nd {\n @include a(blue);\n}\n",
"d {\n color: blue;\n}\n"
);
test!(
mixin_keyword_arg,
"@mixin a($a) {\n color: $a;\n}\nd {\n @include a($a: blue);\n}\n",
"d {\n color: blue;\n}\n"
);
test!(
mixin_keyword_arg_override_default,
"@mixin a($a: red) {\n color: $a;\n}\nd {\n @include a($a: blue);\n}\n",
"d {\n color: blue;\n}\n"
);
test!(
mixin_keyword_applies_to_second_arg,
"@mixin a($a: red, $b) {\n color: $a $b;\n}\nd {\n @include a($b: blue);\n}\n",
"d {\n color: red blue;\n}\n"
);
test!(
mixin_two_keywords,
"@mixin a($a, $b) {\n color: $a $b;\n}\nd {\n @include a($a: red, $b: blue);\n}\n",
"d {\n color: red blue;\n}\n"
);
test!(
mixin_two_keywords_wrong_direction,
"@mixin a($a, $b) {\n color: $a $b;\n}\nd {\n @include a($b: blue, $a: red);\n}\n",
"d {\n color: red blue;\n}\n"
);
}
#[cfg(test)]