From 9f4c5b7ba55a7310c410133e446790a4ad5ad99a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 1 Oct 2021 20:04:24 +0900 Subject: [PATCH] fix(css/parser): Fix error recovery logic of property values (#2331) swc_css_parser: - Fix `state` used for error recovery. --- Cargo.lock | 2 +- css/parser/Cargo.toml | 2 +- css/parser/src/parser/value/mod.rs | 3 +- .../tests/recovery/styled-jsx/1/output.json | 54 +++++ .../tests/recovery/styled-jsx/2/input.css | 3 + .../tests/recovery/styled-jsx/2/output.json | 201 ++++++++++++++++++ 6 files changed, 262 insertions(+), 3 deletions(-) create mode 100644 css/parser/tests/recovery/styled-jsx/2/input.css create mode 100644 css/parser/tests/recovery/styled-jsx/2/output.json diff --git a/Cargo.lock b/Cargo.lock index e7af66c7772a..7b34474ae1b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2525,7 +2525,7 @@ dependencies = [ [[package]] name = "swc_css_parser" -version = "0.8.1" +version = "0.8.2" dependencies = [ "bitflags", "lexical", diff --git a/css/parser/Cargo.toml b/css/parser/Cargo.toml index 9f6a154aa8a7..6abc7beb34a5 100644 --- a/css/parser/Cargo.toml +++ b/css/parser/Cargo.toml @@ -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] diff --git a/css/parser/src/parser/value/mod.rs b/css/parser/src/parser/value/mod.rs index 87f7e526a33e..aad8f5f87d02 100644 --- a/css/parser/src/parser/value/mod.rs +++ b/css/parser/src/parser/value/mod.rs @@ -21,6 +21,7 @@ where /// /// Returned [BytePos] is `hi`. pub(super) fn parse_property_values(&mut self) -> PResult<(Vec, BytePos)> { + let start = self.input.state(); let mut values = vec![]; let mut state = self.input.state(); let start_pos = self.input.cur_span()?.lo; @@ -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, ";", "}", "!", ")") { diff --git a/css/parser/tests/recovery/styled-jsx/1/output.json b/css/parser/tests/recovery/styled-jsx/1/output.json index 3f6392dbf584..91b68e588dcf 100644 --- a/css/parser/tests/recovery/styled-jsx/1/output.json +++ b/css/parser/tests/recovery/styled-jsx/1/output.json @@ -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, @@ -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, @@ -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, diff --git a/css/parser/tests/recovery/styled-jsx/2/input.css b/css/parser/tests/recovery/styled-jsx/2/input.css new file mode 100644 index 000000000000..27e560f10c30 --- /dev/null +++ b/css/parser/tests/recovery/styled-jsx/2/input.css @@ -0,0 +1,3 @@ +.a :global(> .b) { + flex-basis: __styled-jsx-placeholder__0%; +} diff --git a/css/parser/tests/recovery/styled-jsx/2/output.json b/css/parser/tests/recovery/styled-jsx/2/output.json new file mode 100644 index 000000000000..b11bfa6d6b57 --- /dev/null +++ b/css/parser/tests/recovery/styled-jsx/2/output.json @@ -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 + } + ] + } + } + ] +}