refactor how newlines are emitted

still not perfect, but getting closer. this brings the bulma diff down
from around 1,700 lines to only 500 (all a result of newlines)
This commit is contained in:
Connor Skees 2020-07-04 22:46:28 -04:00
parent 379eeb4fd3
commit 91ef5dcfd5
4 changed files with 44 additions and 20 deletions

View File

@ -160,7 +160,7 @@ pub fn from_path(p: &str) -> Result<String> {
.parse()
.map_err(|e| raw_to_parse_error(&map, *e))?;
Css::from_stmts(stmts)
Css::from_stmts(stmts, false)
.map_err(|e| raw_to_parse_error(&map, *e))?
.pretty_print(&map)
.map_err(|e| raw_to_parse_error(&map, *e))
@ -205,7 +205,7 @@ pub fn from_string(p: String) -> Result<String> {
.parse()
.map_err(|e| raw_to_parse_error(&map, *e))?;
Css::from_stmts(stmts)
Css::from_stmts(stmts, false)
.map_err(|e| raw_to_parse_error(&map, *e))?
.pretty_print(&map)
.map_err(|e| raw_to_parse_error(&map, *e))
@ -241,7 +241,7 @@ pub fn from_string(p: String) -> std::result::Result<String, JsValue> {
.parse()
.map_err(|e| raw_to_parse_error(&map, *e).to_string())?;
Ok(Css::from_stmts(stmts)
Ok(Css::from_stmts(stmts, false)
.map_err(|e| raw_to_parse_error(&map, *e).to_string())?
.pretty_print(&map)
.map_err(|e| raw_to_parse_error(&map, *e).to_string())?)

View File

@ -82,15 +82,19 @@ impl Toplevel {
#[derive(Debug, Clone)]
pub(crate) struct Css {
blocks: Vec<Toplevel>,
in_at_rule: bool,
}
impl Css {
pub const fn new() -> Self {
Css { blocks: Vec::new() }
pub const fn new(in_at_rule: bool) -> Self {
Css {
blocks: Vec::new(),
in_at_rule,
}
}
pub(crate) fn from_stmts(s: Vec<Stmt>) -> SassResult<Self> {
Css::new().parse_stylesheet(s)
pub(crate) fn from_stmts(s: Vec<Stmt>, in_at_rule: bool) -> SassResult<Self> {
Css::new(in_at_rule).parse_stylesheet(s)
}
fn parse_stmt(&mut self, stmt: Stmt) -> SassResult<Vec<Toplevel>> {
@ -232,7 +236,7 @@ impl Css {
continue;
}
has_written = true;
if should_emit_newline {
if should_emit_newline && !self.in_at_rule {
should_emit_newline = false;
writeln!(buf)?;
}
@ -247,10 +251,7 @@ impl Css {
continue;
}
has_written = true;
if should_emit_newline {
should_emit_newline = false;
writeln!(buf)?;
}
writeln!(
buf,
"{}{} {{",
@ -290,7 +291,7 @@ impl Css {
writeln!(buf, " {{")?;
}
Css::from_stmts(body)?._inner_pretty_print(buf, map, nesting + 1)?;
Css::from_stmts(body, true)?._inner_pretty_print(buf, map, nesting + 1)?;
writeln!(buf, "{}}}", padding)?;
}
Toplevel::Keyframes(k) => {
@ -313,7 +314,7 @@ impl Css {
writeln!(buf, " {{")?;
}
Css::from_stmts(body)?._inner_pretty_print(buf, map, nesting + 1)?;
Css::from_stmts(body, true)?._inner_pretty_print(buf, map, nesting + 1)?;
writeln!(buf, "{}}}", padding)?;
}
Toplevel::Supports { params, body } => {
@ -335,19 +336,16 @@ impl Css {
writeln!(buf, " {{")?;
}
Css::from_stmts(body)?._inner_pretty_print(buf, map, nesting + 1)?;
Css::from_stmts(body, true)?._inner_pretty_print(buf, map, nesting + 1)?;
writeln!(buf, "{}}}", padding)?;
}
Toplevel::Media { query, body } => {
if body.is_empty() {
continue;
}
if should_emit_newline {
should_emit_newline = false;
writeln!(buf)?;
}
writeln!(buf, "{}@media {} {{", padding, query)?;
Css::from_stmts(body)?._inner_pretty_print(buf, map, nesting + 1)?;
Css::from_stmts(body, true)?._inner_pretty_print(buf, map, nesting + 1)?;
writeln!(buf, "{}}}", padding)?;
}
Toplevel::Style(s) => {

View File

@ -109,3 +109,15 @@ test!(
"@keyframes foo {/**/}",
"@keyframes foo {\n /**/\n}\n"
);
test!(
keyframes_multiple_rulesets,
"@keyframes {
to {
color: red;
}
from {
color: green;
}
}",
"@keyframes {\n to {\n color: red;\n }\n from {\n color: green;\n }\n}\n"
);

View File

@ -17,3 +17,17 @@ test!(
"@media foo {\n a {\n color: red;\n }\n}\n"
);
test!(empty_body, "@media (min-width: 2px) {}", "");
test!(
newlines_are_not_emitted_for_child_styles,
"a {
@media screen {
b {
color: red;
}
c {
color: green;
}
}
}",
"@media screen {\n a b {\n color: red;\n }\n a c {\n color: green;\n }\n}\n"
);