-
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
Changes from all commits
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 |
|---|---|---|
|
|
@@ -11,7 +11,12 @@ import { | |
| } from "openapi3-ts/oas30"; | ||
| import { singular } from "pluralize"; | ||
| import { isValidIdentifier } from "tsutils"; | ||
| import ts, { factory as f } from "typescript"; | ||
| import ts, { | ||
| factory as f, | ||
| isIdentifierPart, | ||
| isIdentifierStart, | ||
| } from "typescript"; | ||
| import { convertNumberToWord } from "../utils/getEnumProperties"; | ||
| import { getReferenceSchema } from "./getReference"; | ||
|
|
||
| type RemoveIndex<T> = { | ||
|
|
@@ -56,11 +61,37 @@ export const schemaToTypeAliasDeclaration = ( | |
| const jsDocNode = isSchemaObject(schema) | ||
| ? getJSDocComment(schema, context) | ||
| : undefined; | ||
|
|
||
| let identifier = pascal(name); | ||
|
|
||
| if (identifier.length > 0 && !isNaN(Number(identifier))) { | ||
|
Contributor
Author
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. 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
Owner
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. Should be fine without the "Zero" handling. |
||
| // If the identifier can be cast to a number, convert it to a word. | ||
| identifier = pascal(convertNumberToWord(Number(identifier))); | ||
| } else { | ||
| // If the identifier does not start with a valid character but valid identifier part, prefix it with an underscore. | ||
| if ( | ||
| !isIdentifierStart(identifier.charCodeAt(0), ts.ScriptTarget.Latest) && | ||
| isIdentifierPart(identifier.charCodeAt(0), ts.ScriptTarget.Latest) | ||
| ) { | ||
| identifier = `_${identifier}`; | ||
| } | ||
|
|
||
| // If the identifier is still not valid, remove invalid characters. | ||
| if (!isValidIdentifier(identifier)) { | ||
| identifier = identifier.replace(/[^a-zA-Z0-9_]/g, ""); | ||
| } | ||
|
|
||
| // If the identifier is now empty, set it to "_". | ||
| if (identifier.length === 0) { | ||
| identifier = "_"; | ||
|
Contributor
Author
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. We could name it here "NoValidName" or we use the invalid name here. I just kept the "_" as placeholder. |
||
| } | ||
| } | ||
|
|
||
| const declarationNode = f.createTypeAliasDeclaration( | ||
| [f.createModifier(ts.SyntaxKind.ExportKeyword)], | ||
| pascal(name), | ||
| identifier, | ||
| undefined, | ||
| getType(schema, context, name) | ||
| getType(schema, context, identifier) | ||
| ); | ||
|
|
||
| return jsDocNode ? [jsDocNode, declarationNode] : [declarationNode]; | ||
|
|
@@ -89,10 +120,10 @@ export const getType = ( | |
|
|
||
| let refNode: ts.TypeNode = f.createTypeReferenceNode( | ||
| namespace === context.currentComponent | ||
| ? f.createIdentifier(pascal(name)) | ||
| ? f.createIdentifier(name) | ||
| : f.createQualifiedName( | ||
| f.createIdentifier(pascal(namespace)), | ||
| f.createIdentifier(pascal(name)) | ||
| f.createIdentifier(name) | ||
| ) | ||
| ); | ||
|
|
||
|
|
@@ -174,7 +205,7 @@ export const getType = ( | |
| if (schema.enum) { | ||
| if (isNodeEnum) { | ||
| return f.createUnionTypeNode([ | ||
| f.createTypeReferenceNode(f.createIdentifier(pascal(name || ""))), | ||
| f.createTypeReferenceNode(f.createIdentifier(name || "")), | ||
| ...(schema.nullable ? [f.createLiteralTypeNode(f.createNull())] : []), | ||
| ]); | ||
| } | ||
|
|
||
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
openapi-codegen/plugins/typescript/src/utils/getEnumProperties.ts
Line 95 in c555865