handle multiline comments surrounding @ if condition

This commit is contained in:
ConnorSkees 2020-04-23 19:32:32 -04:00
parent 69764ceaa3
commit 409ac80921
4 changed files with 24 additions and 7 deletions

View File

@ -8,7 +8,7 @@ use crate::error::SassResult;
use crate::scope::Scope; use crate::scope::Scope;
use crate::selector::Selector; use crate::selector::Selector;
use crate::utils::{ use crate::utils::{
devour_whitespace, eat_ident, read_until_closing_curly_brace, read_until_open_curly_brace, devour_whitespace, eat_ident, read_until_closing_curly_brace, read_until_open_curly_brace, devour_whitespace_or_comment
}; };
use crate::value::Value; use crate::value::Value;
use crate::{Stmt, Token}; use crate::{Stmt, Token};
@ -35,10 +35,11 @@ impl If {
pub fn from_tokens<I: Iterator<Item = Token>>( pub fn from_tokens<I: Iterator<Item = Token>>(
toks: &mut PeekMoreIterator<I>, toks: &mut PeekMoreIterator<I>,
) -> SassResult<If> { ) -> SassResult<If> {
devour_whitespace_or_comment(toks)?;
let mut branches = Vec::new(); let mut branches = Vec::new();
let init_cond = read_until_open_curly_brace(toks); let init_cond = read_until_open_curly_brace(toks);
toks.next(); toks.next();
devour_whitespace(toks); devour_whitespace_or_comment(toks)?;
let mut init_toks = read_until_closing_curly_brace(toks); let mut init_toks = read_until_closing_curly_brace(toks);
init_toks.push(toks.next().unwrap()); init_toks.push(toks.next().unwrap());
devour_whitespace(toks); devour_whitespace(toks);

View File

@ -357,8 +357,7 @@ impl<'a> StyleSheetParser<'a> {
}); });
} }
v => { v => {
let pos = self.lexer.next().unwrap().pos(); let rule = AtRule::from_tokens(&v, span, &mut self.lexer, &mut Scope::new(), &Selector::new())?;
let rule = AtRule::from_tokens(&v, pos, &mut self.lexer, &mut Scope::new(), &Selector::new())?;
match rule.node { match rule.node {
AtRule::Mixin(name, mixin) => { AtRule::Mixin(name, mixin) => {
insert_global_mixin(&name, *mixin); insert_global_mixin(&name, *mixin);

View File

@ -40,13 +40,20 @@ pub(crate) fn devour_whitespace_or_comment<I: Iterator<Item = Token>>(
let mut found_whitespace = false; let mut found_whitespace = false;
while let Some(tok) = toks.peek() { while let Some(tok) = toks.peek() {
if tok.kind == '/' { if tok.kind == '/' {
let pos = toks.next().unwrap().pos(); let next = match toks.peek_forward(1) {
match toks.peek().unwrap().kind { Some(v) => v,
None => return Ok(found_whitespace),
};
match next.kind {
'*' => { '*' => {
toks.next();
eat_comment(toks, &Scope::new(), &Selector::new())?; eat_comment(toks, &Scope::new(), &Selector::new())?;
} }
'/' => read_until_newline(toks), '/' => read_until_newline(toks),
_ => return Err(("Expected expression.", pos).into()), _ => {
toks.reset_view();
return Ok(found_whitespace)
},
}; };
found_whitespace = true; found_whitespace = true;
continue; continue;

View File

@ -78,3 +78,13 @@ test!(
a { color: foo(bar); }", a { color: foo(bar); }",
"a {\n color: bar;\n}\n" "a {\n color: bar;\n}\n"
); );
test!(
multiline_comments_surrounding_condition_empty,
"@if/**/true/**/{ a { color: red; } }",
"a {\n color: red;\n}\n"
);
test!(
multiline_comments_surrounding_condition,
"@if/* pre 1 */true/* post 1 */{ a { color: red; } }",
"a {\n color: red;\n}\n"
);