swc-project/swc · error

assignment property be removed before generator pass

Error message

assignment property be removed before generator pass

What it means

While emitting assignments for object-literal properties inside a converted generator, the generator pass handles KeyValue and Method props but treats Prop::Assign as impossible. Prop::Assign (`{a}` shorthand-assign / `{a = 1}` cover grammar) only exists in destructuring patterns; in a plain object literal expression it is invalid JavaScript, and the destructuring pass is expected to have normalized any pattern context away before the generator pass.

Source

Thrown at crates/swc_ecma_compat_es2015/src/generator.rs:1326

                    }
                    .into(),
                    right: p.into(),
                }
                .into(),
                Prop::KeyValue(p) => AssignExpr {
                    span: DUMMY_SP,
                    op: op!("="),
                    left: MemberExpr {
                        span: DUMMY_SP,
                        obj: temp.clone().into(),
                        prop: p.key.into(),
                    }
                    .into(),
                    right: p.value,
                }
                .into(),
                Prop::Assign(_) => {
                    unreachable!("assignment property be removed before generator pass")
                }
                Prop::Getter(_) | Prop::Setter(_) => {
                    unreachable!("getter/setter property be compiled as CompiledProp::Accessor")
                }
                Prop::Method(p) => AssignExpr {
                    span: DUMMY_SP,
                    op: op!("="),
                    left: MemberExpr {
                        span: DUMMY_SP,
                        obj: temp.clone().into(),
                        prop: p.key.into(),
                    }
                    .into(),
                    right: p.function.into(),
                }
                .into(),
                #[cfg(swc_ast_unknown)]
                _ => panic!("unable to access unknown nodes"),

View on GitHub (pinned to 5176682b65)

Solutions

  1. Fix the producer of the AST: rewrite Prop::Assign into Prop::KeyValue (and shorthand `{a}` into `{a: a}`) before generator lowering.
  2. If the input is source text, it is invalid JS (`({a = 1})` as an expression) — correct the source or run the standard compat chain which destructures patterns first.
  3. Report an swc bug if plain parsed source reproduces it.

Example fix

// before: pattern cover-grammar node in expression position (invalid)
// ast: ObjectLit { props: [Prop::Assign { key: a, value: Some(1) }] }

// after: normalized key/value property
// ast: ObjectLit { props: [Prop::KeyValue { key: a, value: 1 }] }
// source equivalent: ({ a: 1 })
Defensive patterns

Strategy: validation

Validate before calling

// Object literals in expression position must not contain Prop::Assign
use swc_ecma_ast::*; use swc_ecma_visit::{Visit, VisitWith};
struct AssignPropScan(bool); impl Visit for AssignPropScan {
    fn visit_prop(&mut self, p: &Prop) {
        self.0 |= matches!(p, Prop::Assign(_));
        p.visit_children_with(self);
    }
}
// scan program; if true, normalize Prop::Assign -> Prop::KeyValue first

Type guard

fn is_valid_object_expr(o: &ObjectLit) -> bool {
    o.props.iter().all(|p| !matches!(p, PropOrSpread::Prop(p) if matches!(**p, Prop::Assign(_))))
}

Prevention

When it happens

Trigger: Feeding the generator transform an object expression containing Prop::Assign nodes — i.e. an invalid AST produced by a custom transform/codemod that reused pattern nodes as object literals, or a pipeline where the es2015 destructuring normalization did not run.

Common situations: Codemods converting destructuring patterns into object expressions; hand-built ASTs in tests or FFI; lenient/older parsers emitting cover-grammar nodes in expression position.

Related errors


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