diff --git a/packages/babel-generator/src/printer.ts b/packages/babel-generator/src/printer.ts
--- a/packages/babel-generator/src/printer.ts
+++ b/packages/babel-generator/src/printer.ts
@@ -637,7 +637,10 @@ class Printer {
     } else {
       shouldPrintParens = needsParens(node, parent, this._printStack);
     }
-    if (shouldPrintParens) this.token("(");
+    if (shouldPrintParens) {
+      this.token("(");
+      this._endsWithInnerRaw = false;
+    }
 
     this._lastCommentLine = 0;
 

diff --git a/packages/babel-generator/test/fixtures/comments/inner-comments-auto-parens/input.js b/packages/babel-generator/test/fixtures/comments/inner-comments-auto-parens/input.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-generator/test/fixtures/comments/inner-comments-auto-parens/input.js
@@ -0,0 +1,3 @@
+1 + ((/* c */) => {});
+
+(function /* c */ () {});
diff --git a/packages/babel-generator/test/fixtures/comments/inner-comments-auto-parens/output.js b/packages/babel-generator/test/fixtures/comments/inner-comments-auto-parens/output.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-generator/test/fixtures/comments/inner-comments-auto-parens/output.js
@@ -0,0 +1,2 @@
+1 + (( /* c */) => {});
+(function /* c */ () {});
\ No newline at end of file

[Bug]: Inner comments for anonymous functions/classes printed in leading position
### 💻

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

### How are you using Babel?

Programmatic API (`babel.transform`, `babel.parse`)

### Input code

```js
const {parse} = require('@babel/parser'),
  generate = require('@babel/generator').default;

{
  const ast = parse('(function() {})');
  const fnNode = ast.program.body[0].expression;
  fnNode.innerComments = [{type: 'CommentBlock', value: 'foo'}];
  console.log(generate(ast, {compact: true}).code);
  // Prints (/*foo*/function(){});
}

{
  const ast = parse('(class {})');
  const classNode = ast.program.body[0].expression;
  classNode.innerComments = [{type: 'CommentBlock', value: 'foo'}];
  console.log(generate(ast, {compact: true}).code);
  // Prints (/*foo*/class{});
}
```

### Configuration file name

_No response_

### Configuration

n/a

### Current and expected behavior

Previous to 7.20.1, `innerComments` were printed after the `function` or `class` keyword. Now they are printed before.

Previously: `function /*foo*/ () {}`
Now: `/*foo*/function() {}`

So now there's no difference between `innerComments` and `leadingComments` in these cases. In my opinion the old behavior made more sense ("inner" implies that position of comment is somewhere inside the node).

I assume this change occurred in #15080.

### Environment

System:
  OS: macOS 12.4
Binaries:
  Node: 18.12.1 - ~/.nvm/versions/node/v18.12.1/bin/node
  npm: 8.19.2 - ~/.nvm/versions/node/v18.12.1/bin/npm
npmPackages:
  @babel/core: ^7.20.2 => 7.20.2
  @babel/generator: ^7.20.2 => 7.20.2
  @babel/helper-module-transforms: ^7.20.2 => 7.20.2
  @babel/parser: ^7.20.2 => 7.20.2
  @babel/plugin-transform-arrow-functions: ^7.18.6 => 7.18.6
  @babel/plugin-transform-modules-commonjs: ^7.19.6 => 7.19.6
  @babel/plugin-transform-react-jsx: ^7.19.0 => 7.19.0
  @babel/plugin-transform-strict-mode: ^7.18.6 => 7.18.6
  @babel/traverse: ^7.20.1 => 7.20.1
  @babel/types: ^7.20.2 => 7.20.2
  babel-plugin-dynamic-import-node: ^2.3.3 => 2.3.3
  eslint: ^8.26.0 => 8.26.0
  jest: ^29.2.2 => 29.2.2

### Possible solution

_No response_

### Additional context

_No response_

Hey @overlookmotel! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant [Slack community](https://babeljs.slack.com) that typically always has someone willing to help. You can sign-up [here](https://slack.babeljs.io/) for an invite.
https://github.com/nicolo-ribaudo/babel/blob/d382865d5527ed5914786f978b9b85359bd04d2b/packages/babel-generator/src/printer.ts#L642
It seems we need to set `this._endsWithInnerRaw = false;` here.
Also found a case where the inner comments don't get printed at all:

```js
const ast = parse('x = class {};');
const classNode = ast.program.body[0].expression.right;
classNode.innerComments = [{type: 'CommentBlock', value: 'foo'}];
console.log(generate(ast).code);
// Prints x = class {};
```
In `class {}` there isn't really a position where you would find inner comments of `ClassExpression`. If you try parsing `/* 1 */ class /* 2 */ { /* 3 */ } /* 4 */`, you see:
- 1 is a leading comment of the class expression
- 2 is a leading comment of the class body
- 3 is an inner comment of the class body
- 4 is a trailing comment of the class expression

As a workaround, we could print inner comments in trailing positions.
packages/babel-generator/test/printer.skip-bundled.js packages/babel-generator/test/arrow-functions.js
packages/babel-generator/test/index.js