Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(css/parser): improve recovery parsing #6336

Merged
merged 3 commits into from Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 }