Skip to content

Commit 956d574

Browse files
authoredOct 28, 2022
feat(css/parser): Improve error reporting (#6274)
1 parent 55f2cdf commit 956d574

File tree

10 files changed

+527
-143
lines changed

10 files changed

+527
-143
lines changed
 

‎crates/swc_css_parser/src/parser/selectors/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,8 @@ where
403403
I: ParserInput,
404404
{
405405
fn parse(&mut self) -> PResult<CompoundSelector> {
406-
let span = self.input.cur_span();
407-
let start_pos = span.lo;
406+
let start_span = self.input.cur_span();
407+
let start_pos = start_span.lo;
408408

409409
let mut nesting_selector = None;
410410

@@ -459,12 +459,12 @@ where
459459
}
460460
}
461461

462-
let span = span!(self, start_pos);
463-
464462
if nesting_selector.is_none() && type_selector.is_none() && subclass_selectors.is_empty() {
465-
return Err(Error::new(span, ErrorKind::InvalidSelector));
463+
return Err(Error::new(start_span, ErrorKind::InvalidSelector));
466464
}
467465

466+
let span = span!(self, start_pos);
467+
468468
Ok(CompoundSelector {
469469
span,
470470
nesting_selector,

‎crates/swc_css_parser/tests/fixture.rs

-3
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,8 @@ fn span_visualizer_line_comment(input: PathBuf) {
581581
#[testing::fixture(
582582
"tests/recovery/**/input.css",
583583
exclude(
584-
"at-rule/media/condition/input.css",
585-
"at-rule/media/condition-1/input.css",
586584
"at-rule/page/invalid-nesting/input.css",
587585
"at-rule/page/without-page/input.css",
588-
"at-rule/unknown/input.css",
589586
"function/calc/division/input.css",
590587
"function/calc/space/input.css",
591588
"function/var/input.css",

‎crates/swc_css_parser/tests/recovery/at-rule/media/condition-1/output.json

+310-37
Original file line numberDiff line numberDiff line change
@@ -24,60 +24,325 @@
2424
"raw": "media"
2525
},
2626
"prelude": {
27-
"type": "MediaQueryList",
27+
"type": "ListOfComponentValues",
2828
"span": {
29-
"start": 49,
30-
"end": 59,
29+
"start": 7,
30+
"end": 60,
3131
"ctxt": 0
3232
},
33-
"queries": [
33+
"children": [
3434
{
35-
"type": "MediaQuery",
35+
"type": "PreservedToken",
36+
"span": {
37+
"start": 7,
38+
"end": 8,
39+
"ctxt": 0
40+
},
41+
"token": {
42+
"WhiteSpace": {
43+
"value": " "
44+
}
45+
}
46+
},
47+
{
48+
"type": "SimpleBlock",
49+
"span": {
50+
"start": 8,
51+
"end": 22,
52+
"ctxt": 0
53+
},
54+
"name": {
55+
"type": "PreservedToken",
56+
"span": {
57+
"start": 8,
58+
"end": 9,
59+
"ctxt": 0
60+
},
61+
"token": "LParen"
62+
},
63+
"value": [
64+
{
65+
"type": "PreservedToken",
66+
"span": {
67+
"start": 9,
68+
"end": 15,
69+
"ctxt": 0
70+
},
71+
"token": {
72+
"Ident": {
73+
"value": "update",
74+
"raw": "update"
75+
}
76+
}
77+
},
78+
{
79+
"type": "PreservedToken",
80+
"span": {
81+
"start": 15,
82+
"end": 16,
83+
"ctxt": 0
84+
},
85+
"token": "Colon"
86+
},
87+
{
88+
"type": "PreservedToken",
89+
"span": {
90+
"start": 16,
91+
"end": 17,
92+
"ctxt": 0
93+
},
94+
"token": {
95+
"WhiteSpace": {
96+
"value": " "
97+
}
98+
}
99+
},
100+
{
101+
"type": "PreservedToken",
102+
"span": {
103+
"start": 17,
104+
"end": 21,
105+
"ctxt": 0
106+
},
107+
"token": {
108+
"Ident": {
109+
"value": "slow",
110+
"raw": "slow"
111+
}
112+
}
113+
}
114+
]
115+
},
116+
{
117+
"type": "PreservedToken",
118+
"span": {
119+
"start": 22,
120+
"end": 23,
121+
"ctxt": 0
122+
},
123+
"token": {
124+
"WhiteSpace": {
125+
"value": " "
126+
}
127+
}
128+
},
129+
{
130+
"type": "PreservedToken",
131+
"span": {
132+
"start": 23,
133+
"end": 26,
134+
"ctxt": 0
135+
},
136+
"token": {
137+
"Ident": {
138+
"value": "and",
139+
"raw": "and"
140+
}
141+
}
142+
},
143+
{
144+
"type": "PreservedToken",
145+
"span": {
146+
"start": 26,
147+
"end": 27,
148+
"ctxt": 0
149+
},
150+
"token": {
151+
"WhiteSpace": {
152+
"value": " "
153+
}
154+
}
155+
},
156+
{
157+
"type": "SimpleBlock",
158+
"span": {
159+
"start": 27,
160+
"end": 40,
161+
"ctxt": 0
162+
},
163+
"name": {
164+
"type": "PreservedToken",
165+
"span": {
166+
"start": 27,
167+
"end": 28,
168+
"ctxt": 0
169+
},
170+
"token": "LParen"
171+
},
172+
"value": [
173+
{
174+
"type": "PreservedToken",
175+
"span": {
176+
"start": 28,
177+
"end": 33,
178+
"ctxt": 0
179+
},
180+
"token": {
181+
"Ident": {
182+
"value": "hover",
183+
"raw": "hover"
184+
}
185+
}
186+
},
187+
{
188+
"type": "PreservedToken",
189+
"span": {
190+
"start": 33,
191+
"end": 34,
192+
"ctxt": 0
193+
},
194+
"token": "Colon"
195+
},
196+
{
197+
"type": "PreservedToken",
198+
"span": {
199+
"start": 34,
200+
"end": 35,
201+
"ctxt": 0
202+
},
203+
"token": {
204+
"WhiteSpace": {
205+
"value": " "
206+
}
207+
}
208+
},
209+
{
210+
"type": "PreservedToken",
211+
"span": {
212+
"start": 35,
213+
"end": 39,
214+
"ctxt": 0
215+
},
216+
"token": {
217+
"Ident": {
218+
"value": "none",
219+
"raw": "none"
220+
}
221+
}
222+
}
223+
]
224+
},
225+
{
226+
"type": "PreservedToken",
227+
"span": {
228+
"start": 40,
229+
"end": 41,
230+
"ctxt": 0
231+
},
232+
"token": {
233+
"WhiteSpace": {
234+
"value": " "
235+
}
236+
}
237+
},
238+
{
239+
"type": "PreservedToken",
240+
"span": {
241+
"start": 41,
242+
"end": 48,
243+
"ctxt": 0
244+
},
245+
"token": {
246+
"Ident": {
247+
"value": "unknown",
248+
"raw": "unknown"
249+
}
250+
}
251+
},
252+
{
253+
"type": "PreservedToken",
254+
"span": {
255+
"start": 48,
256+
"end": 49,
257+
"ctxt": 0
258+
},
259+
"token": {
260+
"WhiteSpace": {
261+
"value": " "
262+
}
263+
}
264+
},
265+
{
266+
"type": "SimpleBlock",
36267
"span": {
37268
"start": 49,
38269
"end": 59,
39270
"ctxt": 0
40271
},
41-
"modifier": null,
42-
"mediaType": null,
43-
"keyword": null,
44-
"condition": {
45-
"type": "MediaCondition",
272+
"name": {
273+
"type": "PreservedToken",
46274
"span": {
47275
"start": 49,
48-
"end": 59,
276+
"end": 50,
49277
"ctxt": 0
50278
},
51-
"conditions": [
52-
{
53-
"type": "MediaFeaturePlain",
54-
"span": {
55-
"start": 49,
56-
"end": 59,
57-
"ctxt": 0
58-
},
59-
"name": {
60-
"type": "Ident",
61-
"span": {
62-
"start": 50,
63-
"end": 55,
64-
"ctxt": 0
65-
},
279+
"token": "LParen"
280+
},
281+
"value": [
282+
{
283+
"type": "PreservedToken",
284+
"span": {
285+
"start": 50,
286+
"end": 55,
287+
"ctxt": 0
288+
},
289+
"token": {
290+
"Ident": {
66291
"value": "color",
67292
"raw": "color"
68-
},
69-
"value": {
70-
"type": "Number",
71-
"span": {
72-
"start": 57,
73-
"end": 58,
74-
"ctxt": 0
75-
},
293+
}
294+
}
295+
},
296+
{
297+
"type": "PreservedToken",
298+
"span": {
299+
"start": 55,
300+
"end": 56,
301+
"ctxt": 0
302+
},
303+
"token": "Colon"
304+
},
305+
{
306+
"type": "PreservedToken",
307+
"span": {
308+
"start": 56,
309+
"end": 57,
310+
"ctxt": 0
311+
},
312+
"token": {
313+
"WhiteSpace": {
314+
"value": " "
315+
}
316+
}
317+
},
318+
{
319+
"type": "PreservedToken",
320+
"span": {
321+
"start": 57,
322+
"end": 58,
323+
"ctxt": 0
324+
},
325+
"token": {
326+
"Number": {
76327
"value": 1.0,
77-
"raw": "1"
328+
"raw": "1",
329+
"type": "integer"
78330
}
79331
}
80-
]
332+
}
333+
]
334+
},
335+
{
336+
"type": "PreservedToken",
337+
"span": {
338+
"start": 59,
339+
"end": 60,
340+
"ctxt": 0
341+
},
342+
"token": {
343+
"WhiteSpace": {
344+
"value": " "
345+
}
81346
}
82347
}
83348
]
@@ -89,7 +354,15 @@
89354
"end": 62,
90355
"ctxt": 0
91356
},
92-
"name": "{",
357+
"name": {
358+
"type": "PreservedToken",
359+
"span": {
360+
"start": 60,
361+
"end": 61,
362+
"ctxt": 0
363+
},
364+
"token": "LBrace"
365+
},
93366
"value": []
94367
}
95368
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
x Unexpected tokens in at-rule prelude
3+
,-[$DIR/tests/recovery/at-rule/media/condition-1/input.css:1:1]
4+
1 | @media (update: slow) and (hover: none) unknown (color: 1) {}
5+
: ^^^^^^^
6+
`----

‎crates/swc_css_parser/tests/recovery/at-rule/media/condition/output.json

+199-36
Original file line numberDiff line numberDiff line change
@@ -24,60 +24,215 @@
2424
"raw": "media"
2525
},
2626
"prelude": {
27-
"type": "MediaQueryList",
27+
"type": "ListOfComponentValues",
2828
"span": {
29-
"start": 31,
30-
"end": 44,
29+
"start": 7,
30+
"end": 45,
3131
"ctxt": 0
3232
},
33-
"queries": [
33+
"children": [
3434
{
35-
"type": "MediaQuery",
35+
"type": "PreservedToken",
36+
"span": {
37+
"start": 7,
38+
"end": 8,
39+
"ctxt": 0
40+
},
41+
"token": {
42+
"WhiteSpace": {
43+
"value": " "
44+
}
45+
}
46+
},
47+
{
48+
"type": "SimpleBlock",
49+
"span": {
50+
"start": 8,
51+
"end": 22,
52+
"ctxt": 0
53+
},
54+
"name": {
55+
"type": "PreservedToken",
56+
"span": {
57+
"start": 8,
58+
"end": 9,
59+
"ctxt": 0
60+
},
61+
"token": "LParen"
62+
},
63+
"value": [
64+
{
65+
"type": "PreservedToken",
66+
"span": {
67+
"start": 9,
68+
"end": 15,
69+
"ctxt": 0
70+
},
71+
"token": {
72+
"Ident": {
73+
"value": "update",
74+
"raw": "update"
75+
}
76+
}
77+
},
78+
{
79+
"type": "PreservedToken",
80+
"span": {
81+
"start": 15,
82+
"end": 16,
83+
"ctxt": 0
84+
},
85+
"token": "Colon"
86+
},
87+
{
88+
"type": "PreservedToken",
89+
"span": {
90+
"start": 16,
91+
"end": 17,
92+
"ctxt": 0
93+
},
94+
"token": {
95+
"WhiteSpace": {
96+
"value": " "
97+
}
98+
}
99+
},
100+
{
101+
"type": "PreservedToken",
102+
"span": {
103+
"start": 17,
104+
"end": 21,
105+
"ctxt": 0
106+
},
107+
"token": {
108+
"Ident": {
109+
"value": "slow",
110+
"raw": "slow"
111+
}
112+
}
113+
}
114+
]
115+
},
116+
{
117+
"type": "PreservedToken",
118+
"span": {
119+
"start": 22,
120+
"end": 23,
121+
"ctxt": 0
122+
},
123+
"token": {
124+
"WhiteSpace": {
125+
"value": " "
126+
}
127+
}
128+
},
129+
{
130+
"type": "PreservedToken",
131+
"span": {
132+
"start": 23,
133+
"end": 30,
134+
"ctxt": 0
135+
},
136+
"token": {
137+
"Ident": {
138+
"value": "unknown",
139+
"raw": "unknown"
140+
}
141+
}
142+
},
143+
{
144+
"type": "PreservedToken",
145+
"span": {
146+
"start": 30,
147+
"end": 31,
148+
"ctxt": 0
149+
},
150+
"token": {
151+
"WhiteSpace": {
152+
"value": " "
153+
}
154+
}
155+
},
156+
{
157+
"type": "SimpleBlock",
36158
"span": {
37159
"start": 31,
38160
"end": 44,
39161
"ctxt": 0
40162
},
41-
"modifier": null,
42-
"mediaType": null,
43-
"keyword": null,
44-
"condition": {
45-
"type": "MediaCondition",
163+
"name": {
164+
"type": "PreservedToken",
46165
"span": {
47166
"start": 31,
48-
"end": 44,
167+
"end": 32,
49168
"ctxt": 0
50169
},
51-
"conditions": [
52-
{
53-
"type": "MediaFeaturePlain",
54-
"span": {
55-
"start": 31,
56-
"end": 44,
57-
"ctxt": 0
58-
},
59-
"name": {
60-
"type": "Ident",
61-
"span": {
62-
"start": 32,
63-
"end": 37,
64-
"ctxt": 0
65-
},
170+
"token": "LParen"
171+
},
172+
"value": [
173+
{
174+
"type": "PreservedToken",
175+
"span": {
176+
"start": 32,
177+
"end": 37,
178+
"ctxt": 0
179+
},
180+
"token": {
181+
"Ident": {
66182
"value": "hover",
67183
"raw": "hover"
68-
},
69-
"value": {
70-
"type": "Ident",
71-
"span": {
72-
"start": 39,
73-
"end": 43,
74-
"ctxt": 0
75-
},
184+
}
185+
}
186+
},
187+
{
188+
"type": "PreservedToken",
189+
"span": {
190+
"start": 37,
191+
"end": 38,
192+
"ctxt": 0
193+
},
194+
"token": "Colon"
195+
},
196+
{
197+
"type": "PreservedToken",
198+
"span": {
199+
"start": 38,
200+
"end": 39,
201+
"ctxt": 0
202+
},
203+
"token": {
204+
"WhiteSpace": {
205+
"value": " "
206+
}
207+
}
208+
},
209+
{
210+
"type": "PreservedToken",
211+
"span": {
212+
"start": 39,
213+
"end": 43,
214+
"ctxt": 0
215+
},
216+
"token": {
217+
"Ident": {
76218
"value": "none",
77219
"raw": "none"
78220
}
79221
}
80-
]
222+
}
223+
]
224+
},
225+
{
226+
"type": "PreservedToken",
227+
"span": {
228+
"start": 44,
229+
"end": 45,
230+
"ctxt": 0
231+
},
232+
"token": {
233+
"WhiteSpace": {
234+
"value": " "
235+
}
81236
}
82237
}
83238
]
@@ -89,7 +244,15 @@
89244
"end": 47,
90245
"ctxt": 0
91246
},
92-
"name": "{",
247+
"name": {
248+
"type": "PreservedToken",
249+
"span": {
250+
"start": 45,
251+
"end": 46,
252+
"ctxt": 0
253+
},
254+
"token": "LBrace"
255+
},
93256
"value": []
94257
}
95258
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
x Unexpected tokens in at-rule prelude
3+
,-[$DIR/tests/recovery/at-rule/media/condition/input.css:1:1]
4+
1 | @media (update: slow) unknown (hover: none) {}
5+
: ^^^^^^^
6+
`----

‎crates/swc_css_parser/tests/recovery/at-rule/unknown/input.css

-1
This file was deleted.

‎crates/swc_css_parser/tests/recovery/at-rule/unknown/output.json

-30
This file was deleted.

‎crates/swc_css_parser/tests/recovery/at-rule/unknown/span.rust-debug

-30
This file was deleted.

‎crates/swc_css_parser/tests/recovery/cdo-and-cdc/output.swc-stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
x Invalid selector
2121
,-[$DIR/tests/recovery/cdo-and-cdc/input.css:1:1]
2222
1 | <!-- 123 -->
23-
: ^
23+
: ^^^
2424
`----

1 commit comments

Comments
 (1)

github-actions[bot] commented on Oct 28, 2022

@github-actions[bot]

Benchmark

Benchmark suite Current: 956d574 Previous: 02f8d31 Ratio
es/full/bugs-1 351173 ns/iter (± 21971) 354161 ns/iter (± 28230) 0.99
es/full/minify/libraries/antd 1974722542 ns/iter (± 56745584) 1865115424 ns/iter (± 63451655) 1.06
es/full/minify/libraries/d3 456567209 ns/iter (± 9231317) 429008668 ns/iter (± 11133572) 1.06
es/full/minify/libraries/echarts 1688225647 ns/iter (± 38123174) 1570650146 ns/iter (± 38899866) 1.07
es/full/minify/libraries/jquery 120712503 ns/iter (± 5159910) 99925110 ns/iter (± 1841293) 1.21
es/full/minify/libraries/lodash 145119092 ns/iter (± 7237146) 113938045 ns/iter (± 8793400) 1.27
es/full/minify/libraries/moment 69265521 ns/iter (± 4378178) 62702625 ns/iter (± 2262337) 1.10
es/full/minify/libraries/react 21896969 ns/iter (± 733659) 20906086 ns/iter (± 983532) 1.05
es/full/minify/libraries/terser 356031188 ns/iter (± 19978424) 335456294 ns/iter (± 7702636) 1.06
es/full/minify/libraries/three 585498327 ns/iter (± 14200213) 576764323 ns/iter (± 13336693) 1.02
es/full/minify/libraries/typescript 3669590252 ns/iter (± 81940161) 3462433450 ns/iter (± 77210381) 1.06
es/full/minify/libraries/victory 897559822 ns/iter (± 34392897) 822593894 ns/iter (± 19122266) 1.09
es/full/minify/libraries/vue 183308548 ns/iter (± 10929151) 165270185 ns/iter (± 7545555) 1.11
es/full/codegen/es3 34453 ns/iter (± 2215) 34581 ns/iter (± 458) 1.00
es/full/codegen/es5 34147 ns/iter (± 3075) 34445 ns/iter (± 1261) 0.99
es/full/codegen/es2015 34521 ns/iter (± 1178) 34563 ns/iter (± 657) 1.00
es/full/codegen/es2016 34382 ns/iter (± 1139) 34421 ns/iter (± 994) 1.00
es/full/codegen/es2017 34197 ns/iter (± 2501) 34017 ns/iter (± 1070) 1.01
es/full/codegen/es2018 34240 ns/iter (± 1842) 34418 ns/iter (± 1711) 0.99
es/full/codegen/es2019 34137 ns/iter (± 2461) 34451 ns/iter (± 1569) 0.99
es/full/codegen/es2020 34410 ns/iter (± 1429) 34519 ns/iter (± 437) 1.00
es/full/all/es3 230720682 ns/iter (± 15514517) 196319736 ns/iter (± 11486928) 1.18
es/full/all/es5 219303837 ns/iter (± 38465480) 183276300 ns/iter (± 10531859) 1.20
es/full/all/es2015 177058610 ns/iter (± 11816327) 147468855 ns/iter (± 5020165) 1.20
es/full/all/es2016 172167049 ns/iter (± 11357839) 144376424 ns/iter (± 14632678) 1.19
es/full/all/es2017 161453457 ns/iter (± 8321840) 148698989 ns/iter (± 7522574) 1.09
es/full/all/es2018 162055643 ns/iter (± 11636701) 140426073 ns/iter (± 5928379) 1.15
es/full/all/es2019 169801773 ns/iter (± 9905999) 149695966 ns/iter (± 10112648) 1.13
es/full/all/es2020 167918539 ns/iter (± 15932387) 138654535 ns/iter (± 6286155) 1.21
es/full/parser 754540 ns/iter (± 74902) 728183 ns/iter (± 90481) 1.04
es/full/base/fixer 26627 ns/iter (± 1444) 26325 ns/iter (± 410) 1.01
es/full/base/resolver_and_hygiene 96495 ns/iter (± 4317) 92630 ns/iter (± 2222) 1.04
serialization of ast node 220 ns/iter (± 19) 214 ns/iter (± 3) 1.03
serialization of serde 222 ns/iter (± 11) 215 ns/iter (± 4) 1.03

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

Please sign in to comment.