Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/enum-description-line-break.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

Keep multi-line `x-enum-descriptions` on a single comment line. A description containing a line break previously ended the `//` comment early and pushed the remaining text onto its own line as code, producing an invalid `enum` (only relevant with the `enum` option).
7 changes: 6 additions & 1 deletion packages/openapi-typescript/src/lib/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,12 @@ export function tsEnumMember(value: string | number, metadata: { name?: string;
return member;
}

return ts.addSyntheticLeadingComment(member, ts.SyntaxKind.SingleLineCommentTrivia, ` ${trimmedDescription}`, true);
return ts.addSyntheticLeadingComment(
member,
ts.SyntaxKind.SingleLineCommentTrivia,
` ${trimmedDescription.replace(LB_RE, " ")}`,
true,
);
}

/** Create an intersection type */
Expand Down
17 changes: 17 additions & 0 deletions packages/openapi-typescript/test/lib/ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,23 @@ describe("tsEnum", () => {
Etc_GMTPlus0 = "Etc/GMT+0",
Etc_GMTPlus1 = "Etc/GMT+1",
Etc_GMT_1 = "Etc/GMT-1"
}`);
});

test("multi-line x-enum-descriptions stay on a single comment line", () => {
expect(
astToString(
tsEnum(
".Error.code.",
[100, 101],
[{ description: "Code 100\nspanning two lines" }, { description: "Code 101" }],
),
).trim(),
).toBe(`enum ErrorCode {
// Code 100 spanning two lines
Value100 = 100,
// Code 101
Value101 = 101
}`);
});
});
Expand Down
Loading