Repository: babel/babel (TypeScript)

Issue:
[Bug]: Unable to use pako in create-react-app 5 with latest babel v7.20.x
### 💻

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

### How are you using Babel?

Other (Next.js, Gatsby, vue-cli, ...)

### Input code

The file from pako is node_modules/pako/dist/pako.esm.mjs   

```
const gen_bitlen = (s, desc) =>
//    hello_deflate_state *s;
//    tree_desc *desc;    /* the tree descriptor */
{
  const tree            = desc.dyn_tree;
  const max_code        = desc.max_code;
 
```

The error from a build is

```
$ yarn build
yarn run v1.22.18
$ react-scripts build
Creating an optimized production build...
Failed to compile.

Module parse failed: Unexpected token (255:129)
File was processed with these loaders:
 * ./node_modules/babel-loader/lib/index.js
 * ./node_modules/source-map-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
|  *     not null.
|  */
> var gen_bitlen = function gen_bitlen(s, desc) /*    hello_deflate_state *s;*/ /*    tree_desc *desc;    /* the tree descriptor */*/{
|   var tree = desc.dyn_tree;
|   var max_code = desc.max_code;


```

### Configuration file name

package.json

### Configuration

I use a default version of installing create-react-app with yarn.lock in https://github.com/cmdcolin/pako_error_in_cra5

Can eject the cra config to see details

### Current and expected behavior

This I believe is a fairly recently change, and I'm not certain it's babel that does this type of conversion of comment formats. If this is not true I can redirect this issue elsewhere!

The code worked when my yarn.lock has e.g. ~7.19.x versions of babel packages, but started failing now at ~7.20.x. there are many babel packages so I'm not sure which but a diff between working and non-working is at this pastebin https://pastebin.com/xe3HSAgW



### Environment

- babel 7.20.x (failing) babel 7.19.x (working)
- node 18.20
- yarn 1.22.18
- ubuntu 22.10
- not a monorepo but testing in a create react app

reproducible repo here https://github.com/cmdcolin/pako_error_in_cra5

### Possible solution

_No response_

### Additional context

_No response_


Hints:
Hey @cmdcolin! 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.
Might be dupe of https://github.com/babel/babel/issues/15111 can close if so!
i'll just close my own issue, it's very likely a dupe :)
It seems like a new issue.

The line comment `//    tree_desc *desc;    /* the tree descriptor */` is valid, however Babel generates a block comment `/* tree_desc /* tree descriptor */ */` which completely messes up with the `/* */` pair within the line comment.
@cmdcolin The fix of #15111 has been published in 7.20.2, can you reproduce this issue on 7.20.2?
I think https://github.com/cmdcolin/pako_error_in_cra5 is using 7.20.2 of @babel/core

can see in yarn.lock for details

and it does still produce the error. do you think this issue should re-open?

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
@@ -10,7 +10,6 @@ export function _params(
   this.token("(");
   this._parameters(node.params, node);
   this.token(")");
-  this._noLineTerminator = true;
 
   this.print(node.returnType, node, node.type === "ArrowFunctionExpression");
 }
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
@@ -23,6 +23,7 @@ const ZERO_DECIMAL_INTEGER = /\.0+$/;
 const NON_DECIMAL_LITERAL = /^0[box]/;
 const PURE_ANNOTATION_RE = /^\s*[@#]__PURE__\s*$/;
 const HAS_NEWLINE = /[\n\r\u2028\u2029]/;
+const HAS_BlOCK_COMMENT_END = /\*\//;
 
 const { needsParens } = n;
 
@@ -904,7 +905,13 @@ class Printer {
 
     if (this._printedComments.has(comment)) return false;
 
-    if (this._noLineTerminator && HAS_NEWLINE.test(comment.value)) {
+    const noLineTerminator = this._noLineTerminator;
+
+    if (
+      noLineTerminator &&
+      (HAS_NEWLINE.test(comment.value) ||
+        HAS_BlOCK_COMMENT_END.test(comment.value))
+    ) {
       return true;
     }
 
@@ -957,7 +964,7 @@ class Printer {
 
         val = val.replace(/\n(?!$)/g, `\n${" ".repeat(indentSize)}`);
       }
-    } else if (!this._noLineTerminator) {
+    } else if (!noLineTerminator) {
       val = `//${comment.value}`;
     } else {
       // It was a single-line comment, so it's guaranteed to not
@@ -972,7 +979,7 @@ class Printer {
     this.source("start", comment.loc);
     this._append(val, isBlockComment);
 
-    if (!isBlockComment && !this._noLineTerminator) {
+    if (!isBlockComment && !noLineTerminator) {
       this.newline(1, true);
     }
 
@@ -997,12 +1004,16 @@ class Printer {
     const nodeEndLine = hasLoc ? nodeLoc.end.line : 0;
     let lastLine = 0;
     let leadingCommentNewline = 0;
-    const { _noLineTerminator } = this;
+
+    const maybeNewline = this._noLineTerminator
+      ? function () {}
+      : this.newline.bind(this);
 
     for (let i = 0; i < len; i++) {
       const comment = comments[i];
 
-      if (hasLoc && comment.loc && !this._printedComments.has(comment)) {
+      const printed = this._printedComments.has(comment);
+      if (hasLoc && comment.loc && !printed) {
         const commentStartLine = comment.loc.start.line;
         const commentEndLine = comment.loc.end.line;
         if (type === COMMENT_TYPE.LEADING) {
@@ -1022,11 +1033,11 @@ class Printer {
           }
           lastLine = commentEndLine;
 
-          if (!_noLineTerminator) this.newline(offset);
+          maybeNewline(offset);
           this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL);
 
-          if (!_noLineTerminator && i + 1 === len) {
-            this.newline(
+          if (i + 1 === len) {
+            maybeNewline(
               Math.max(nodeStartLine - lastLine, leadingCommentNewline),
             );
             lastLine = nodeStartLine;
@@ -1036,11 +1047,11 @@ class Printer {
             commentStartLine - (i === 0 ? nodeStartLine : lastLine);
           lastLine = commentEndLine;
 
-          if (!_noLineTerminator) this.newline(offset);
+          maybeNewline(offset);
           if (this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL)) break;
 
-          if (!_noLineTerminator && i + 1 === len) {
-            this.newline(Math.min(1, nodeEndLine - lastLine)); // TODO: Improve here when inner comments processing is stronger
+          if (i + 1 === len) {
+            maybeNewline(Math.min(1, nodeEndLine - lastLine)); // TODO: Improve here when inner comments processing is stronger
             lastLine = nodeEndLine;
           }
         } else {
@@ -1048,16 +1059,18 @@ class Printer {
             commentStartLine - (i === 0 ? nodeEndLine - lineOffset : lastLine);
           lastLine = commentEndLine;
 
-          if (!_noLineTerminator) this.newline(offset);
+          maybeNewline(offset);
           this._printComment(comment, COMMENT_SKIP_NEWLINE.SKIP_ALL);
         }
       } else {
         hasLoc = false;
 
+        if (printed) continue;
+
         if (len === 1) {
           const singleLine = comment.loc
             ? comment.loc.start.line === comment.loc.end.line
-            : !comment.value.includes("\n");
+            : !HAS_NEWLINE.test(comment.value);
 
           const shouldSkipNewline =
             singleLine &&
@@ -1091,7 +1104,7 @@ class Printer {
           //   /*:: b: ?string*/
           // }
 
-          const skippedDueToNewlie = this._printComment(
+          const skippedDueToNewline = this._printComment(
             comment,
             i === 0
               ? COMMENT_SKIP_NEWLINE.SKIP_LEADING
@@ -1099,7 +1112,7 @@ class Printer {
               ? COMMENT_SKIP_NEWLINE.SKIP_TRAILING
               : COMMENT_SKIP_NEWLINE.DEFAULT,
           );
-          if (skippedDueToNewlie) break;
+          if (skippedDueToNewline) break;
         } else {
           this._printComment(comment, COMMENT_SKIP_NEWLINE.DEFAULT);
         }


Test patch:
diff --git a/packages/babel-generator/test/fixtures/regression/inner-line-commens-func/input.js b/packages/babel-generator/test/fixtures/regression/inner-line-commens-func/input.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-generator/test/fixtures/regression/inner-line-commens-func/input.js
@@ -0,0 +1,9 @@
+// https://github.com/babel/babel/issues/15132
+
+var gen_bitlen = function gen_bitlen(s, desc) //    deflate_state *s;
+//    tree_desc *desc;    /* the tree descriptor */
+{
+};
+async (
+// 11111 /* */
+) => {};
diff --git a/packages/babel-generator/test/fixtures/regression/inner-line-commens-func/output.js b/packages/babel-generator/test/fixtures/regression/inner-line-commens-func/output.js
new file mode 100644
--- /dev/null
+++ b/packages/babel-generator/test/fixtures/regression/inner-line-commens-func/output.js
@@ -0,0 +1,9 @@
+// https://github.com/babel/babel/issues/15132
+
+var gen_bitlen = function gen_bitlen(s, desc)
+//    deflate_state *s;
+//    tree_desc *desc;    /* the tree descriptor */
+{};
+async (
+  // 11111 /* */
+) => {};
\ No newline at end of file