MCPcopy Create free account
hub / github.com/arktypeio/arktype / genericDefinitionOf

Function genericDefinitionOf

ark/aot/diagnostics.ts:91–113  ·  view source on GitHub ↗
(
	read: Node,
	module: SourceModule
)

Source from the content-addressed store, hash-verified

89
90// e.g. "t | null" for `T.or("null")`, if the call is that simple
91export 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
116export const genericSiteDefinitionOf = (

Callers 1

Calls 3

operatorOfFunction · 0.90
memberNameOfFunction · 0.90
snippetOfMethod · 0.80

Tested by

no test coverage detected