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

Method fixedDepthOf

ark/aot/sameEachRun.ts:299–378  ·  view source on GitHub ↗
(
		expression: Node,
		module: SourceModule,
		seen: Set<Binding>
	)

Source from the content-addressed store, hash-verified

297 // what an expression's text alone decides, as how many levels down it holds
298 // what could be written: 0 for a primitive
299 private fixedDepthOf(
300 expression: Node,
301 module: SourceModule,
302 seen: Set<Binding>
303 ): number | undefined {
304 const node = unwrapExpression(expression)
305 const deepest = (nodes: readonly (Node | null)[]) => {
306 let depth = 0
307 for (const operand of nodes) {
308 if (!operand) continue
309 const held = this.fixedDepthOf(
310 operand.type === "SpreadElement" ? operand.argument : operand,
311 module,
312 seen
313 )
314 if (held === undefined) return
315 depth = Math.max(depth, held)
316 }
317 return depth
318 }
319 const holding = (nodes: readonly (Node | null)[]) => {
320 const depth = deepest(nodes)
321 return depth === undefined ? undefined : depth + 1
322 }
323 const primitive = (nodes: readonly (Node | null)[]) =>
324 deepest(nodes) === undefined ? undefined : 0
325 switch (node.type) {
326 case "Literal":
327 // a regex with a `g` or `y` flag holds where it last matched
328 return (
329 !("regex" in node) || !node.regex ? 0
330 : /[gy]/.test(node.regex.flags) ? undefined
331 : 1
332 )
333 case "TemplateLiteral":
334 return primitive(node.expressions)
335 case "UnaryExpression":
336 return node.operator === "delete" ?
337 undefined
338 : primitive([node.argument])
339 case "BinaryExpression":
340 return primitive([node.left, node.right])
341 case "LogicalExpression":
342 return deepest([node.left, node.right])
343 case "ConditionalExpression":
344 return deepest([node.test, node.consequent, node.alternate])
345 case "ArrayExpression":
346 return holding(node.elements)
347 case "ObjectExpression": {
348 const values: Node[] = []
349 for (const property of node.properties) {
350 if (property.type === "SpreadElement") values.push(property)
351 else if (
352 property.kind !== "init" ||
353 property.computed ||
354 property.method
355 )
356 return

Callers 2

fixedDepthOfBindingMethod · 0.95
deepestMethod · 0.95

Calls 10

locateMethod · 0.95
fixedDepthOfBindingMethod · 0.95
unwrapExpressionFunction · 0.90
primitiveFunction · 0.85
deepestFunction · 0.85
holdingFunction · 0.85
testMethod · 0.80
getMethod · 0.65
pushMethod · 0.45
hasMethod · 0.45

Tested by

no test coverage detected