Skip to content

Commit

Permalink
fix(css/parser): Fix error recovery logic of property values (#2331)
Browse files Browse the repository at this point in the history
swc_css_parser:
 - Fix `state` used for error recovery.
  • Loading branch information
kdy1 authored Oct 1, 2021
1 parent dad7392 commit 9f4c5b7
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion css/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"
license = "Apache-2.0/MIT"
name = "swc_css_parser"
repository = "https://github.com/swc-project/swc.git"
version = "0.8.1"
version = "0.8.2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
Expand Down
3 changes: 2 additions & 1 deletion css/parser/src/parser/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ where
///
/// Returned [BytePos] is `hi`.
pub(super) fn parse_property_values(&mut self) -> PResult<(Vec<Value>, BytePos)> {
let start = self.input.state();
let mut values = vec![];
let mut state = self.input.state();
let start_pos = self.input.cur_span()?.lo;
Expand All @@ -47,7 +48,7 @@ where
if self.ctx.recover_from_property_value
&& !is_one_of!(self, EOF, ";", "}", "!", ")")
{
self.input.reset(&state);
self.input.reset(&start);

let mut tokens = vec![];
while !is_one_of!(self, EOF, ";", "}", "!", ")") {
Expand Down
54 changes: 54 additions & 0 deletions css/parser/tests/recovery/styled-jsx/1/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,24 @@
"ctxt": 0
},
"tokens": [
{
"span": {
"start": 236,
"end": 237,
"ctxt": 0
},
"token": "WhiteSpace"
},
{
"span": {
"start": 237,
"end": 264,
"ctxt": 0
},
"token": {
"Ident": "__styled-jsx-placeholder__2"
}
},
{
"span": {
"start": 264,
Expand Down Expand Up @@ -587,6 +605,24 @@
"ctxt": 0
},
"tokens": [
{
"span": {
"start": 377,
"end": 378,
"ctxt": 0
},
"token": "WhiteSpace"
},
{
"span": {
"start": 378,
"end": 405,
"ctxt": 0
},
"token": {
"Ident": "__styled-jsx-placeholder__3"
}
},
{
"span": {
"start": 405,
Expand Down Expand Up @@ -779,6 +815,24 @@
"ctxt": 0
},
"tokens": [
{
"span": {
"start": 506,
"end": 507,
"ctxt": 0
},
"token": "WhiteSpace"
},
{
"span": {
"start": 507,
"end": 534,
"ctxt": 0
},
"token": {
"Ident": "__styled-jsx-placeholder__4"
}
},
{
"span": {
"start": 534,
Expand Down
3 changes: 3 additions & 0 deletions css/parser/tests/recovery/styled-jsx/2/input.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.a :global(> .b) {
flex-basis: __styled-jsx-placeholder__0%;
}
201 changes: 201 additions & 0 deletions css/parser/tests/recovery/styled-jsx/2/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
{
"type": "Stylesheet",
"span": {
"start": 0,
"end": 67,
"ctxt": 0
},
"rules": [
{
"type": "StyleRule",
"span": {
"start": 0,
"end": 66,
"ctxt": 0
},
"selectors": [
{
"type": "ComplexSelector",
"span": {
"start": 0,
"end": 16,
"ctxt": 0
},
"selectors": [
{
"type": "CompoundSelector",
"span": {
"start": 0,
"end": 2,
"ctxt": 0
},
"hasNestPrefix": false,
"combinator": null,
"typeSelector": null,
"subclassSelectors": [
{
"type": "ClassSelector",
"span": {
"start": 0,
"end": 2,
"ctxt": 0
},
"text": {
"type": "Text",
"span": {
"start": 1,
"end": 2,
"ctxt": 0
},
"value": "a"
}
}
]
},
{
"type": "CompoundSelector",
"span": {
"start": 3,
"end": 16,
"ctxt": 0
},
"hasNestPrefix": false,
"combinator": null,
"typeSelector": null,
"subclassSelectors": [
{
"type": "PseudoSelector",
"span": {
"start": 3,
"end": 16,
"ctxt": 0
},
"isElement": false,
"name": {
"type": "Text",
"span": {
"start": 4,
"end": 10,
"ctxt": 0
},
"value": "global"
},
"args": {
"type": "Tokens",
"span": {
"start": 11,
"end": 15,
"ctxt": 0
},
"tokens": [
{
"span": {
"start": 11,
"end": 12,
"ctxt": 0
},
"token": "GreaterThan"
},
{
"span": {
"start": 12,
"end": 13,
"ctxt": 0
},
"token": "WhiteSpace"
},
{
"span": {
"start": 13,
"end": 14,
"ctxt": 0
},
"token": "Dot"
},
{
"span": {
"start": 14,
"end": 15,
"ctxt": 0
},
"token": {
"Ident": "b"
}
}
]
}
}
]
}
]
}
],
"block": {
"type": "DeclBlock",
"span": {
"start": 17,
"end": 66,
"ctxt": 0
},
"items": [
{
"type": "Property",
"span": {
"start": 23,
"end": 62,
"ctxt": 0
},
"name": {
"type": "Text",
"span": {
"start": 23,
"end": 33,
"ctxt": 0
},
"value": "flex-basis"
},
"values": [
{
"type": "Tokens",
"span": {
"start": 34,
"end": 63,
"ctxt": 0
},
"tokens": [
{
"span": {
"start": 34,
"end": 35,
"ctxt": 0
},
"token": "WhiteSpace"
},
{
"span": {
"start": 35,
"end": 62,
"ctxt": 0
},
"token": {
"Ident": "__styled-jsx-placeholder__0"
}
},
{
"span": {
"start": 62,
"end": 63,
"ctxt": 0
},
"token": "Percent"
}
]
}
],
"important": null
}
]
}
}
]
}

0 comments on commit 9f4c5b7

Please sign in to comment.