swc-project/swc · error · swc_css_parser::Error

Unexpected '{' token

Error message

Unexpected '{' token

What it means

Fired in parse_at_rule_block when the at-rule named in source is `charset`: @charset is defined to have only a prelude (a string) and no block, so encountering a '{' that would start a block is a syntax error. Input at fault is any `@charset "utf-8" { ... }`-style rule that carries a block body.

Source

Thrown at crates/swc_css_parser/src/parser/at_rules/mod.rs:457

        if !is!(self, EOF) {
            let span = self.input.cur_span();

            return Err(Error::new(
                span,
                ErrorKind::Unexpected("tokens in at-rule prelude"),
            ));
        }

        Ok(prelude)
    }

    pub(super) fn parse_at_rule_block(&mut self, name: &str) -> PResult<Vec<ComponentValue>> {
        let block_contents = match name {
            "charset" => {
                let span = self.input.cur_span();

                return Err(Error::new(span, ErrorKind::Unexpected("'{' token")));
            }
            "color-profile" => {
                let declaration_list: Vec<DeclarationOrAtRule> = self.parse()?;
                let declaration_list: Vec<ComponentValue> = declaration_list
                    .into_iter()
                    .map(ComponentValue::from)
                    .collect();

                declaration_list
            }
            "container" => match self.ctx.block_contents_grammar {
                BlockContentsGrammar::StyleBlock => {
                    let ctx = Ctx {
                        in_container_at_rule: true,
                        ..self.ctx
                    };
                    let style_blocks = self.with_ctx(ctx).parse_as::<Vec<StyleBlock>>()?;
                    let style_blocks: Vec<ComponentValue> =

View on GitHub (pinned to 5176682b65)

Solutions

  1. Remove the block after block-less at-rules like @charset or @import
  2. Surface the collected error
  3. Skip the invalid rule
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/swc_css_parser/src/parser/at_rules/mod.rs:457 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/71fe4ec69d0fadd8. Report an issue: GitHub.