swc-project/swc · error

computed spread property

Error message

computed spread property

What it means

The ES2015 `computed_props` pass rewrites object literals with computed keys into `_define_property` helper calls. While iterating the literal's properties it only handles KeyValue, Shorthand and Method props; a `PropOrSpread::Spread` element (object spread/rest, an ES2018 feature) reaches `unimplemented!` because the pass cannot express a spread as a defineProperty sequence.

Source

Thrown at crates/swc_ecma_compat_es2015/src/computed_props.rs:225

                                }
                                .into(),
                            );

                            continue;
                            // unimplemented!("getter /setter property")
                        }
                        Prop::Method(MethodProp { key, function }) => (
                            prop_name_to_expr(key, self.c.loose),
                            FnExpr {
                                ident: None,
                                function,
                            }
                            .into(),
                        ),
                        #[cfg(swc_ast_unknown)]
                        _ => panic!("unable to access unknown nodes"),
                    },
                    PropOrSpread::Spread(..) => unimplemented!("computed spread property"),
                    #[cfg(swc_ast_unknown)]
                    _ => panic!("unable to access unknown nodes"),
                };

                if !self.c.loose && props_cnt == 1 {
                    single_cnt_prop = Some(
                        CallExpr {
                            span,
                            callee: helper!(define_property),
                            args: vec![exprs.pop().unwrap().as_arg(), key.as_arg(), value.as_arg()],
                            ..Default::default()
                        }
                        .into(),
                    );
                    break;
                }
                exprs.push(if self.c.loose {
                    let left = if is_compute {

View on GitHub (pinned to d7d7434666)

Solutions

  1. Run `es2018::object_rest_spread(...)` before `es2015::computed_props(...)` in the pass chain, or use the built-in es2015 preset which already pairs them
  2. Raise the compilation target to ES2018+ so neither pass runs
  3. Pre-transpile object spread with another tool before feeding the code to swc

Example fix

// Rust pass chain
// before
let passes = vec![computed_props(config)];

// after
let passes = vec![object_rest_spread(config), computed_props(config)];
Defensive patterns

Strategy: validation

Validate before calling

// Rust: check for spread in object literals before running computed_props
fn object_literal_has_spread(e: &Expr) -> bool {
    matches!(e, Expr::Object(ObjectLit { props, .. })
        if props.iter().any(|p| matches!(p, PropOrSpread::Spread(..))))
}

Type guard

fn is_supported_by_computed_props(props: &[PropOrSpread]) -> bool {
    props.iter().all(|p| !matches!(p, PropOrSpread::Spread(..)))
}

Prevention

When it happens

Trigger: Run `es2015::computed_props` over an object literal containing a spread, e.g. `({ [k]: v, ...rest })`, without first lowering spreads with the ES2018 `object_rest_spread` pass.

Common situations: Assembling ES2015 passes manually with incomplete coverage; targeting old browsers while source uses modern syntax; pass-ordering regressions after refactoring a custom transform chain.

Related errors


AI-assisted analysis of swc-project/swc@d7d7434666 (2026-08-16). Data as JSON: /api/errors/a7ba780daf1ba208. Report an issue: GitHub.