Repository: swc-project/swc
File: crates/swc_ecma_compat_es2015/src/spread.rs (Rust)

Code before:
        // to it are depth-1 flattened. This effectively implements the loose
        // spread. But if any arrays that are not being spread are present, they
        // will be incorrectly flattened. The solution is to wrap every
        // contiguous slice of non-spread args in an array, which will protect
        // array args from being flattened.
        if self.c.loose {
            let mut arg_list = Vec::new();
            let mut current_elems = Vec::new();
            for arg in args.flatten() {
                let expr = arg.expr;
                match arg.spread {
                    Some(span) => {
                        if !current_elems.is_empty() {
                            arg_list.push(
                                ArrayLit {
                                    span: DUMMY_SP,
                                    elems: current_elems,
                                }
                                .as_arg(),
                            );
                            current_elems = Vec::new();
                        }
                        // Special handling for `arguments` to call Array.prototype.slice
                        // https://github.com/babel/babel/blob/61ad8555b875cb0c0996f18f803b6bf1d2150173/packages/babel-plugin-transform-spread/src/index.ts#L43-L47
                        let expr = match *expr {
                            Expr::Ident(Ident { ref sym, .. }) if &**sym == "arguments" => {
                                CallExpr {
                                    span,
                                    callee: member_expr!(
                                        Default::default(),
                                        DUMMY_SP,
                                        Array.prototype.slice.call
                                    )
                                    .as_callee(),
                                    args: vec![expr.as_arg()],
                                    ..Default::default()
                                }
                                .into()
                            }
                            _ => *expr,
                        };
                        arg_list.push(expr.as_arg());
                    }
                    None => {
                        current_elems.push(Some(expr.as_arg()));
                    }
                }
            }
            if !current_elems.is_empty() {
                arg_list.push(
                    ArrayLit {

Review comment (refactor):
[nitpick] Consider extracting the string literal 'arguments' into a constant to avoid magic strings and improve maintainability.

Code after:
        // Shorthand [].concat(arr1, arr2) should be used under loose mode.
        // Array.prototype.concat has the lovely feature by which arrays passed
        // to it are depth-1 flattened. This effectively implements the loose
        // spread. But if any arrays that are not being spread are present, they
        // will be incorrectly flattened. The solution is to wrap every
        // contiguous slice of non-spread args in an array, which will protect
        // array args from being flattened.
        if self.c.loose {
            let mut arg_list = Vec::new();
            let mut current_elems = Vec::new();
            for arg in args.flatten() {
                let expr = arg.expr;
                match arg.spread {
                    Some(span) => {
                        if !current_elems.is_empty() {
                            arg_list.push(
                                ArrayLit {
                                    span: DUMMY_SP,
                                    elems: current_elems,
                                }
                                .as_arg(),
                            );
                            current_elems = Vec::new();
                        }
                        // Special handling for `arguments` to call Array.prototype.slice
                        // https://github.com/babel/babel/blob/61ad8555b875cb0c0996f18f803b6bf1d2150173/packages/babel-plugin-transform-spread/src/index.ts#L43-L47
                        let expr = match *expr {
                            Expr::Ident(Ident { ref sym, ctxt, .. })
                                if &**sym == "arguments" && ctxt == self.unresolved_ctxt =>
                            {
                                CallExpr {
                                    span,
                                    callee: member_expr!(
                                        Default::default(),
                                        DUMMY_SP,
                                        Array.prototype.slice.call
                                    )
                                    .as_callee(),
                                    args: vec![expr.as_arg()],
                                    ..Default::default()
                                }
                                .into()
                            }
                            _ => *expr,
                        };
                        arg_list.push(expr.as_arg());
                    }
                    None => {
                        current_elems.push(Some(expr.as_arg()));
                    }
                }