Skip to content

Commit b0cf79a

Browse files
authored
fix: Print newlines for leading Comments of TSEnumMember (#15216)
1 parent 65cc66f commit b0cf79a

2 files changed

Lines changed: 34 additions & 1 deletion

File tree

packages/babel-generator/src/printer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
isStatement,
88
isClassBody,
99
isTSInterfaceBody,
10+
isTSEnumDeclaration,
1011
} from "@babel/types";
1112
import type {
1213
RecordAndTuplePluginOptions,
@@ -1103,7 +1104,8 @@ class Printer {
11031104
singleLine &&
11041105
!isStatement(node) &&
11051106
!isClassBody(parent) &&
1106-
!isTSInterfaceBody(parent);
1107+
!isTSInterfaceBody(parent) &&
1108+
!isTSEnumDeclaration(parent);
11071109

11081110
if (type === COMMENT_TYPE.LEADING) {
11091111
this._printComment(

packages/babel-generator/test/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,37 @@ describe("generation", function () {
634634
`);
635635
});
636636

637+
it("comments without loc3", () => {
638+
const ast = parse(
639+
`
640+
/** This describes how the endpoint is implemented when the lease is deployed */
641+
export enum Endpoint_Kind {
642+
/** SHARED_HTTP - Describes an endpoint that becomes a Kubernetes Ingress */
643+
SHARED_HTTP = 0,
644+
/** RANDOM_PORT - Describes an endpoint that becomes a Kubernetes NodePort */
645+
RANDOM_PORT = 1,
646+
UNRECOGNIZED = -1,
647+
}
648+
`,
649+
{ sourceType: "module", plugins: ["typescript"] },
650+
);
651+
652+
for (const comment of ast.comments) {
653+
comment.loc = undefined;
654+
}
655+
656+
expect(generate(ast).code).toMatchInlineSnapshot(`
657+
"/** This describes how the endpoint is implemented when the lease is deployed */
658+
export enum Endpoint_Kind {
659+
/** SHARED_HTTP - Describes an endpoint that becomes a Kubernetes Ingress */
660+
SHARED_HTTP = 0,
661+
/** RANDOM_PORT - Describes an endpoint that becomes a Kubernetes NodePort */
662+
RANDOM_PORT = 1,
663+
UNRECOGNIZED = -1,
664+
}"
665+
`);
666+
});
667+
637668
it("comments without node.loc", () => {
638669
const ast = parse(
639670
`

0 commit comments

Comments
 (0)