do not emit newline between media queries when they follow ruleset

This commit is contained in:
Connor Skees 2021-07-23 02:22:47 -04:00
parent 6b109a5c3d
commit 5c4f11e63d
2 changed files with 51 additions and 12 deletions

View File

@ -230,28 +230,48 @@ impl Css {
} }
fn parse_stylesheet(mut self, stmts: Vec<Stmt>) -> SassResult<Css> { fn parse_stylesheet(mut self, stmts: Vec<Stmt>) -> SassResult<Css> {
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
enum ToplevelKind {
Comment,
Media,
Other,
}
let mut is_first = true; let mut is_first = true;
let mut last_was_comment = false; let mut last_toplevel = ToplevelKind::Other;
for stmt in stmts { for stmt in stmts {
let v = self.parse_stmt(stmt)?; let v = self.parse_stmt(stmt)?;
// this is how we print newlines between unrelated styles // this is how we print newlines between unrelated styles
// it could probably be refactored // it could probably be refactored
if !v.is_empty() { if !v.is_empty() {
if matches!(v.first(), Some(Toplevel::MultilineComment(..))) { match v.first() {
if !last_was_comment && !is_first { Some(Toplevel::MultilineComment(..)) => {
if last_toplevel != ToplevelKind::Comment && !is_first {
self.blocks.push(Toplevel::Newline); self.blocks.push(Toplevel::Newline);
} }
last_was_comment = true; last_toplevel = ToplevelKind::Comment;
} else if is_first { }
last_was_comment = false; Some(Toplevel::Media { .. }) => {
} else { if last_toplevel != ToplevelKind::Media && !is_first {
if !last_was_comment {
self.blocks.push(Toplevel::Newline); self.blocks.push(Toplevel::Newline);
} }
last_was_comment = false; last_toplevel = ToplevelKind::Media;
} }
_ if is_first => {
last_toplevel = ToplevelKind::Other;
}
_ => {
if last_toplevel != ToplevelKind::Comment {
self.blocks.push(Toplevel::Newline);
}
last_toplevel = ToplevelKind::Other;
}
}
is_first = false; is_first = false;
self.blocks.extend(v); self.blocks.extend(v);
} }

View File

@ -170,6 +170,25 @@ test!(
}", }",
"@media (true) {\n a {\n color: red;\n }\n}\n" "@media (true) {\n a {\n color: red;\n }\n}\n"
); );
test!(
no_newline_between_two_media_following_ruleset,
"a {
color: red;
}
@media (min-width: 0px) {
a {
color: red;
}
}
@media (min-width: 0px) {
a {
color: red;
}
}",
"a {\n color: red;\n}\n\n@media (min-width: 0px) {\n a {\n color: red;\n }\n}\n@media (min-width: 0px) {\n a {\n color: red;\n }\n}\n"
);
error!( error!(
media_feature_missing_closing_paren, media_feature_missing_closing_paren,