swc-project/swc · error · SyntaxError

Tried to parse an argument of yield

Error message

Tried to parse an argument of yield

What it means

Labeled syntax error from parse_yield_expr: a yield expression was found inside the FormalParameters of a generator function (Context::InParameters without InFunction). The spec forbids this because parameter expressions are evaluated before the generator object is resumable; the label clarifies the failing sub-parse of the yield argument.

Source

Thrown at crates/swc_ecma_lexer/src/common/parser/expr.rs:122

fn parse_yield_expr<'a, P: Parser<'a>>(p: &mut P) -> PResult<Box<Expr>> {
    let start = p.input().cur_pos();
    p.assert_and_bump(&P::Token::YIELD);
    debug_assert!(p.ctx().contains(Context::InGenerator));

    // Spec says
    // YieldExpression cannot be used within the FormalParameters of a generator
    // function because any expressions that are part of FormalParameters are
    // evaluated before the resulting generator object is in a resumable state.
    if p.ctx().contains(Context::InParameters) && !p.ctx().contains(Context::InFunction) {
        syntax_error!(p, p.input().prev_span(), SyntaxError::YieldParamInGen)
    }

    let parse_with_arg = |p: &mut P| {
        let has_star = p.input_mut().eat(&P::Token::MUL);
        let err_span = p.span(start);
        let arg = parse_assignment_expr(p).map_err(|err| {
            Error::new(
                err.span(),
                SyntaxError::WithLabel {
                    inner: Box::new(err),
                    span: err_span,
                    note: "Tried to parse an argument of yield",
                },
            )
        })?;
        Ok(YieldExpr {
            span: p.span(start),
            arg: Some(arg),
            delegate: has_star,
        }
        .into())
    };

    if p.is_general_semi() || {
        let cur = p.input().cur();

View on GitHub (pinned to 5176682b65)

Solutions

  1. Move the yield expression out of the generator's parameter list into the function body
  2. Wrap the inner function (non-generator or arrow) so yield is legal in that scope
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/swc_ecma_lexer/src/common/parser/expr.rs:122 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/52c6618896ca3e54. Report an issue: GitHub.