|
| 1 | +import { CountNode, DefinedTypeNode, isNode } from '@codama/nodes'; |
| 2 | + |
| 3 | +import { extendVisitor } from './extendVisitor'; |
| 4 | +import { mergeVisitor } from './generated/mergeVisitor'; |
| 5 | +import type { LinkableDictionary } from './LinkableDictionary'; |
| 6 | +import { getLastNodeFromPath } from './NodePath'; |
| 7 | +import { NodeStack } from './NodeStack'; |
| 8 | +import { pipe } from './pipe'; |
| 9 | +import { recordNodeStackVisitor } from './recordNodeStackVisitor'; |
| 10 | +import { visit, Visitor } from './visitor'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Whether the visited type has at least one finite value, following links. |
| 14 | + * |
| 15 | + * Only the shape of values matters: options may be `None`, enums may pick any |
| 16 | + * variant and collections without a fixed positive count may be empty, so all |
| 17 | + * of them stop a recursion. Transforms, prefixes and sizes only prevent a |
| 18 | + * value from existing when their constants link back to a type being visited, |
| 19 | + * since those constants could then never be created. Links that cannot be |
| 20 | + * resolved count as finite. |
| 21 | + * |
| 22 | + * A link back to a defined type being visited has no finite value on its own: |
| 23 | + * a finite value never needs to nest a type within itself, so another branch |
| 24 | + * must provide one. Enums without variants have no value at all. |
| 25 | + * |
| 26 | + * Links are resolved from the closest program of the current path, so visit |
| 27 | + * nodes with their full path, e.g. using `stack.visitPath(path, visitor)` with |
| 28 | + * the visitor's own stack. Links visited without a program are not resolved. |
| 29 | + * |
| 30 | + * @example |
| 31 | + * ```ts |
| 32 | + * // list = struct { next: option<link(list)> }, loop = struct { next: link(loop) } |
| 33 | + * stack.visitPath([root, program, list], getHasFiniteValueVisitor(linkables, { stack })); // true |
| 34 | + * stack.visitPath([root, program, loop], getHasFiniteValueVisitor(linkables, { stack })); // false |
| 35 | + * ``` |
| 36 | + */ |
| 37 | +export function getHasFiniteValueVisitor( |
| 38 | + linkables: LinkableDictionary, |
| 39 | + options: { stack?: NodeStack } = {}, |
| 40 | +): Visitor<boolean> { |
| 41 | + const stack = options.stack ?? new NodeStack(); |
| 42 | + const visitingDefinedTypes = new Set<DefinedTypeNode>(); |
| 43 | + // Only `true` is cached: a `false` may only hold because of the defined types being |
| 44 | + // visited, whereas a `true` holds whichever are, so it never needs computing again. |
| 45 | + const finiteDefinedTypes = new Set<DefinedTypeNode>(); |
| 46 | + |
| 47 | + return pipe( |
| 48 | + mergeVisitor( |
| 49 | + () => true, |
| 50 | + (_, values) => values.every(Boolean), |
| 51 | + ), |
| 52 | + v => |
| 53 | + extendVisitor(v, { |
| 54 | + visitArrayType(node, { self }) { |
| 55 | + return !hasFixedPositiveCount(node.count) || visit(node.item, self); |
| 56 | + }, |
| 57 | + |
| 58 | + visitDefinedType(node, { next }) { |
| 59 | + if (finiteDefinedTypes.has(node)) return true; |
| 60 | + visitingDefinedTypes.add(node); |
| 61 | + const hasFiniteValue = next(node); |
| 62 | + visitingDefinedTypes.delete(node); |
| 63 | + if (hasFiniteValue) finiteDefinedTypes.add(node); |
| 64 | + return hasFiniteValue; |
| 65 | + }, |
| 66 | + |
| 67 | + visitDefinedTypeLink(node, { self }) { |
| 68 | + const linkedPath = linkables.getPath(stack.getPath(node.kind)); |
| 69 | + if (!linkedPath) return true; |
| 70 | + if (visitingDefinedTypes.has(getLastNodeFromPath(linkedPath))) return false; |
| 71 | + return stack.visitPath(linkedPath, self); |
| 72 | + }, |
| 73 | + |
| 74 | + visitEnumType(node, { self }) { |
| 75 | + return (node.variants ?? []).some(variant => visit(variant, self)); |
| 76 | + }, |
| 77 | + |
| 78 | + // The defined type of an enum value only references it. |
| 79 | + visitEnumValue() { |
| 80 | + return true; |
| 81 | + }, |
| 82 | + |
| 83 | + visitMapType(node, { self }) { |
| 84 | + return !hasFixedPositiveCount(node.count) || (visit(node.key, self) && visit(node.value, self)); |
| 85 | + }, |
| 86 | + |
| 87 | + visitOptionType() { |
| 88 | + return true; |
| 89 | + }, |
| 90 | + |
| 91 | + visitRemainderOptionType() { |
| 92 | + return true; |
| 93 | + }, |
| 94 | + |
| 95 | + visitSetType(node, { self }) { |
| 96 | + return !hasFixedPositiveCount(node.count) || visit(node.item, self); |
| 97 | + }, |
| 98 | + |
| 99 | + visitZeroableOptionType() { |
| 100 | + return true; |
| 101 | + }, |
| 102 | + }), |
| 103 | + v => recordNodeStackVisitor(v, stack), |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +function hasFixedPositiveCount(count: CountNode): boolean { |
| 108 | + return isNode(count, 'fixedCountNode') && count.value > 0; |
| 109 | +} |
0 commit comments