|
| 1 | +const _ = require("lodash"); |
| 2 | + |
| 3 | +const Keyword = { |
| 4 | + Number: "number", |
| 5 | + String: "string", |
| 6 | + Boolean: "boolean", |
| 7 | + Any: "any", |
| 8 | + Void: "void", |
| 9 | + Unknown: "unknown", |
| 10 | + Null: "null", |
| 11 | + Undefined: "undefined", |
| 12 | + Object: "object", |
| 13 | + File: "File", |
| 14 | + Date: "Date", |
| 15 | + Type: "type", |
| 16 | + Enum: "enum", |
| 17 | + Interface: "interface", |
| 18 | + Array: "Array", |
| 19 | + Record: "Record", |
| 20 | + Intersection: "&", |
| 21 | + Union: "|", |
| 22 | +}; |
| 23 | + |
| 24 | +const CodeGenKeyword = { |
| 25 | + UtilRequiredKeys: "UtilRequiredKeys", |
| 26 | +}; |
| 27 | + |
| 28 | +const Ts = { |
| 29 | + Keyword, |
| 30 | + CodeGenKeyword, |
| 31 | + /** |
| 32 | + * $A[] or Array<$A> |
| 33 | + */ |
| 34 | + ArrayType: (content) => { |
| 35 | + const { config } = require("./config"); |
| 36 | + |
| 37 | + if (config.anotherArrayType) { |
| 38 | + return Ts.TypeWithGeneric(Ts.Keyword.Array, [content]); |
| 39 | + } |
| 40 | + |
| 41 | + return `${Ts.ExpressionGroup(content)}[]`; |
| 42 | + }, |
| 43 | + /** |
| 44 | + * "$A" |
| 45 | + */ |
| 46 | + StringValue: (content) => `"${content}"`, |
| 47 | + /** |
| 48 | + * $A |
| 49 | + */ |
| 50 | + BooleanValue: (content) => `${content}`, |
| 51 | + /** |
| 52 | + * $A |
| 53 | + */ |
| 54 | + NumberValue: (content) => `${content}`, |
| 55 | + /** |
| 56 | + * $A |
| 57 | + */ |
| 58 | + NullValue: (content) => content, |
| 59 | + /** |
| 60 | + * $A1 | $A2 |
| 61 | + */ |
| 62 | + UnionType: (contents) => _.join(_.uniq(contents), ` ${Ts.Keyword.Union} `), |
| 63 | + /** |
| 64 | + * ($A1) |
| 65 | + */ |
| 66 | + ExpressionGroup: (content) => (content ? `(${content})` : ""), |
| 67 | + /** |
| 68 | + * $A1 & $A2 |
| 69 | + */ |
| 70 | + IntersectionType: (contents) => _.join(_.uniq(contents), ` ${Ts.Keyword.Intersection} `), |
| 71 | + /** |
| 72 | + * Record<$A1, $A2> |
| 73 | + */ |
| 74 | + RecordType: (key, value) => Ts.TypeWithGeneric(Ts.Keyword.Record, [key, value]), |
| 75 | + /** |
| 76 | + * readonly $key?:$value |
| 77 | + */ |
| 78 | + TypeField: ({ readonly, key, optional, value }) => |
| 79 | + _.compact([readonly && "readonly ", key, optional && "?", ": ", value]).join(""), |
| 80 | + /** |
| 81 | + * [key: $A1]: $A2 |
| 82 | + */ |
| 83 | + InterfaceDynamicField: (key, value) => `[key: ${key}]: ${value}`, |
| 84 | + /** |
| 85 | + * $A1 = $A2 |
| 86 | + */ |
| 87 | + EnumField: (key, value) => `${key} = ${value}`, |
| 88 | + /** |
| 89 | + * $A0.key = $A0.value, |
| 90 | + * $A1.key = $A1.value, |
| 91 | + * $AN.key = $AN.value, |
| 92 | + */ |
| 93 | + EnumFieldsWrapper: (contents) => _.map(contents, ({ key, value }) => ` ${Ts.EnumField(key, value)}`).join(",\n"), |
| 94 | + /** |
| 95 | + * {\n $A \n} |
| 96 | + */ |
| 97 | + ObjectWrapper: (content) => `{\n${content}\n}`, |
| 98 | + /** |
| 99 | + * /** $A *\/ |
| 100 | + */ |
| 101 | + MultilineComment: (contents, formatFn) => |
| 102 | + [ |
| 103 | + ...(contents.length === 1 |
| 104 | + ? [`/** ${contents[0]} */`] |
| 105 | + : ["/**", ...contents.map((content) => ` * ${content}`), " */"]), |
| 106 | + ].map((part) => `${formatFn ? formatFn(part) : part}\n`), |
| 107 | + /** |
| 108 | + * $A1<...$A2.join(,)> |
| 109 | + */ |
| 110 | + TypeWithGeneric: (typeName, genericArgs) => { |
| 111 | + return `${typeName}${genericArgs.length ? `<${genericArgs.join(",")}>` : ""}`; |
| 112 | + }, |
| 113 | +}; |
| 114 | + |
| 115 | +const JsDoc = { |
| 116 | + Deprecated: "@deprecated", |
| 117 | + Format: "@format", |
| 118 | + Min: "@min", |
| 119 | + Max: "@max", |
| 120 | + Pattern: "@pattern", |
| 121 | + Example: "@example", |
| 122 | + Description: "", |
| 123 | + Title: "", |
| 124 | + TextLine: (key, text) => _.compact([key, `${text === undefined ? "" : text}`]).join(" "), |
| 125 | + ObjectFieldDescription: ({ title, description, deprecated, format, minimum, maximum, pattern, example }) => { |
| 126 | + return _.compact([ |
| 127 | + JsDoc.TextLine(JsDoc.Title, title), |
| 128 | + JsDoc.TextLine(JsDoc.Description, description), |
| 129 | + !_.isUndefined(deprecated) && JsDoc.Deprecated, |
| 130 | + !_.isUndefined(format) && JsDoc.TextLine(JsDoc.Format, format), |
| 131 | + !_.isUndefined(minimum) && JsDoc.TextLine(JsDoc.Min, minimum), |
| 132 | + !_.isUndefined(maximum) && JsDoc.TextLine(JsDoc.Max, maximum), |
| 133 | + !_.isUndefined(pattern) && JsDoc.TextLine(JsDoc.Pattern, pattern), |
| 134 | + !_.isUndefined(example) && JsDoc.TextLine(JsDoc.Example, _.isObject(example) ? JSON.stringify(example) : example), |
| 135 | + ]).join("\n"); |
| 136 | + }, |
| 137 | +}; |
| 138 | + |
| 139 | +module.exports = { |
| 140 | + Ts, |
| 141 | + JsDoc, |
| 142 | +}; |
0 commit comments