Skip to content

Commit

Permalink
feat(css/parser): Improve error recovery (#6336)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 3, 2022
1 parent a1a6513 commit 0bc682f
Show file tree
Hide file tree
Showing 22 changed files with 2,738 additions and 95 deletions.
1 change: 1 addition & 0 deletions crates/swc_css_modules/tests/fixture/url/url.compiled.css
Expand Up @@ -72,6 +72,7 @@ b {
}
@keyframes __local__anim {
background: green url('./img.png') xyz;

}
.__local__a {
background-image: -webkit-image-set(url('./img1x.png') 1x, url('./img2x.png') 2x);
Expand Down
32 changes: 13 additions & 19 deletions crates/swc_css_parser/src/parser/syntax/mod.rs
Expand Up @@ -14,6 +14,8 @@ where
I: ParserInput,
{
fn parse(&mut self) -> PResult<Stylesheet> {
// Create a new stylesheet, with its location set to location (or null, if
// location was not passed).
let start = self.input.cur_span();

// Consume a list of rules from input, with the top-level flag set, and set the
Expand Down Expand Up @@ -46,10 +48,9 @@ where

// Repeatedly consume the next input token:
loop {
// TODO: remove `}`
// <EOF-token>
// Return the list of rules.
if is_one_of!(self, EOF, "}") {
if is!(self, EOF) {
return Ok(rules);
}

Expand Down Expand Up @@ -92,28 +93,21 @@ where
self.input.reset(&state);

let span = self.input.cur_span();
let mut children = vec![];
let mut list_of_component_values = ListOfComponentValues {
span: Default::default(),
children: vec![],
};

while !is_one_of!(self, EOF, "}") {
if let Some(token_and_span) = self.input.bump() {
children.push(ComponentValue::PreservedToken(token_and_span));
}
while !is_one_of!(self, EOF) {
let component_value = self.parse_as::<ComponentValue>()?;

if is!(self, ";") {
if let Some(token_and_span) = self.input.bump() {
children
.push(ComponentValue::PreservedToken(token_and_span));
}

break;
}
list_of_component_values.children.push(component_value);
}

list_of_component_values.span = span!(self, span.lo);

rules.push(Rule::ListOfComponentValues(Box::new(
ListOfComponentValues {
span: span!(self, span.lo),
children,
},
list_of_component_values,
)));
}
};
Expand Down
Expand Up @@ -121,7 +121,7 @@
"type": "ListOfComponentValues",
"span": {
"start": 33,
"end": 57,
"end": 58,
"ctxt": 0
},
"children": [
Expand Down Expand Up @@ -186,6 +186,19 @@
"ctxt": 0
},
"token": "Semi"
},
{
"type": "PreservedToken",
"span": {
"start": 57,
"end": 58,
"ctxt": 0
},
"token": {
"WhiteSpace": {
"value": "\n"
}
}
}
]
}
Expand Down
Expand Up @@ -132,13 +132,15 @@
x ComponentValue
,-[$DIR/tests/recovery/at-rule/media/invalid-nesting/input.css:2:5]
2 | max-inline-size: 1024px;
: ^^^^^^^^^^^^^^^^^^^^^^^^
: ^^^^^^^^^^^^^^^^^^^^^^^^^
3 | }
`----

x Rule
,-[$DIR/tests/recovery/at-rule/media/invalid-nesting/input.css:2:5]
2 | max-inline-size: 1024px;
: ^^^^^^^^^^^^^^^^^^^^^^^^
: ^^^^^^^^^^^^^^^^^^^^^^^^^
3 | }
`----

x ComponentValue
Expand Down Expand Up @@ -200,3 +202,18 @@
2 | max-inline-size: 1024px;
: ^
`----

x ComponentValue
,-[$DIR/tests/recovery/at-rule/media/invalid-nesting/input.css:2:5]
2 | max-inline-size: 1024px;
: ^
3 | }
`----

x WhiteSpace { value: Atom('
| ' type=inline) }
,-[$DIR/tests/recovery/at-rule/media/invalid-nesting/input.css:2:5]
2 | max-inline-size: 1024px;
: ^
3 | }
`----
@@ -0,0 +1,5 @@
a { color: red }

@media {};

a { color: blue }

1 comment on commit 0bc682f

@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: 0bc682f Previous: 064baa3 Ratio
es/full/bugs-1 338527 ns/iter (± 24360) 339808 ns/iter (± 48022) 1.00
es/full/minify/libraries/antd 1833379374 ns/iter (± 21082279) 1856398174 ns/iter (± 46806771) 0.99
es/full/minify/libraries/d3 398937604 ns/iter (± 12891610) 401701503 ns/iter (± 8657620) 0.99
es/full/minify/libraries/echarts 1578242949 ns/iter (± 52008559) 1555155718 ns/iter (± 35870688) 1.01
es/full/minify/libraries/jquery 106063482 ns/iter (± 4437550) 103256786 ns/iter (± 10774673) 1.03
es/full/minify/libraries/lodash 124068263 ns/iter (± 5828124) 124116171 ns/iter (± 18125859) 1.00
es/full/minify/libraries/moment 64736929 ns/iter (± 2590095) 61347180 ns/iter (± 15396281) 1.06
es/full/minify/libraries/react 22038563 ns/iter (± 4121609) 20327598 ns/iter (± 321172) 1.08
es/full/minify/libraries/terser 333585064 ns/iter (± 34300381) 315845682 ns/iter (± 11791358) 1.06
es/full/minify/libraries/three 581825185 ns/iter (± 72716746) 657432677 ns/iter (± 51592779) 0.88
es/full/minify/libraries/typescript 3645247424 ns/iter (± 324550789) 4103281120 ns/iter (± 286754135) 0.89
es/full/minify/libraries/victory 873732272 ns/iter (± 25670948) 893316270 ns/iter (± 24916831) 0.98
es/full/minify/libraries/vue 172703773 ns/iter (± 35590215) 182807407 ns/iter (± 35193740) 0.94
es/full/codegen/es3 34601 ns/iter (± 2101) 39284 ns/iter (± 21432) 0.88
es/full/codegen/es5 35332 ns/iter (± 2179) 37685 ns/iter (± 11258) 0.94
es/full/codegen/es2015 35445 ns/iter (± 2615) 36696 ns/iter (± 1924) 0.97
es/full/codegen/es2016 35474 ns/iter (± 2604) 34983 ns/iter (± 4634) 1.01
es/full/codegen/es2017 35534 ns/iter (± 3290) 34542 ns/iter (± 3656) 1.03
es/full/codegen/es2018 34364 ns/iter (± 1932) 33965 ns/iter (± 1371) 1.01
es/full/codegen/es2019 34865 ns/iter (± 2110) 33881 ns/iter (± 2814) 1.03
es/full/codegen/es2020 35151 ns/iter (± 2926) 34071 ns/iter (± 1199) 1.03
es/full/all/es3 237190192 ns/iter (± 27753241) 218747442 ns/iter (± 67997967) 1.08
es/full/all/es5 207843771 ns/iter (± 21580109) 213554067 ns/iter (± 65525383) 0.97
es/full/all/es2015 161634105 ns/iter (± 22635752) 162053569 ns/iter (± 54919097) 1.00
es/full/all/es2016 155208364 ns/iter (± 29529765) 162728545 ns/iter (± 58030801) 0.95
es/full/all/es2017 164248786 ns/iter (± 21341861) 162981036 ns/iter (± 55499897) 1.01
es/full/all/es2018 161512573 ns/iter (± 14838808) 162238444 ns/iter (± 41139637) 1.00
es/full/all/es2019 162666550 ns/iter (± 13698305) 158923971 ns/iter (± 47143856) 1.02
es/full/all/es2020 148743953 ns/iter (± 18428633) 160588323 ns/iter (± 39311778) 0.93
es/full/parser 733115 ns/iter (± 46407) 785268 ns/iter (± 138991) 0.93
es/full/base/fixer 26756 ns/iter (± 868) 32959 ns/iter (± 9814) 0.81
es/full/base/resolver_and_hygiene 95018 ns/iter (± 2854) 105120 ns/iter (± 8719) 0.90
serialization of ast node 215 ns/iter (± 7) 219 ns/iter (± 17) 0.98
serialization of serde 219 ns/iter (± 12) 221 ns/iter (± 29) 0.99

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

Please sign in to comment.