Skip to content

Commit

Permalink
refactor(css/parser): Refactor more (#6251)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Oct 26, 2022
1 parent 3916886 commit a0e4684
Show file tree
Hide file tree
Showing 111 changed files with 6,250 additions and 3,087 deletions.
1 change: 1 addition & 0 deletions crates/swc_css_codegen/src/ctx.rs
Expand Up @@ -22,6 +22,7 @@ where
pub(crate) struct Ctx {
pub in_single_line_selectors: bool,
pub allow_to_lowercase: bool,
pub in_list_of_component_values: bool,
}

pub(super) struct WithCtx<'w, I: 'w + CssWriter> {
Expand Down
76 changes: 68 additions & 8 deletions crates/swc_css_codegen/src/lib.rs
Expand Up @@ -68,7 +68,15 @@ where
match n {
Rule::QualifiedRule(n) => emit!(self, n),
Rule::AtRule(n) => emit!(self, n),
Rule::ListOfComponentValues(n) => emit!(self, n),
Rule::ListOfComponentValues(n) => {
emit!(
&mut *self.with_ctx(Ctx {
in_list_of_component_values: true,
..self.ctx
}),
n
)
}
}
}

Expand All @@ -81,11 +89,19 @@ where
#[emitter]
fn emit_qualified_rule_prelude(&mut self, n: &QualifiedRulePrelude) -> Result {
match n {
QualifiedRulePrelude::ListOfComponentValues(n) => emit!(self, n),
QualifiedRulePrelude::SelectorList(n) => {
emit!(self, n);
formatting_space!(self);
}
QualifiedRulePrelude::ListOfComponentValues(n) => {
emit!(
&mut *self.with_ctx(Ctx {
in_list_of_component_values: true,
..self.ctx
}),
n
)
}
}
}

Expand Down Expand Up @@ -135,7 +151,6 @@ where
#[emitter]
fn emit_at_rule_prelude(&mut self, n: &AtRulePrelude) -> Result {
match n {
AtRulePrelude::ListOfComponentValues(n) => emit!(self, n),
AtRulePrelude::CharsetPrelude(n) => {
space!(self);
emit!(self, n);
Expand Down Expand Up @@ -295,6 +310,15 @@ where
space!(self);
emit!(self, n);
}
AtRulePrelude::ListOfComponentValues(n) => {
emit!(
&mut *self.with_ctx(Ctx {
in_list_of_component_values: true,
..self.ctx
}),
n
)
}
}
}

Expand Down Expand Up @@ -1118,6 +1142,10 @@ where
for (idx, node) in iter.enumerate() {
emit!(self, node);

if self.ctx.in_list_of_component_values {
continue;
}

let is_current_preserved_token = matches!(node, ComponentValue::PreservedToken(_));
let next = nodes.get(idx + 1);
let is_next_preserved_token = matches!(next, Some(ComponentValue::PreservedToken(_)));
Expand Down Expand Up @@ -1333,7 +1361,7 @@ where
decrease_indent!(self);
}
_ => {
if ending == "]" && idx != len - 1 {
if !self.ctx.in_list_of_component_values && ending == "]" && idx != len - 1 {
space!(self);
}
}
Expand Down Expand Up @@ -1380,7 +1408,15 @@ where
#[emitter]
fn emit_style_block(&mut self, n: &StyleBlock) -> Result {
match n {
StyleBlock::ListOfComponentValues(n) => emit!(self, n),
StyleBlock::ListOfComponentValues(n) => {
emit!(
&mut *self.with_ctx(Ctx {
in_list_of_component_values: true,
..self.ctx
}),
n
)
}
StyleBlock::AtRule(n) => emit!(self, n),
StyleBlock::Declaration(n) => emit!(self, n),
StyleBlock::QualifiedRule(n) => emit!(self, n),
Expand All @@ -1392,7 +1428,15 @@ where
match n {
DeclarationOrAtRule::Declaration(n) => emit!(self, n),
DeclarationOrAtRule::AtRule(n) => emit!(self, n),
DeclarationOrAtRule::ListOfComponentValues(n) => emit!(self, n),
DeclarationOrAtRule::ListOfComponentValues(n) => {
emit!(
&mut *self.with_ctx(Ctx {
in_list_of_component_values: true,
..self.ctx
}),
n
)
}
}
}

Expand Down Expand Up @@ -2137,7 +2181,15 @@ where
fn emit_forgiving_complex_list(&mut self, n: &ForgivingComplexSelector) -> Result {
match n {
ForgivingComplexSelector::ComplexSelector(n) => emit!(self, n),
ForgivingComplexSelector::ListOfComponentValues(n) => emit!(self, n),
ForgivingComplexSelector::ListOfComponentValues(n) => {
emit!(
&mut *self.with_ctx(Ctx {
in_list_of_component_values: true,
..self.ctx
}),
n
)
}
}
}

Expand Down Expand Up @@ -2175,7 +2227,15 @@ where
fn emit_forgiving_relative_selector(&mut self, n: &ForgivingRelativeSelector) -> Result {
match n {
ForgivingRelativeSelector::RelativeSelector(n) => emit!(self, n),
ForgivingRelativeSelector::ListOfComponentValues(n) => emit!(self, n),
ForgivingRelativeSelector::ListOfComponentValues(n) => {
emit!(
&mut *self.with_ctx(Ctx {
in_list_of_component_values: true,
..self.ctx
}),
n
)
}
}
}

Expand Down
Expand Up @@ -23,9 +23,9 @@ $bad: rule;
a {
div.major { color: blue } color: red }
a {
div: hover {color:blue} color:red;
div: hover { color: blue } color:red;
}
a {
div: hover {color:blue};
div: hover { color: blue };
color: red;
}
@@ -1,4 +1,4 @@
{}{color:red}#broken# {color:red}$bad{color:red}$bad {color:red}$bad foo{color:red}$bad foo {color:red}$bad: rule;


a {div.major { color: blue } color: red }a{div:hover{color:blue}color:red}a{div:hover{color:blue};color:red}
a {div.major { color: blue } color: red }a{div:hover{ color: blue }color:red}a{div:hover{ color: blue };color:red}
Expand Up @@ -3,12 +3,10 @@
.selector:not(*:root) {}
@supports (-webkit-appearance: none) {}
.selector {
(;property: value;
);}
(;property: value;);}
.selector {
[;property: value;
];}
@media screen {}
[;property: value;];}
@media \\0 screen {}
@media all and (-webkit-min-device-pixel-ratio: 0) and (min-resolution: .001dpcm) {
.selector {}
}
Expand Down Expand Up @@ -153,11 +151,9 @@ _:-ms-fullscreen,
.selector:not(*:root) {}
@supports (-webkit-appearance: none) {}
.selector {
(;property: value;
);}
(;property: value;);}
.selector {
[;property: value;
];}
[;property: value;];}
html:first-child .selector {}
_:-o-prefocus,
body:last-child .selector {}
Expand All @@ -173,11 +169,9 @@ body:last-child .selector {}
.selector:not(*:root) {}
@supports (-webkit-appearance: none) {}
.selector {
(;property: value;
);}
(;property: value;);}
.selector {
[;property: value;
];}
[;property: value;];}
@media screen and (min-width: 0\0) {}
@media screen {
@media (min-width: 0px) {}
Expand All @@ -188,7 +182,7 @@ html[xmlns*=""]:root .selector {}
*|html[xmlns*=""] .selector {}
_::-moz-svg-foreign-content,
:root .selector {}
@media screen {}
@media \\0 screen {}
a {
*color : black;_background: white;
font-size: big;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -14,13 +14,13 @@
unknown: x {a,b};
}
* {
unknown: x (a+b);
unknown: x ( a + b );
}
* {
unknown: x (a/b);
}
* {
unknown: x (a*b);
unknown: x ( a * b );
}
* {
unknown: x (a,b);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -1,10 +1,4 @@

x Unexpected duplicate '@import' rule 'a.css'.
,-[$DIR/tests/rules/fail/no-duplicate-at-import-rules/11/input.css:2:1]
2 | @import 'a.css' layer();
: ^^^^^^^^^^^^^^^^^^^^^^^^
`----

x Unexpected duplicate '@import' rule 'a.css'.
,-[$DIR/tests/rules/fail/no-duplicate-at-import-rules/11/input.css:3:1]
3 | @import 'a.css' layer(default);
Expand Down
Expand Up @@ -17,6 +17,8 @@
@import '';
@import "";
@import " ";
@import "
";
@import url();
@import url('');
@import url("");
Expand Down Expand Up @@ -136,7 +138,7 @@ st.css');
@import url("./test.css") layer(default) supports(display: flex) screen and (min-width: 400px);
@import url("./test.css") layer supports(display: flex) screen and (min-width: 400px);
@import url("./test.css") layer() supports(display: flex) screen and (min-width: 400px);
@import url("./test.css");
@import url("./test.css") layer();
@import url("http://example.com/style.css") supports(display: flex) screen and (min-width: 400px);
@import url("./test.css") layer(default) supports(display: flex) screen and (min-width: 400px);
@import url("./test.css") screen and (min-width: 400px);
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_css_parser/src/error.rs
Expand Up @@ -39,6 +39,9 @@ impl Error {
ErrorKind::UnexpectedCharInUrl => "Unexpected character in url".into(),

// Parser
ErrorKind::EofButExpected(s) => {
format!("Unexpected end of file, but expected {}", s).into()
}
ErrorKind::Ignore => "Not an error".into(),
ErrorKind::UnexpectedChar(c) => format!("Unexpected character `{:?}`", c).into(),
ErrorKind::Expected(s) => format!("Expected {}", s).into(),
Expand Down Expand Up @@ -90,6 +93,7 @@ pub enum ErrorKind {

// Parser errors
Ignore,
EofButExpected(&'static str),
UnexpectedChar(char),
Expected(&'static str),
Unexpected(&'static str),
Expand Down

1 comment on commit a0e4684

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: a0e4684 Previous: 3916886 Ratio
es/full/bugs-1 423171 ns/iter (± 40365) 405871 ns/iter (± 14176) 1.04
es/full/minify/libraries/antd 2159277611 ns/iter (± 33363518) 2023486921 ns/iter (± 25377117) 1.07
es/full/minify/libraries/d3 439239820 ns/iter (± 23784604) 414963053 ns/iter (± 10893065) 1.06
es/full/minify/libraries/echarts 1837609783 ns/iter (± 32915958) 1787257953 ns/iter (± 20413257) 1.03
es/full/minify/libraries/jquery 120823898 ns/iter (± 5756160) 116802697 ns/iter (± 1713242) 1.03
es/full/minify/libraries/lodash 138260676 ns/iter (± 1125983) 139245863 ns/iter (± 2242792) 0.99
es/full/minify/libraries/moment 69381540 ns/iter (± 1289694) 69687420 ns/iter (± 1084309) 1.00
es/full/minify/libraries/react 23626803 ns/iter (± 1164785) 24017548 ns/iter (± 1045510) 0.98
es/full/minify/libraries/terser 306142641 ns/iter (± 12452350) 312832431 ns/iter (± 11355277) 0.98
es/full/minify/libraries/three 606018658 ns/iter (± 23680751) 547156300 ns/iter (± 19126790) 1.11
es/full/minify/libraries/typescript 4001201964 ns/iter (± 42105909) 3899631157 ns/iter (± 32533162) 1.03
es/full/minify/libraries/victory 889313618 ns/iter (± 23688978) 867308704 ns/iter (± 17558100) 1.03
es/full/minify/libraries/vue 174346312 ns/iter (± 4623275) 175362466 ns/iter (± 3644205) 0.99
es/full/codegen/es3 40451 ns/iter (± 1459) 40309 ns/iter (± 841) 1.00
es/full/codegen/es5 40088 ns/iter (± 1399) 40232 ns/iter (± 1257) 1.00
es/full/codegen/es2015 38133 ns/iter (± 1501) 40275 ns/iter (± 2425) 0.95
es/full/codegen/es2016 39781 ns/iter (± 1659) 40011 ns/iter (± 732) 0.99
es/full/codegen/es2017 38695 ns/iter (± 2052) 39772 ns/iter (± 918) 0.97
es/full/codegen/es2018 39416 ns/iter (± 1487) 40171 ns/iter (± 1751) 0.98
es/full/codegen/es2019 40357 ns/iter (± 650) 40488 ns/iter (± 2163) 1.00
es/full/codegen/es2020 40372 ns/iter (± 997) 38678 ns/iter (± 1121) 1.04
es/full/all/es3 228747794 ns/iter (± 4403640) 232327301 ns/iter (± 5039625) 0.98
es/full/all/es5 215166200 ns/iter (± 9813116) 218065155 ns/iter (± 4964212) 0.99
es/full/all/es2015 173900740 ns/iter (± 5044383) 176394299 ns/iter (± 3434344) 0.99
es/full/all/es2016 174255811 ns/iter (± 3834220) 174999894 ns/iter (± 5544099) 1.00
es/full/all/es2017 173362323 ns/iter (± 4217673) 173408345 ns/iter (± 3253477) 1.00
es/full/all/es2018 171855658 ns/iter (± 3858775) 173189694 ns/iter (± 3534387) 0.99
es/full/all/es2019 171257898 ns/iter (± 3843774) 172093035 ns/iter (± 2912166) 1.00
es/full/all/es2020 165515258 ns/iter (± 9177759) 166870309 ns/iter (± 5854280) 0.99
es/full/parser 872459 ns/iter (± 20144) 864223 ns/iter (± 29977) 1.01
es/full/base/fixer 31285 ns/iter (± 736) 31720 ns/iter (± 1840) 0.99
es/full/base/resolver_and_hygiene 113609 ns/iter (± 4000) 111480 ns/iter (± 2623) 1.02
serialization of ast node 260 ns/iter (± 7) 257 ns/iter (± 13) 1.01
serialization of serde 275 ns/iter (± 4) 260 ns/iter (± 4) 1.06

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.