Skip to content

Commit

Permalink
fix(css/parser): error message
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Nov 10, 2022
1 parent 0f26c5c commit 03bbbd5
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 125 deletions.
49 changes: 24 additions & 25 deletions crates/swc_css_parser/src/parser/syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,9 @@ where
// Otherwise, append a <semicolon-token> to the qualified rule’s prelude.
tok!(";") => {
if self.ctx.mixed_with_declarations {
return Err(Error::new(
span!(self, span.lo),
ErrorKind::EofButExpected("'{'"),
));
let span = self.input.cur_span();

return Err(Error::new(span, ErrorKind::Expected("'{'")));
} else {
let component_value = self.parse_as::<ComponentValue>()?;

Expand Down Expand Up @@ -669,9 +668,7 @@ where

self.errors.push(Error::new(
span,
ErrorKind::Expected(
"whitespace, semicolon, EOF, at-keyword or ident token",
),
ErrorKind::Expected("whitespace, ';', '@', ident or EOF"),
));

// For recovery mode
Expand Down Expand Up @@ -885,30 +882,31 @@ where
}

// Grammar parsing
let locv = self.create_locv(declaration.value);
let list_of_component_values = self.create_locv(declaration.value);

declaration.value = match self.parse_according_to_grammar(&locv, |parser| {
let mut values = vec![];
declaration.value =
match self.parse_according_to_grammar(&list_of_component_values, |parser| {
let mut values = vec![];

loop {
if is!(parser, EOF) {
break;
loop {
if is!(parser, EOF) {
break;
}

values.push(parser.parse_generic_value()?);
}

values.push(parser.parse_generic_value()?);
}
Ok(values)
}) {
Ok(values) => values,
Err(err) => {
if *err.kind() != ErrorKind::Ignore {
self.errors.push(err);
}

Ok(values)
}) {
Ok(values) => values,
Err(err) => {
if *err.kind() != ErrorKind::Ignore {
self.errors.push(err);
list_of_component_values.children
}

locv.children
}
};
};

// 8. Return the declaration.
Ok(declaration)
Expand Down Expand Up @@ -1104,6 +1102,7 @@ where

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

// Grammar parsing
match self.ctx.block_contents_grammar {
BlockContentsGrammar::DeclarationList => {}
_ => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
: ^^^
`----

x Expected whitespace, semicolon, EOF, at-keyword or ident token
x Expected whitespace, ';', '@', ident or EOF
,-[$DIR/tests/recovery/at-rule/font-face/input.css:10:5]
10 | 123
: ^^^
`----

x Expected whitespace, semicolon, EOF, at-keyword or ident token
x Expected whitespace, ';', '@', ident or EOF
,-[$DIR/tests/recovery/at-rule/font-face/input.css:14:5]
14 | 123;
: ^^^
Expand Down
32 changes: 12 additions & 20 deletions crates/swc_css_parser/tests/recovery/cdo-and-cdc/output.swc-stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@

x Expected '{'
,-[$DIR/tests/recovery/cdo-and-cdc/input.css:20:5]
20 | color: blue;
: ^
`----

x Expected '{'
,-[$DIR/tests/recovery/cdo-and-cdc/input.css:29:1]
29 | -->;
: ^
`----

x Invalid selector
,-[$DIR/tests/recovery/cdo-and-cdc/input.css:1:1]
1 | <!-- 123 -->
Expand All @@ -21,26 +33,6 @@
10 | }
`----

x Unexpected end of file, but expected '{'
,-[$DIR/tests/recovery/cdo-and-cdc/input.css:14:1]
14 | ,-> <!--
15 | |
16 | | test
17 | |
18 | | -->
19 | |
20 | `-> color: blue;
`----

x Unexpected end of file, but expected '{'
,-[$DIR/tests/recovery/cdo-and-cdc/input.css:25:1]
25 | ,-> <!--
26 | |
27 | | test
28 | |
29 | `-> -->;
`----

x Unexpected token
,-[$DIR/tests/recovery/cdo-and-cdc/input.css:5:1]
5 | <!--
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

x Unexpected end of file, but expected '{'
x Expected '{'
,-[$DIR/tests/recovery/declaration/wrong-name/input.css:2:5]
2 | 20: red;
: ^^^^^^^
: ^
`----

x Unexpected token
Expand Down

0 comments on commit 03bbbd5

Please sign in to comment.