|
| 1 | +import * as ts from "typescript"; |
| 2 | +import { JSONSchema4 } from "json-schema"; |
| 3 | + |
| 4 | +export interface Params { |
| 5 | + schemas: JSONSchema4; |
| 6 | +} |
| 7 | + |
| 8 | +const createStringType = ({ factory }: ts.TransformationContext): ts.TypeNode => { |
| 9 | + return factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword); |
| 10 | +}; |
| 11 | + |
| 12 | +const createAnyType = ({ factory }: ts.TransformationContext): ts.TypeNode => { |
| 13 | + return factory.createKeywordTypeNode(ts.SyntaxKind.AnyKeyword); |
| 14 | +}; |
| 15 | + |
| 16 | +const createNumberType = ({ factory }: ts.TransformationContext): ts.TypeNode => { |
| 17 | + return factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword); |
| 18 | +}; |
| 19 | + |
| 20 | +const createNullType = ({ factory }: ts.TransformationContext): ts.TypeNode => { |
| 21 | + return factory.createLiteralTypeNode(factory.createNull()); |
| 22 | +}; |
| 23 | + |
| 24 | +const createBooleanType = ({ factory }: ts.TransformationContext): ts.TypeNode => { |
| 25 | + return factory.createKeywordTypeNode(ts.SyntaxKind.BooleanKeyword); |
| 26 | +}; |
| 27 | + |
| 28 | +const createArrayType = ({ factory }: ts.TransformationContext, typeNode: ts.TypeNode): ts.TypeNode => { |
| 29 | + return factory.createArrayTypeNode(typeNode); |
| 30 | +}; |
| 31 | + |
| 32 | +const createObjectMemberType = ({ factory }: ts.TransformationContext, members: readonly ts.TypeElement[] | undefined) => { |
| 33 | + return factory.createTypeLiteralNode(members); |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * Don't use root schema root |
| 38 | + */ |
| 39 | +const createTypeNode = (context: ts.TransformationContext, schema: JSONSchema4): ts.TypeNode => { |
| 40 | + if (schema.type === "object") { |
| 41 | + if (!schema.properties) { |
| 42 | + return createObjectMemberType(context, []); |
| 43 | + } |
| 44 | + const members = Object.entries(schema.properties).map(([key, childSchema]) => { |
| 45 | + return context.factory.createPropertySignature( |
| 46 | + undefined, |
| 47 | + context.factory.createIdentifier(key), |
| 48 | + undefined, |
| 49 | + createTypeNode(context, childSchema), |
| 50 | + ); |
| 51 | + }); |
| 52 | + return createObjectMemberType(context, members); |
| 53 | + } else if (schema.type === "string") { |
| 54 | + return createStringType(context); |
| 55 | + } else if (schema.type === "array") { |
| 56 | + if (!schema.items) { |
| 57 | + return createArrayType(context, createAnyType(context)); |
| 58 | + } |
| 59 | + if (!Array.isArray(schema.items)) { |
| 60 | + return createArrayType(context, createTypeNode(context, schema.items)); |
| 61 | + } |
| 62 | + throw new Error("TODO"); |
| 63 | + } else if (schema.type === "any") { |
| 64 | + return createAnyType(context); |
| 65 | + } else if (schema.type === "boolean") { |
| 66 | + return createBooleanType(context); |
| 67 | + } else if (schema.type === "number") { |
| 68 | + return createNumberType(context); |
| 69 | + } else if (schema.type === "integer") { |
| 70 | + return createNumberType(context); |
| 71 | + } else if (schema.type === "null") { |
| 72 | + return createNullType(context); |
| 73 | + } |
| 74 | + return createAnyType(context); |
| 75 | +}; |
| 76 | + |
| 77 | +const createInterfaceFromRootObjectSchema = (context: ts.TransformationContext, schema: JSONSchema4): ts.PropertySignature[] => { |
| 78 | + if (schema.type !== "object" || !schema.properties) { |
| 79 | + return []; |
| 80 | + } |
| 81 | + return Object.entries(schema.properties).map(([key, childSchema]) => { |
| 82 | + return context.factory.createPropertySignature( |
| 83 | + undefined, |
| 84 | + context.factory.createIdentifier(key), |
| 85 | + undefined, |
| 86 | + createTypeNode(context, childSchema), |
| 87 | + ); |
| 88 | + }); |
| 89 | +}; |
| 90 | + |
| 91 | +/** |
| 92 | + * interface名をrenameする |
| 93 | + */ |
| 94 | +export const traverse = (params: Params) => <T extends ts.Node>(context: ts.TransformationContext) => (rootNode: T) => { |
| 95 | + const visit = (node: ts.Node): ts.Node => { |
| 96 | + node = ts.visitEachChild(node, visit, context); |
| 97 | + if (!ts.isInterfaceDeclaration(node)) { |
| 98 | + return node; |
| 99 | + } |
| 100 | + return context.factory.createInterfaceDeclaration( |
| 101 | + undefined, |
| 102 | + undefined, |
| 103 | + node.name, |
| 104 | + undefined, |
| 105 | + undefined, |
| 106 | + createInterfaceFromRootObjectSchema(context, params.schemas), |
| 107 | + ); |
| 108 | + }; |
| 109 | + return ts.visitNode(rootNode, visit); |
| 110 | +}; |
0 commit comments