diff --git a/src/output.rs b/src/output.rs index 2541521..a36a9a6 100644 --- a/src/output.rs +++ b/src/output.rs @@ -231,17 +231,28 @@ impl Css { fn parse_stylesheet(mut self, stmts: Vec) -> SassResult { let mut is_first = true; + let mut last_was_comment = false; for stmt in stmts { let v = self.parse_stmt(stmt)?; // this is how we print newlines between unrelated styles // it could probably be refactored if !v.is_empty() { - if let Some(Toplevel::MultilineComment(..)) = v.first() { + if matches!(v.first(), Some(Toplevel::MultilineComment(..))) { + if !last_was_comment && !is_first { + self.blocks.push(Toplevel::Newline); + } + + last_was_comment = true; } else if is_first { - is_first = false; + last_was_comment = false; } else { - self.blocks.push(Toplevel::Newline); + if !last_was_comment { + self.blocks.push(Toplevel::Newline); + } + + last_was_comment = false; } + is_first = false; self.blocks.extend(v); } } @@ -502,7 +513,14 @@ impl Formatter for ExpandedFormatter { writeln!(buf, "{}}}", padding)?; } Toplevel::MultilineComment(s) => { + if has_written && should_emit_newline { + writeln!(buf)?; + } + has_written = true; + + should_emit_newline = false; + writeln!(buf, "{}/*{}*/", padding, s)?; } Toplevel::Import(s) => { diff --git a/tests/comments.rs b/tests/comments.rs index 275e165..88a84ba 100644 --- a/tests/comments.rs +++ b/tests/comments.rs @@ -34,7 +34,7 @@ test!( test!( preserves_toplevel_comment_after, "a {\n color: red;\n}\n/* foo */\n", - "a {\n color: red;\n}\n/* foo */\n" + "a {\n color: red;\n}\n\n/* foo */\n" ); test!( removes_single_line_comment, @@ -79,3 +79,22 @@ test!( a /** */ b {x: y}", "a b {\n x: y;\n}\n\na b {\n x: y;\n}\n\na b {\n x: y;\n}\n\na b {\n x: y;\n}\n" ); +test!( + comment_has_newline_above_and_not_below_when_between_two_rulesets, + r"a { + color: red; + } + + /**/ + + a { + color: green; + }", + "a {\n color: red;\n}\n\n/**/\na {\n color: green;\n}\n" +); +test!( + no_extra_newline_between_two_comments, + r"/**/ + /**/", + "/**/\n/**/\n" +); diff --git a/tests/each.rs b/tests/each.rs index 77eafa8..ca0474a 100644 --- a/tests/each.rs +++ b/tests/each.rs @@ -84,7 +84,7 @@ test!( color: $b; } } /**/ ", - "/**/\n/**/\na {\n color: a;\n}\n/**/\n\na {\n color: b;\n}\n/**/\n" + "/**/\n/**/\na {\n color: a;\n}\n\n/**/\na {\n color: b;\n}\n\n/**/\n" ); error!( list_of_single_map,