Skip to content

Commit

Permalink
fix: Print newlines for leading Comments of TSEnumMember (#15216)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Nov 29, 2022
1 parent 65cc66f commit b0cf79a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/babel-generator/src/printer.ts
Expand Up @@ -7,6 +7,7 @@ import {
isStatement,
isClassBody,
isTSInterfaceBody,
isTSEnumDeclaration,
} from "@babel/types";
import type {
RecordAndTuplePluginOptions,
Expand Down Expand Up @@ -1103,7 +1104,8 @@ class Printer {
singleLine &&
!isStatement(node) &&
!isClassBody(parent) &&
!isTSInterfaceBody(parent);
!isTSInterfaceBody(parent) &&
!isTSEnumDeclaration(parent);

if (type === COMMENT_TYPE.LEADING) {
this._printComment(
Expand Down
31 changes: 31 additions & 0 deletions packages/babel-generator/test/index.js
Expand Up @@ -634,6 +634,37 @@ describe("generation", function () {
`);
});

it("comments without loc3", () => {
const ast = parse(
`
/** This describes how the endpoint is implemented when the lease is deployed */
export enum Endpoint_Kind {
/** SHARED_HTTP - Describes an endpoint that becomes a Kubernetes Ingress */
SHARED_HTTP = 0,
/** RANDOM_PORT - Describes an endpoint that becomes a Kubernetes NodePort */
RANDOM_PORT = 1,
UNRECOGNIZED = -1,
}
`,
{ sourceType: "module", plugins: ["typescript"] },
);

for (const comment of ast.comments) {
comment.loc = undefined;
}

expect(generate(ast).code).toMatchInlineSnapshot(`
"/** This describes how the endpoint is implemented when the lease is deployed */
export enum Endpoint_Kind {
/** SHARED_HTTP - Describes an endpoint that becomes a Kubernetes Ingress */
SHARED_HTTP = 0,
/** RANDOM_PORT - Describes an endpoint that becomes a Kubernetes NodePort */
RANDOM_PORT = 1,
UNRECOGNIZED = -1,
}"
`);
});

it("comments without node.loc", () => {
const ast = parse(
`
Expand Down

0 comments on commit b0cf79a

Please sign in to comment.