diff --git a/ecmascript/transforms/src/compat/es2015/computed_props/mod.rs b/ecmascript/transforms/src/compat/es2015/computed_props/mod.rs index a551fd70fca..ae98fb8e112 100644 --- a/ecmascript/transforms/src/compat/es2015/computed_props/mod.rs +++ b/ecmascript/transforms/src/compat/es2015/computed_props/mod.rs @@ -278,7 +278,7 @@ fn is_complex(props: &[PropOrSpread]) -> bool { impl> Fold> for ComputedProps where - Vec: FoldWith, + T: FoldWith + FoldWith, { fn fold(&mut self, stmts: Vec) -> Vec { // Fast path when there's no computed properties. @@ -291,26 +291,21 @@ where let mut buf = Vec::with_capacity(stmts.len()); for stmt in stmts { - match stmt.try_into_stmt() { - Err(module_item) => buf.push(module_item), - Ok(stmt) => { - let mut folder = ObjectLitFolder::default(); - let stmt = stmt.fold_with(&mut folder); + let mut folder = ObjectLitFolder::default(); + let stmt = stmt.fold_with(&mut folder); - // Add variable declaration - // e.g. var ref - if !folder.vars.is_empty() { - buf.push(T::from_stmt(Stmt::Decl(Decl::Var(VarDecl { - span: DUMMY_SP, - kind: VarDeclKind::Var, - decls: folder.vars, - declare: false, - })))); - } - - buf.push(T::from_stmt(stmt)); - } + // Add variable declaration + // e.g. var ref + if !folder.vars.is_empty() { + buf.push(T::from_stmt(Stmt::Decl(Decl::Var(VarDecl { + span: DUMMY_SP, + kind: VarDeclKind::Var, + decls: folder.vars, + declare: false, + })))); } + + buf.push(stmt); } buf diff --git a/ecmascript/transforms/src/compat/es2015/computed_props/tests.rs b/ecmascript/transforms/src/compat/es2015/computed_props/tests.rs index 6a37a07db1b..218f372c283 100644 --- a/ecmascript/transforms/src/compat/es2015/computed_props/tests.rs +++ b/ecmascript/transforms/src/compat/es2015/computed_props/tests.rs @@ -1,5 +1,19 @@ use super::*; +test!( + ::swc_ecma_parser::Syntax::default(), + |_| ComputedProps, + issue_210, + " +const b = {[a]: 1} +export const c = {[a]: 1} +", + "const b = _defineProperty({ +}, a, 1); +export const c = _defineProperty({ +}, a, 1);" +); + test!( ::swc_ecma_parser::Syntax::default(), |_| ComputedProps,