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

Expected string, url or function token

Error message

Expected string, url or function token

What it means

Fired while parsing the prelude of an @import at-rule: after skipping whitespace, the parser looked at the current token to read the import href and it was neither a string nor a url(...) token. Per CSS syntax, @import's href must be a <string> or a <url>; anything else (e.g. an identifier or bare delimiter) is rejected. The input at fault is a malformed @import prelude such as `@import foo;`.

Source

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

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

                    return Err(Error::new(span, ErrorKind::Expected("'{' token")));
                }

                None
            }
            "import" => {
                self.input.skip_ws();

                let span = self.input.cur_span();
                let href = Box::new(match cur!(self) {
                    tok!("string") => ImportHref::Str(self.parse()?),
                    tok!("url") => ImportHref::Url(self.parse()?),
                    // TODO why we need it?
                    tok!("function") => ImportHref::Url(self.parse()?),
                    _ => {
                        return Err(Error::new(
                            span,
                            ErrorKind::Expected("string, url or function token"),
                        ))
                    }
                });

                self.input.skip_ws();

                let layer_name = if !is!(self, EOF) {
                    match cur!(self) {
                        Token::Ident { value, .. }
                            if matches_eq_ignore_ascii_case!(value, "layer") =>
                        {
                            let name = ImportLayerName::Ident(self.parse()?);

                            self.input.skip_ws();

                            Some(Box::new(name))

View on GitHub (pinned to 5176682b65)

Solutions

  1. Write the @import target as a string or url()
  2. Report the error from the parse result
  3. Skip the invalid at-rule
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/swc_css_parser/src/parser/at_rules/mod.rs:160 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/52cec1f7549e0f92. Report an issue: GitHub.