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

Tried to parse the condition for an if statement

Error message

Tried to parse the condition for an if statement

What it means

Diagnostic label applied by parse_if_stmt: the expression between 'if (' and ')' failed to parse; the original error is nested in SyntaxError::WithLabel with this message so the if-condition is identified as the failing region. The inner boxed error holds the true cause.

Source

Thrown at crates/swc_ecma_parser/src/parser/stmt.rs:647

            debug_assert_eq!(cur.alt, None);
            cur.alt = Some(alt);
        }
    }

    fn parse_if_stmt(&mut self) -> PResult<IfStmt> {
        let start = self.cur_pos();

        self.assert_and_bump(Token::If);
        let if_token = self.input().prev_span();

        expect!(self, Token::LParen);

        let test = self
            .do_outside_of_context(Context::IgnoreElseClause, |p| {
                p.allow_in_expr(|p| p.parse_expr())
            })
            .map_err(|err| {
                Error::new(
                    err.span(),
                    SyntaxError::WithLabel {
                        inner: Box::new(err),
                        span: if_token,
                        note: "Tried to parse the condition for an if statement",
                    },
                )
            })?;

        expect!(self, Token::RParen);

        let cons = {
            // Prevent stack overflow
            crate::maybe_grow(256 * 1024, 1024 * 1024, || {
                // // Annex B
                // if !self.ctx().contains(Context::Strict) && self.input().is(Token::FUNCTION)
                // {     // TODO: report error?
                // }

View on GitHub (pinned to 5176682b65)

Solutions

  1. Read the inner error to find the precise syntax fault
  2. Rewrite or complete the condition expression
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/swc_ecma_parser/src/parser/stmt.rs:647 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/f42c5db0becd84a6. Report an issue: GitHub.