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

Function evalStringTypeInScope

ark/aot/droppedNarrow.ts:1276–1349  ·  view source on GitHub ↗
(
	rawExpr: string,
	scope: BaseScope,
	seen = new Set<unknown>(),
	shadowed?: ReadonlySet<string>
)

Source from the content-addressed store, hash-verified

1274}
1275
1276const evalStringTypeInScope = (
1277 rawExpr: string,
1278 scope: BaseScope,
1279 seen = new Set<unknown>(),
1280 shadowed?: ReadonlySet<string>
1281): BaseRoot | undefined => {
1282 try {
1283 const expr = stripOuterParens(rawExpr)
1284 if (!expr) return undefined
1285 const unionParts = splitTopLevelOp(expr, "|")
1286 if (unionParts) {
1287 const roots: BaseRoot[] = []
1288 for (const part of unionParts) {
1289 const r = evalStringTypeInScope(part, scope, new Set(seen), shadowed)
1290 if (!r) return undefined
1291 roots.push(r)
1292 }
1293 return roots.reduce((a, b) => a.or(b))
1294 }
1295 const interParts = splitTopLevelOp(expr, "&")
1296 if (interParts) {
1297 const roots: BaseRoot[] = []
1298 for (const part of interParts) {
1299 const r = evalStringTypeInScope(part, scope, new Set(seen), shadowed)
1300 if (!r) return undefined
1301 roots.push(r)
1302 }
1303 return roots.reduce((a, b) => a.and(b))
1304 }
1305 if (expr.endsWith("[]")) {
1306 const inner = evalStringTypeInScope(
1307 expr.slice(0, -2),
1308 scope,
1309 seen,
1310 shadowed
1311 )
1312 return inner?.array()
1313 }
1314 const lit = unescapeStringLiteral(expr)
1315 if (lit !== undefined) return type.unit(lit).internal
1316 // single generic instantiation spanning the whole expression
1317 const genMatch = /^([#$A-Z_a-z][\w#$.]*)\s*</.exec(expr)
1318 if (genMatch) {
1319 const ltIndex = genMatch[0].length - 1
1320 const parsed = parseAngleArgs(expr, ltIndex)
1321 if (parsed && parsed.end === expr.length) {
1322 const target = lookupInScope(genMatch[1], scope, shadowed)
1323 if (hasArkKind(target, "generic")) {
1324 const argRoots: BaseRoot[] = []
1325 for (const arg of parsed.args) {
1326 const r = evalStringTypeInScope(arg, scope, new Set(seen), shadowed)
1327 if (!r) return undefined
1328 argRoots.push(r)
1329 }
1330 const structural = structuralGenericOf(target)
1331 if (structural) return applyStructuralOp(structural[0], argRoots)
1332 return target(...argRoots)
1333 }

Callers 2

evalDefInScopeFunction · 0.85
checkStringGenericDropsFunction · 0.85

Calls 15

hasArkKindFunction · 0.90
stripOuterParensFunction · 0.85
splitTopLevelOpFunction · 0.85
unescapeStringLiteralFunction · 0.85
parseAngleArgsFunction · 0.85
lookupInScopeFunction · 0.85
structuralGenericOfFunction · 0.85
applyStructuralOpFunction · 0.85
targetFunction · 0.85
evalDefInScopeFunction · 0.85
reduceMethod · 0.80
orMethod · 0.80

Tested by

no test coverage detected