From 409ac8092194fdeb77ff5eda5850bd20605203b7 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Thu, 23 Apr 2020 19:32:32 -0400 Subject: [PATCH] handle multiline comments surrounding @ if condition --- src/atrule/if_rule.rs | 5 +++-- src/lib.rs | 3 +-- src/utils/comment_whitespace.rs | 13 ++++++++++--- tests/if.rs | 10 ++++++++++ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/atrule/if_rule.rs b/src/atrule/if_rule.rs index 4bd814c..f08bcbb 100644 --- a/src/atrule/if_rule.rs +++ b/src/atrule/if_rule.rs @@ -8,7 +8,7 @@ use crate::error::SassResult; use crate::scope::Scope; use crate::selector::Selector; 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::{Stmt, Token}; @@ -35,10 +35,11 @@ impl If { pub fn from_tokens>( toks: &mut PeekMoreIterator, ) -> SassResult { + devour_whitespace_or_comment(toks)?; let mut branches = Vec::new(); let init_cond = read_until_open_curly_brace(toks); toks.next(); - devour_whitespace(toks); + devour_whitespace_or_comment(toks)?; let mut init_toks = read_until_closing_curly_brace(toks); init_toks.push(toks.next().unwrap()); devour_whitespace(toks); diff --git a/src/lib.rs b/src/lib.rs index d38d28c..4a97cae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -357,8 +357,7 @@ impl<'a> StyleSheetParser<'a> { }); } v => { - let pos = self.lexer.next().unwrap().pos(); - let rule = AtRule::from_tokens(&v, pos, &mut self.lexer, &mut Scope::new(), &Selector::new())?; + let rule = AtRule::from_tokens(&v, span, &mut self.lexer, &mut Scope::new(), &Selector::new())?; match rule.node { AtRule::Mixin(name, mixin) => { insert_global_mixin(&name, *mixin); diff --git a/src/utils/comment_whitespace.rs b/src/utils/comment_whitespace.rs index 1e62ce8..97ca64e 100644 --- a/src/utils/comment_whitespace.rs +++ b/src/utils/comment_whitespace.rs @@ -40,13 +40,20 @@ pub(crate) fn devour_whitespace_or_comment>( let mut found_whitespace = false; while let Some(tok) = toks.peek() { if tok.kind == '/' { - let pos = toks.next().unwrap().pos(); - match toks.peek().unwrap().kind { + let next = match toks.peek_forward(1) { + Some(v) => v, + None => return Ok(found_whitespace), + }; + match next.kind { '*' => { + toks.next(); eat_comment(toks, &Scope::new(), &Selector::new())?; } '/' => read_until_newline(toks), - _ => return Err(("Expected expression.", pos).into()), + _ => { + toks.reset_view(); + return Ok(found_whitespace) + }, }; found_whitespace = true; continue; diff --git a/tests/if.rs b/tests/if.rs index 06fe284..3355463 100644 --- a/tests/if.rs +++ b/tests/if.rs @@ -78,3 +78,13 @@ test!( a { color: foo(bar); }", "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" +);