( read: Node, module: SourceModule )
| 89 | |
| 90 | // e.g. "t | null" for `T.or("null")`, if the call is that simple |
| 91 | export const genericDefinitionOf = ( |
| 92 | read: Node, |
| 93 | module: SourceModule |
| 94 | ): string | undefined => { |
| 95 | if (read.type !== "MemberExpression") return |
| 96 | const [callee, call] = operatorOf(read) |
| 97 | if (call?.type !== "CallExpression" || call.callee !== callee) return |
| 98 | const name = memberNameOf(read) |
| 99 | const [arg, ...rest] = call.arguments |
| 100 | if (name === "array") return arg ? undefined : '"t[]"' |
| 101 | const operator = |
| 102 | name === "or" ? "|" |
| 103 | : name === "and" ? "&" |
| 104 | : undefined |
| 105 | if (!operator || !arg || rest.length || arg.type === "SpreadElement") return |
| 106 | if (arg.type === "Literal" && typeof arg.value === "string") { |
| 107 | const operand = |
| 108 | operator === "&" && arg.value.includes("|") ? `(${arg.value})` : arg.value |
| 109 | return JSON.stringify(`t ${operator} ${operand}`) |
| 110 | } |
| 111 | const text = module.snippetOf(arg) |
| 112 | return text.endsWith("…") ? undefined : `["t", "${operator}", ${text}]` |
| 113 | } |
| 114 | |
| 115 | // e.g. `{ value: "t" }` for `type({ value: T })`, if each read is a whole definition |
| 116 | export const genericSiteDefinitionOf = ( |
no test coverage detected