-
Notifications
You must be signed in to change notification settings - Fork 73
fix(typescript): Handle invalid type names #317
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
base: main
Are you sure you want to change the base?
Conversation
7ce0868 to
5e6a919
Compare
|
+1 |
fabien0102
left a comment
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.
Interesting edge case, let’s try to make it even more fancy for the number only (the function is already there)
| expect(printSchema(schema, "200")).toMatchInlineSnapshot(` | ||
| "export type _200 = void;" | ||
| `); | ||
| }); |
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.
For the case where it’s only numbers, we could use this
| export const convertNumberToWord = (n: number): string => { |
57ebc4f to
9d8b639
Compare
9d8b639 to
14e8de3
Compare
|
|
||
| // If the identifier is now empty, set it to "_". | ||
| if (identifier.length === 0) { | ||
| identifier = "_"; |
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.
We could name it here "NoValidName" or we use the invalid name here.
I just kept the "_" as placeholder.
|
|
||
| let identifier = pascal(name); | ||
|
|
||
| if (identifier.length > 0 && !isNaN(Number(identifier))) { |
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.
This check will also convert "0123" to "OneTwoThree", as it can convert it to a number.
What do you prefer? Either we ignore this, add a handling for "Zero" at the start or we let the check fail and handle this case in the "else"-block
I recently saw that there is an issue with certain names in the schema.
The schema above will generate the definition
export type 400 = voidwhere 400 is an invalid identifier.The problem happens in schemaToTypeAliasDeclaration as the name is used without any check for its validity.
I addressed this issue by prepending invalid names by
_and if they are still invalid, by removing any non A-Za-z0-9 characters.It might cause collisions though if you have e.g.
400and_400, or you use a schema full of e.g. emojis as names.