Repository: swc-project/swc (rust)

Issue:
Minification bug with return and preserveAllComments
### Describe the bug

This bug is related to #4233, but for `return` instead of `throw`.

### Input code

```typescript
export function left() {
    // left binop
    if (!check()) action();
    return foo();
}
```


### Config

```json
{
    "jsc": {
        "parser": {
            "syntax": "ecmascript"
        },
        "preserveAllComments": true,
        "target": "es2017",
        "minify": {
            "compress": true
        }
    }
}
```


### Playground link

_No response_

### Expected behavior

```ts
export function left() {
    return(// left binop
    check() || action(), foo());
}
```

### Actual behavior

However, the output is currently:
```ts
export function left() {
    return // left binop
    check() || action(), foo();
}
```
Note: no parentheses around the returned expression.

### Version

1.2.194

### Additional context

_No response_

Pull request:
fix(es/codegen): Handle comments inside binop expression
<!-- Note: CI script will automatically rebase your PR so please do not rebase unless required -->

<!-- Thanks for submitting a pull request! Please provide enough information so that others can review your pull request. -->

**Description:**

<!-- Explain the **motivation** for making this change. What existing problem does the pull request solve? -->

**BREAKING CHANGE:**

<!--
If this PR introduces a breaking change, it must contain a notice for it to be included in the CHANGELOG. Add description or remove entirely if not breaking.

You may need to update `MIGRATION.md` for the breaking changes.
-->

**Related issue (if exists):**
Fixes #4816



Patch:
diff --git a/crates/swc_ecma_codegen/src/lib.rs b/crates/swc_ecma_codegen/src/lib.rs
index acf8879da2..ccf3bca853 100644
--- a/crates/swc_ecma_codegen/src/lib.rs
+++ b/crates/swc_ecma_codegen/src/lib.rs
@@ -2706,6 +2706,12 @@ where
                 }
             }
 
+            Expr::Bin(e) => {
+                if self.has_leading_comment(&e.left) {
+                    return true;
+                }
+            }
+
             Expr::Seq(e) => {
                 if let Some(e) = e.exprs.first() {
                     if self.has_leading_comment(e) {


Test patch:
diff --git a/crates/swc/tests/fixture/issues-4xxx/4816/1/input/.swcrc b/crates/swc/tests/fixture/issues-4xxx/4816/1/input/.swcrc
new file mode 100644
index 0000000000..6885a3b01f
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-4xxx/4816/1/input/.swcrc
@@ -0,0 +1,12 @@
+{
+    "jsc": {
+        "parser": {
+            "syntax": "ecmascript"
+        },
+        "preserveAllComments": true,
+        "target": "es2017",
+        "minify": {
+            "compress": true
+        }
+    }
+}
diff --git a/crates/swc/tests/fixture/issues-4xxx/4816/1/input/index.js b/crates/swc/tests/fixture/issues-4xxx/4816/1/input/index.js
new file mode 100644
index 0000000000..4513d6dc5c
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-4xxx/4816/1/input/index.js
@@ -0,0 +1,23 @@
+export function left() {
+    // left binop
+    if (!check()) action();
+    return foo();
+}
+
+export function right() {
+    if (!check())
+        // right binop
+        action();
+    return foo();
+}
+
+export function between() {
+    if (!check()) action();
+    // between
+    return foo();
+}
+
+export function end() {
+    if (!check()) action();
+    return foo(); // end
+}
diff --git a/crates/swc/tests/fixture/issues-4xxx/4816/1/output/index.js b/crates/swc/tests/fixture/issues-4xxx/4816/1/output/index.js
new file mode 100644
index 0000000000..7b25108ddd
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-4xxx/4816/1/output/index.js
@@ -0,0 +1,15 @@
+export function left() {
+    return(// left binop
+    check() || action(), foo());
+}
+export function right() {
+    return check() || // right binop
+    action(), foo();
+}
+export function between() {
+    // between
+    return check() || action(), foo();
+}
+export function end() {
+    return check() || action(), foo();
+} // end
diff --git a/crates/swc/tests/fixture/issues-4xxx/4816/2/input/.swcrc b/crates/swc/tests/fixture/issues-4xxx/4816/2/input/.swcrc
new file mode 100644
index 0000000000..6885a3b01f
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-4xxx/4816/2/input/.swcrc
@@ -0,0 +1,12 @@
+{
+    "jsc": {
+        "parser": {
+            "syntax": "ecmascript"
+        },
+        "preserveAllComments": true,
+        "target": "es2017",
+        "minify": {
+            "compress": true
+        }
+    }
+}
diff --git a/crates/swc/tests/fixture/issues-4xxx/4816/2/input/index.js b/crates/swc/tests/fixture/issues-4xxx/4816/2/input/index.js
new file mode 100644
index 0000000000..3b43274502
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-4xxx/4816/2/input/index.js
@@ -0,0 +1,15 @@
+export function left() {
+    return (
+        // left binop
+        check() || action(), foo()
+    );
+}
+
+export function right() {
+    return (
+        check() ||
+            // right binop
+            action(),
+        foo()
+    );
+}
diff --git a/crates/swc/tests/fixture/issues-4xxx/4816/2/output/index.js b/crates/swc/tests/fixture/issues-4xxx/4816/2/output/index.js
new file mode 100644
index 0000000000..c1a661e526
--- /dev/null
+++ b/crates/swc/tests/fixture/issues-4xxx/4816/2/output/index.js
@@ -0,0 +1,8 @@
+export function left() {
+    return(// left binop
+    check() || action(), foo());
+}
+export function right() {
+    return check() || // right binop
+    action(), foo();
+}