From 03f48cfd22d27b021ac67fdf6a2994a769601fe8 Mon Sep 17 00:00:00 2001 From: Connor Skees Date: Mon, 12 Jul 2021 02:22:13 -0400 Subject: [PATCH] more robustly parse comments inside min and max --- src/parse/value/css_function.rs | 23 +++++++++++++++-------- tests/min-max.rs | 16 ++++++++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/parse/value/css_function.rs b/src/parse/value/css_function.rs index 0a3260e..7a13d81 100644 --- a/src/parse/value/css_function.rs +++ b/src/parse/value/css_function.rs @@ -4,6 +4,7 @@ use codemap::Spanned; use crate::{ error::SassResult, + parse::common::Comment, utils::{ as_hex, hex_char_for, is_name, peek_until_closing_curly_brace, peek_whitespace, IsWhitespace, @@ -145,7 +146,7 @@ impl<'a> Parser<'a> { String::new() }; - self.whitespace(); + self.whitespace_or_comment(); while let Some(tok) = self.toks.peek() { let kind = tok.kind; @@ -240,7 +241,7 @@ impl<'a> Parser<'a> { _ => return Ok(None), } - self.whitespace(); + self.whitespace_or_comment(); let next = match self.toks.peek() { Some(tok) => tok, @@ -270,7 +271,7 @@ impl<'a> Parser<'a> { _ => return Ok(None), } - self.whitespace(); + self.whitespace_or_comment(); } Ok(Some(buf)) @@ -332,7 +333,16 @@ impl<'a> Parser<'a> { } '/' => { if matches!(self.toks.peek_n(1), Some(Token { kind: '*', .. })) { - todo!() + self.toks.next(); + + let comment = match self.parse_comment()?.node { + Comment::Loud(s) => s, + Comment::Silent => continue, + }; + + buffer.push_str("/*"); + buffer.push_str(&comment); + buffer.push_str("*/"); } else { buffer.push('/'); self.toks.next(); @@ -353,10 +363,7 @@ impl<'a> Parser<'a> { } c @ (' ' | '\t') => { if wrote_newline - || !self - .toks - .peek_n(1) - .map_or(false, |tok| tok.is_whitespace()) + || !self.toks.peek_n(1).map_or(false, |tok| tok.is_whitespace()) { buffer.push(c); } diff --git a/tests/min-max.rs b/tests/min-max.rs index 2c6725d..fb14f3d 100644 --- a/tests/min-max.rs +++ b/tests/min-max.rs @@ -170,3 +170,19 @@ test!( "a {\n color: min(1, var(--foo));\n}\n", "a {\n color: min(1, var(--foo));\n}\n" ); +test!( + min_conains_multiline_comment, + "a {\n color: min(1/**/);\n}\n", + "a {\n color: min(1);\n}\n" +); +test!( + min_conains_calc_contains_multiline_comment, + "a {\n color: min(calc(1 /**/ 2));\n}\n", + "a {\n color: min(calc(1 /**/ 2));\n}\n" +); +test!( + #[ignore = "we currently resolve interpolation eagerly inside loud comments"] + min_conains_calc_contains_multiline_comment_with_interpolation, + "a {\n color: min(calc(1 /* #{5} */ 2));\n}\n", + "a {\n color: min(calc(1 /* #{5} */ 2));\n}\n" +);