Repository: swc-project/swc
File: crates/swc_ecma_transformer/src/es2017/async_to_generator.rs (Rust)

Code before:
            for prop in &mut obj.props {
                match prop {
                    ObjectPatProp::KeyValue(kv) => {
                        if let PropName::Computed(computed) = &mut kv.key {
                            replace_this_in_expr(&mut computed.expr, this_var);
                        }
                        replace_this_in_pat_inner(&mut kv.value, this_var);
                    }
                    ObjectPatProp::Assign(assign) => {
                        if let Some(value) = &mut assign.value {
                            replace_this_in_expr(value, this_var);
                        }
                    }
                    ObjectPatProp::Rest(rest) => {
                        replace_this_in_pat_inner(&mut rest.arg, this_var);
                    }
                    #[cfg(swc_ast_unknown)]
                    _ => {}
                }
            }
        }
        AssignTargetPat::Invalid(_) => {}
        #[cfg(swc_ast_unknown)]
        _ => {}
    }
}

fn replace_this_in_pat_inner(pat: &mut Pat, this_var: &Ident) {
    match pat {
        Pat::Array(array) => {
            for elem in array.elems.iter_mut().flatten() {
                replace_this_in_pat_inner(elem, this_var);
            }
        }
        Pat::Object(obj) => {
            for prop in &mut obj.props {
                match prop {
                    ObjectPatProp::KeyValue(kv) => {
                        if let PropName::Computed(computed) = &mut kv.key {
                            replace_this_in_expr(&mut computed.expr, this_var);
                        }
                        replace_this_in_pat_inner(&mut kv.value, this_var);
                    }
                    ObjectPatProp::Assign(assign) => {
                        if let Some(value) = &mut assign.value {
                            replace_this_in_expr(value, this_var);
                        }
                    }
                    ObjectPatProp::Rest(rest) => {
                        replace_this_in_pat_inner(&mut rest.arg, this_var);
                    }

Review comment (refactor):
There is significant code duplication between `replace_this_in_pat` and `replace_this_in_pat_inner` for handling `ObjectPatProp` cases. Both functions have identical logic for processing `ObjectPatProp::KeyValue`, `ObjectPatProp::Assign`, and `ObjectPatProp::Rest` variants (lines 1263-1279 and 1299-1316). Consider extracting this shared logic into a helper function to improve maintainability and reduce duplication.

Code after:
        }
        AssignTargetPat::Object(obj) => {
            for prop in &mut obj.props {
                match prop {
                    ObjectPatProp::KeyValue(kv) => {
                        if let PropName::Computed(computed) = &mut kv.key {
                            replace_this_in_expr(&mut computed.expr, this_var);
                        }
                        replace_this_in_pat_inner(&mut kv.value, this_var);
                    }
                    ObjectPatProp::Assign(assign) => {
                        if let Some(value) = &mut assign.value {
                            replace_this_in_expr(value, this_var);
                        }
                    }
                    ObjectPatProp::Rest(rest) => {
                        replace_this_in_pat_inner(&mut rest.arg, this_var);
                    }
                    #[cfg(swc_ast_unknown)]
                    _ => {}
                }
            }
        }
        AssignTargetPat::Invalid(_) => {}
        #[cfg(swc_ast_unknown)]
        _ => {}
    }
}

fn replace_this_in_pat_inner(pat: &mut Pat, this_var: &Ident) {
    match pat {
        Pat::Array(array) => {
            for elem in array.elems.iter_mut().flatten() {
                replace_this_in_pat_inner(elem, this_var);
            }
        }
        Pat::Object(obj) => {
            for prop in &mut obj.props {
                match prop {
                    ObjectPatProp::KeyValue(kv) => {
                        if let PropName::Computed(computed) = &mut kv.key {
                            replace_this_in_expr(&mut computed.expr, this_var);
                        }
                        replace_this_in_pat_inner(&mut kv.value, this_var);
                    }
                    ObjectPatProp::Assign(assign) => {
                        if let Some(value) = &mut assign.value {
                            replace_this_in_expr(value, this_var);
                        }
                    }
                    ObjectPatProp::Rest(rest) => {