Skip to content

Commit

Permalink
refactor(css/parser): Use new approach for parsing invalid tokens (#6224
Browse files Browse the repository at this point in the history
)

Co-authored-by: Donny/강동윤 <kdy1997.dev@gmail.com>
  • Loading branch information
alexander-akait and kdy1 committed Oct 22, 2022
1 parent e2c1fcf commit ac619e8
Show file tree
Hide file tree
Showing 47 changed files with 1,433 additions and 1,200 deletions.
@@ -0,0 +1,7 @@
a {};

b {};

c { }

c {color: red; }
@@ -0,0 +1,10 @@
a {}
;

b {}
;

c {}
c {
color: red;
}
@@ -0,0 +1,5 @@
a{};

b {};

c {}c{color:red}
Expand Up @@ -17,8 +17,6 @@ $bad foo { color: red }

$bad: rule;

/* TODO: FIX ME */
/* a {}; b {}; */

a { div.major { color: blue } color: red }
a { div:hover { color: blue } color: red }
Expand Down
Expand Up @@ -18,9 +18,10 @@ $bad foo {
color: red;
}
$bad: rule;


a {
div.major { color: blue }
color: red }
div.major { color: blue } color: red }
a {
div: hover {color:blue} color:red;
}
Expand Down
@@ -1,2 +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}
{}{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}
Expand Up @@ -31,18 +31,30 @@

@layer {
@layer foo {
color: red;
.class {
color: red;
}
}
@layer foo {
color: red;
.class {
color: red;
}
}
}

@layer {
@layer foo { color: red; }
@layer foo {
.class {
color: red;
}
}
}
@layer {
@layer foo { color: red; }
@layer foo {
.class {
color: red;
}
}
}

@layer framework {
Expand Down

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

37 changes: 20 additions & 17 deletions crates/swc_css_parser/src/parser/at_rules/mod.rs
Expand Up @@ -171,7 +171,7 @@ where
parser.input.skip_ws();

let prelude = if is!(parser, Ident) {
let mut name_list = vec![];
let mut name_list: Vec<LayerName> = vec![];

while is!(parser, Ident) {
name_list.push(parser.parse()?);
Expand All @@ -185,23 +185,26 @@ where
}
}

match name_list.len() == 1 {
// Block
true => Some(AtRulePrelude::LayerPrelude(LayerPrelude::Name(
name_list.remove(0),
))),
// Statement
false => {
let first = name_list[0].span;
let last = name_list[name_list.len() - 1].span;

Some(AtRulePrelude::LayerPrelude(LayerPrelude::NameList(
LayerNameList {
name_list,
span: Span::new(first.lo, last.hi, Default::default()),
},
)))
if is!(parser, ";") {
let first = name_list[0].span;
let last = name_list[name_list.len() - 1].span;

Some(AtRulePrelude::LayerPrelude(LayerPrelude::NameList(
LayerNameList {
name_list,
span: Span::new(first.lo, last.hi, Default::default()),
},
)))
} else {
if name_list.len() > 1 {
let span = parser.input.cur_span();

return Err(Error::new(span, ErrorKind::Expected("';' token")));
}

Some(AtRulePrelude::LayerPrelude(LayerPrelude::Name(
name_list.remove(0),
)))
}
} else {
None
Expand Down

1 comment on commit ac619e8

@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: ac619e8 Previous: 61e63df Ratio
es/full/bugs-1 417132 ns/iter (± 31386) 344659 ns/iter (± 58655) 1.21
es/full/minify/libraries/antd 1929838236 ns/iter (± 62155638) 1763024993 ns/iter (± 41243845) 1.09
es/full/minify/libraries/d3 448627098 ns/iter (± 26588188) 395332176 ns/iter (± 30802578) 1.13
es/full/minify/libraries/echarts 1685534636 ns/iter (± 57821745) 1513960726 ns/iter (± 27279797) 1.11
es/full/minify/libraries/jquery 121054014 ns/iter (± 7567893) 96085146 ns/iter (± 4272598) 1.26
es/full/minify/libraries/lodash 146773775 ns/iter (± 20427694) 124221774 ns/iter (± 33430190) 1.18
es/full/minify/libraries/moment 68356029 ns/iter (± 9635188) 59098084 ns/iter (± 2512245) 1.16
es/full/minify/libraries/react 23907140 ns/iter (± 2384930) 19228140 ns/iter (± 308378) 1.24
es/full/minify/libraries/terser 401071439 ns/iter (± 105774281) 284935298 ns/iter (± 3849592) 1.41
es/full/minify/libraries/three 597464635 ns/iter (± 30496218) 557622115 ns/iter (± 32410860) 1.07
es/full/minify/libraries/typescript 3710322952 ns/iter (± 91292997) 3325883354 ns/iter (± 58154515) 1.12
es/full/minify/libraries/victory 911988820 ns/iter (± 49920259) 781637165 ns/iter (± 23852358) 1.17
es/full/minify/libraries/vue 180925651 ns/iter (± 11752773) 147558217 ns/iter (± 17014846) 1.23
es/full/codegen/es3 33633 ns/iter (± 781) 33338 ns/iter (± 929) 1.01
es/full/codegen/es5 33781 ns/iter (± 1393) 33347 ns/iter (± 1449) 1.01
es/full/codegen/es2015 33687 ns/iter (± 1360) 33163 ns/iter (± 1364) 1.02
es/full/codegen/es2016 33704 ns/iter (± 1263) 33040 ns/iter (± 1144) 1.02
es/full/codegen/es2017 33601 ns/iter (± 1826) 32539 ns/iter (± 661) 1.03
es/full/codegen/es2018 33619 ns/iter (± 536) 32713 ns/iter (± 552) 1.03
es/full/codegen/es2019 33609 ns/iter (± 1886) 33198 ns/iter (± 905) 1.01
es/full/codegen/es2020 33720 ns/iter (± 985) 32566 ns/iter (± 1031) 1.04
es/full/all/es3 221332019 ns/iter (± 17767951) 185450719 ns/iter (± 7122511) 1.19
es/full/all/es5 206741870 ns/iter (± 14488315) 176648556 ns/iter (± 9748255) 1.17
es/full/all/es2015 161918906 ns/iter (± 16384092) 141243913 ns/iter (± 3748977) 1.15
es/full/all/es2016 163775795 ns/iter (± 16926496) 140373835 ns/iter (± 8625788) 1.17
es/full/all/es2017 163407731 ns/iter (± 11877118) 143748915 ns/iter (± 10618450) 1.14
es/full/all/es2018 156692344 ns/iter (± 12641828) 141307936 ns/iter (± 10894477) 1.11
es/full/all/es2019 160795826 ns/iter (± 11177785) 139028697 ns/iter (± 3783130) 1.16
es/full/all/es2020 151948687 ns/iter (± 15465789) 134901726 ns/iter (± 8130817) 1.13
es/full/parser 724786 ns/iter (± 35636) 709739 ns/iter (± 32116) 1.02
es/full/base/fixer 27052 ns/iter (± 1184) 25580 ns/iter (± 675) 1.06
es/full/base/resolver_and_hygiene 92554 ns/iter (± 3164) 89450 ns/iter (± 3912) 1.03
serialization of ast node 213 ns/iter (± 4) 211 ns/iter (± 3) 1.01
serialization of serde 218 ns/iter (± 17) 216 ns/iter (± 4) 1.01

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

Please sign in to comment.