better handle silent comments in maps and fn args

This commit is contained in:
Connor Skees 2020-07-27 22:09:38 -04:00
parent dee6699bde
commit f69b863e33
5 changed files with 42 additions and 5 deletions

View File

@ -48,6 +48,7 @@ impl<'a> Parser<'a> {
match &tok.kind {
',' => {
self.toks.next();
self.whitespace_or_comment();
args.push(FuncArg {
name: name.node.into(),
default: Some(default),

View File

@ -1062,9 +1062,16 @@ impl<'a, 'b: 'a> IntermediateValueIterator<'a, 'b> {
let paren_toks = &mut t.node.into_iter().peekmore();
let mut map = SassMap::new();
let key = self
.parser
.parse_value_from_vec(read_until_char(paren_toks, ':')?, true)?;
let key_toks = read_until_char(paren_toks, ':')?;
if key_toks.iter().all(|t| t.is_whitespace()) {
return Ok(Spanned {
node: HigherIntermediateValue::Paren(Box::new(HigherIntermediateValue::Literal(
Value::Map(map),
))),
span: t.span,
});
}
let key = self.parser.parse_value_from_vec(key_toks, true)?;
if paren_toks.peek().is_none() {
return Ok(Spanned {
@ -1096,13 +1103,14 @@ impl<'a, 'b: 'a> IntermediateValueIterator<'a, 'b> {
let key = self
.parser
.parse_value_from_vec(read_until_char(paren_toks, ':')?, true)?;
devour_whitespace(paren_toks);
let val = self
.parser
.parse_value_from_vec(read_until_char(paren_toks, ',')?, true)?;
span = span.merge(val.span);
devour_whitespace(paren_toks);
if map.insert(key.node, val.node) {
if map.insert(key.node.clone(), val.node) {
return Err(("Duplicate key.", key.span).into());
}
if paren_toks.peek().is_none() {

View File

@ -4,7 +4,7 @@ use peekmore::PeekMoreIterator;
use crate::{error::SassResult, Token};
use super::{read_until_closing_paren, read_until_closing_quote};
use super::{read_until_closing_paren, read_until_closing_quote, read_until_newline};
/// Reads until the char is found, consuming the char,
/// or until the end of the iterator is hit
pub(crate) fn read_until_char(
@ -24,6 +24,13 @@ pub(crate) fn read_until_char(
v.extend(read_until_closing_paren(toks)?);
continue;
}
'/' => {
match toks.peek() {
Some(Token { kind: '/', .. }) => read_until_newline(toks),
_ => v.push(tok),
};
continue;
}
t if t == c => break,
_ => {}
}

View File

@ -86,3 +86,16 @@ test!(
}",
"a {\n color: red;\n}\n"
);
test!(
comment_after_comma_in_func_args,
"@mixin a(
$foo,//foo
) {
color: $foo;
}
a {
@include a(red);
}",
"a {\n color: red;\n}\n"
);

View File

@ -197,3 +197,11 @@ test!(
"a {\n color: (a: b)==(a: c);\n}\n",
"a {\n color: false;\n}\n"
);
test!(
empty_with_single_line_comments,
"$foo: (\n \n // :/a.b\n \n );
a {
color: inspect($foo);
}",
"a {\n color: ();\n}\n"
);