-
-
Notifications
You must be signed in to change notification settings - Fork 528
Add support for x-enum-varnames and x-enum-descriptions #1374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"openapi-typescript": major | ||
--- | ||
|
||
Add support for x-enum-varnames and x-enum-descriptions |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -229,6 +229,8 @@ export function tsDedupe(types: ts.TypeNode[]): ts.TypeNode[] { | |||||
export function tsEnum( | ||||||
name: string, | ||||||
members: (string | number)[], | ||||||
membersNames?: string[], | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think this is an easier-to-use signature; I find the former where multiple ordered params have the same type to be easy to make mistakes in rearranging them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, done 👍 |
||||||
membersDescriptions?: string[], | ||||||
options?: { readonly?: boolean; export?: boolean }, | ||||||
) { | ||||||
let enumName = name.replace(JS_ENUM_INVALID_CHARS_RE, (c) => { | ||||||
|
@@ -249,28 +251,48 @@ export function tsEnum( | |||||
}) | ||||||
: undefined, | ||||||
/* name */ enumName, | ||||||
/* members */ members.map(tsEnumMember), | ||||||
/* members */ members.map((value, i) => | ||||||
tsEnumMember(value, membersNames?.[i], membersDescriptions?.[i]), | ||||||
), | ||||||
); | ||||||
} | ||||||
|
||||||
/** Sanitize TS enum member expression */ | ||||||
export function tsEnumMember(value: string | number) { | ||||||
if (typeof value === "number") { | ||||||
return ts.factory.createEnumMember( | ||||||
`Value${String(value)}`.replace(".", "_"), // don’t forget decimals | ||||||
ts.factory.createNumericLiteral(value), | ||||||
); | ||||||
} | ||||||
let name = value; | ||||||
export function tsEnumMember( | ||||||
value: string | number, | ||||||
memberName?: string, | ||||||
description?: string, | ||||||
) { | ||||||
let name = memberName ?? String(value); | ||||||
if (!JS_PROPERTY_INDEX_RE.test(name)) { | ||||||
if (Number(name[0]) >= 0) { | ||||||
name = `Value${name}`; | ||||||
name = `Value${name}`.replace(".", "_"); // don't forged decimals; | ||||||
} | ||||||
name = name.replace(JS_PROPERTY_INDEX_INVALID_CHARS_RE, "_"); | ||||||
} | ||||||
return ts.factory.createEnumMember( | ||||||
name, | ||||||
ts.factory.createStringLiteral(value), | ||||||
|
||||||
let member; | ||||||
if (typeof value === "number") { | ||||||
member = ts.factory.createEnumMember( | ||||||
name, | ||||||
ts.factory.createNumericLiteral(value), | ||||||
); | ||||||
} else { | ||||||
member = ts.factory.createEnumMember( | ||||||
name, | ||||||
ts.factory.createStringLiteral(value), | ||||||
); | ||||||
} | ||||||
|
||||||
if (description == undefined) { | ||||||
return member; | ||||||
} | ||||||
|
||||||
return ts.addSyntheticLeadingComment( | ||||||
member, | ||||||
ts.SyntaxKind.SingleLineCommentTrivia, | ||||||
" ".concat(description.trim()), | ||||||
true, | ||||||
); | ||||||
} | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done