Handle comments inside call args

This commit is contained in:
ConnorSkees 2020-01-26 13:53:18 -05:00
parent 05eb03a38b
commit e2a487a59b
2 changed files with 48 additions and 5 deletions

View File

@ -2,7 +2,7 @@ use std::collections::BTreeMap;
use std::iter::Peekable;
use crate::common::{Scope, Symbol};
use crate::utils::devour_whitespace;
use crate::utils::{devour_whitespace, devour_whitespace_or_comment};
use crate::value::Value;
use crate::{Token, TokenKind};
@ -129,13 +129,13 @@ pub(crate) fn eat_call_args<I: Iterator<Item = Token>>(
scope: &Scope,
) -> CallArgs {
let mut args: BTreeMap<String, Value> = BTreeMap::new();
devour_whitespace(toks);
devour_whitespace_or_comment(toks);
let mut name: Option<String> = None;
let mut val = Vec::new();
while let Some(Token { kind, pos }) = toks.next() {
match kind {
TokenKind::Variable(v) => {
devour_whitespace(toks);
devour_whitespace_or_comment(toks);
match toks.peek() {
Some(Token {
kind: TokenKind::Symbol(Symbol::Colon),
@ -178,7 +178,7 @@ pub(crate) fn eat_call_args<I: Iterator<Item = Token>>(
}
}
TokenKind::Symbol(Symbol::Colon) => {
devour_whitespace(toks);
devour_whitespace_or_comment(toks);
while let Some(tok) = toks.peek() {
match &tok.kind {
TokenKind::Symbol(Symbol::Comma) => {
@ -240,7 +240,7 @@ pub(crate) fn eat_call_args<I: Iterator<Item = Token>>(
}
_ => val.push(Token { kind, pos }),
}
devour_whitespace(toks);
devour_whitespace_or_comment(toks);
}
CallArgs(args)
}

View File

@ -534,6 +534,11 @@ mod test_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_no_args,
"@mixin a {\n color: red;\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",
@ -609,6 +614,36 @@ mod test_mixins {
"@mixin a($a) {\n color: $a;\n}\nd {\n $c: red;\n @include a($c);\n}\n",
"d {\n color: red;\n}\n"
);
test!(
comment_before_positional_call_arg,
"@mixin a($a) {\n color: $a;\n}\nd {\n @include a(/*foo*/red);\n}\n",
"d {\n color: red;\n}\n"
);
test!(
comment_after_positional_call_arg,
"@mixin a($a) {\n color: $a;\n}\nd {\n @include a(red/*foo*/);\n}\n",
"d {\n color: red;\n}\n"
);
test!(
comment_before_keyword_call_arg_val,
"@mixin a($a) {\n color: $a;\n}\nd {\n @include a($a: /*foo*/red);\n}\n",
"d {\n color: red;\n}\n"
);
test!(
comment_after_keyword_call_arg_val,
"@mixin a($a) {\n color: $a;\n}\nd {\n @include a($a: red/*foo*/);\n}\n",
"d {\n color: red;\n}\n"
);
test!(
comment_before_keyword_call_arg_name,
"@mixin a($a) {\n color: $a;\n}\nd {\n @include a(/*foo*/$a: red);\n}\n",
"d {\n color: red;\n}\n"
);
test!(
comment_after_keyword_call_arg_name,
"@mixin a($a) {\n color: $a;\n}\nd {\n @include a($a/*foo*/: red);\n}\n",
"d {\n color: red;\n}\n"
);
}
#[cfg(test)]
@ -656,6 +691,14 @@ mod test_values {
test!(preserves_keyword_true, "a {\n color: true;\n}\n");
test!(preserves_keyword_false, "a {\n color: false;\n}\n");
test!(preserves_keyword_null, "a {\n color: null;\n}\n");
test!(preserves_keyword_auto, "a {\n color: auto;\n}\n");
test!(preserves_keyword_initial, "a {\n color: initial;\n}\n");
test!(preserves_keyword_infinity, "a {\n color: infinity;\n}\n");
test!(preserves_keyword_not, "a {\n color: not;\n}\n");
test!(preserves_keyword_and, "a {\n color: and;\n}\n");
test!(preserves_keyword_or, "a {\n color: or;\n}\n");
test!(preserves_keyword_unset, "a {\n color: unset;\n}\n");
test!(preserves_keyword_nan, "a {\n color: NaN;\n}\n");
test!(
whitespace_space_list_number,
"a {\n color: 1 2 3 ;\n}\n",