Repository: babel/babel (TypeScript)

Issue:
[Bug]: With `comments: false`, babel inserts an extra newline before the `=>` of an arrow function
### 💻

- [ ] Would you like to work on a fix?

### How are you using Babel?

@babel/cli

### Input code

```js
var test = (/* placeholder */) => {
  /* Unused */
}
```

### Configuration file name

babel.config.json

### Configuration

```json
{
  "sourceType": "script",
  "presets": ["@babel/preset-env"],
  "comments": false
}
```
Browserslist is set to `["defaults"]`.

### Current and expected behavior

With `comments: false`, the following output is produced:
```js
var test = (
)
=> {
};
```
which is not valid code. With `comments: true`, the following output is produced:
```js
var test = ( /* placeholder */
) => {
  /* Unused */
};
```
which is valid. With `@babel/generator` version `7.20.2` and `comments: false`, the following output is produced:
```js
var test = (
) => {
};
```
which is valid, so this looks like a regression in version `7.20.3` (from https://github.com/babel/babel/pull/15135?).

### Environment

```
System:
    OS: Windows 10 10.0.19044
  Binaries:
    Node: 18.12.1 - C:\Program Files\nodejs\node.EXE
    npm: 8.19.3 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    @babel/cli: ^7.19.3 => 7.19.3
    @babel/core: ^7.20.2 => 7.20.2
    @babel/generator: ^7.20.3 => 7.20.3
    @babel/parser: ^7.20.3 => 7.20.3
    @babel/preset-env: ^7.20.2 => 7.20.2
```

### Possible solution

_No response_

### Additional context

Maybe https://github.com/babel/babel/pull/15160 will also fix this, but there seems to be a gap in testing with `comments: false`.


Hints:

Patch:
diff --git a/packages/babel-generator/src/generators/methods.ts b/packages/babel-generator/src/generators/methods.ts
--- a/packages/babel-generator/src/generators/methods.ts
+++ b/packages/babel-generator/src/generators/methods.ts
@@ -11,7 +11,10 @@ export function _params(
   this._parameters(node.params, node);
   this.token(")");
 
-  this.print(node.returnType, node, node.type === "ArrowFunctionExpression");
+  const noLineTerminator = node.type === "ArrowFunctionExpression";
+  this.print(node.returnType, node, noLineTerminator);
+
+  this._noLineTerminator = noLineTerminator;
 }
 
 export function _parameters(


Test patch:
diff --git a/packages/babel-generator/test/fixtures/regression/15161/input.js b/packages/babel-generator/test/fixtures/regression/15161/input.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-generator/test/fixtures/regression/15161/input.js
@@ -0,0 +1,3 @@
+var test = (/* placeholder */) => {
+  /* Unused */
+}
diff --git a/packages/babel-generator/test/fixtures/regression/15161/options.json b/packages/babel-generator/test/fixtures/regression/15161/options.json
new file mode 100644
--- /dev/null
+++ b/packages/babel-generator/test/fixtures/regression/15161/options.json
@@ -0,0 +1,3 @@
+{
+  "comments": false
+}
diff --git a/packages/babel-generator/test/fixtures/regression/15161/output.js b/packages/babel-generator/test/fixtures/regression/15161/output.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-generator/test/fixtures/regression/15161/output.js
@@ -0,0 +1,3 @@
+var test = (
+) => {
+};
\ No newline at end of file
diff --git a/packages/babel-generator/test/index.js b/packages/babel-generator/test/index.js
--- a/packages/babel-generator/test/index.js
+++ b/packages/babel-generator/test/index.js
@@ -1151,7 +1151,7 @@ describe("programmatic generation", function () {
       expect(generate(expr).code).toMatchInlineSnapshot(`"1 + 2 /*foo*/"`);
     });
 
-    it("comment skipped because of newlines", () => {
+    it("comment skipped in arrow function because of newlines", () => {
       const arrow = t.arrowFunctionExpression(
         [t.identifier("x"), t.identifier("y")],
         t.identifier("z"),
@@ -1166,6 +1166,23 @@ describe("programmatic generation", function () {
         line*/"
       `);
     });
+
+    it("comment in arrow function with return type", () => {
+      const arrow = t.arrowFunctionExpression(
+        [t.identifier("x"), t.identifier("y")],
+        t.identifier("z"),
+      );
+      arrow.returnType = t.tsTypeAnnotation(t.tsAnyKeyword());
+      arrow.returnType.trailingComments = [
+        { type: "CommentBlock", value: "foo" },
+        // This comment is dropped. There is no way to safely print it
+        // as a trailingComment of the return type.
+        { type: "CommentBlock", value: "new\nline" },
+      ];
+      expect(generate(arrow).code).toMatchInlineSnapshot(
+        `"(x, y): any /*foo*/ => z"`,
+      );
+    });
   });
 });