Repository navigation
[2.x] Detect cyclic defined types #1211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { DefinedTypeNode } from '@codama/nodes'; | ||
|
|
||
| import { extendVisitor } from './extendVisitor'; | ||
| import { mergeVisitor } from './generated/mergeVisitor'; | ||
| import type { LinkableDictionary } from './LinkableDictionary'; | ||
| import { NodePath } from './NodePath'; | ||
| import { NodeStack } from './NodeStack'; | ||
| import { pipe } from './pipe'; | ||
| import { recordNodeStackVisitor } from './recordNodeStackVisitor'; | ||
| import { visit, Visitor } from './visitor'; | ||
|
|
||
| /** | ||
| * List the paths of the defined types the visited node links to, without | ||
| * following these links. Links that cannot be resolved are skipped. | ||
| * | ||
| * Only links shaping a type are listed: the defined type of an | ||
| * `enumValueNode` merely references it, so it is left out. | ||
| * | ||
| * Links are resolved from the closest program of the current path, so visit | ||
| * nodes with their full path, e.g. using `stack.visitPath(path, visitor)` with | ||
| * the visitor's own stack. Links visited without a program are not resolved. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // node = struct { value: link(amount), next: option<link(node)> } | ||
| * stack.visitPath([root, program, node], getDefinedTypeLinksVisitor(linkables, { stack })); | ||
| * // [[root, program, amount], [root, program, node]] | ||
| * ``` | ||
| */ | ||
| export function getDefinedTypeLinksVisitor( | ||
| linkables: LinkableDictionary, | ||
| options: { stack?: NodeStack } = {}, | ||
| ): Visitor<NodePath<DefinedTypeNode>[]> { | ||
| const stack = options.stack ?? new NodeStack(); | ||
|
|
||
| return pipe( | ||
| mergeVisitor( | ||
| () => [] as NodePath<DefinedTypeNode>[], | ||
| (_, values) => values.flat(), | ||
| ), | ||
| v => | ||
| extendVisitor(v, { | ||
| visitDefinedTypeLink(node, { next }) { | ||
| const linkedPath = linkables.getPath(stack.getPath(node.kind)); | ||
| return [...(linkedPath ? [linkedPath] : []), ...next(node)]; | ||
| }, | ||
| visitEnumValue(node, { self }) { | ||
| return node.value ? visit(node.value, self) : []; | ||
| }, | ||
| }), | ||
| v => recordNodeStackVisitor(v, stack), | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import { CountNode, DefinedTypeNode, isNode } from '@codama/nodes'; | ||
|
|
||
| import { extendVisitor } from './extendVisitor'; | ||
| import { mergeVisitor } from './generated/mergeVisitor'; | ||
| import type { LinkableDictionary } from './LinkableDictionary'; | ||
| import { getLastNodeFromPath } from './NodePath'; | ||
| import { NodeStack } from './NodeStack'; | ||
| import { pipe } from './pipe'; | ||
| import { recordNodeStackVisitor } from './recordNodeStackVisitor'; | ||
| import { visit, Visitor } from './visitor'; | ||
|
|
||
| /** | ||
| * Whether the visited type has at least one finite value, following links. | ||
| * | ||
| * Only the shape of values matters: options may be `None`, enums may pick any | ||
| * variant and collections without a fixed positive count may be empty, so all | ||
| * of them stop a recursion. Transforms, prefixes and sizes only prevent a | ||
| * value from existing when their constants link back to a type being visited, | ||
| * since those constants could then never be created. Links that cannot be | ||
| * resolved count as finite. | ||
| * | ||
| * A link back to a defined type being visited has no finite value on its own: | ||
| * a finite value never needs to nest a type within itself, so another branch | ||
| * must provide one. Enums without variants have no value at all. | ||
| * | ||
| * Links are resolved from the closest program of the current path, so visit | ||
| * nodes with their full path, e.g. using `stack.visitPath(path, visitor)` with | ||
| * the visitor's own stack. Links visited without a program are not resolved. | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * // list = struct { next: option<link(list)> }, loop = struct { next: link(loop) } | ||
| * stack.visitPath([root, program, list], getHasFiniteValueVisitor(linkables, { stack })); // true | ||
| * stack.visitPath([root, program, loop], getHasFiniteValueVisitor(linkables, { stack })); // false | ||
| * ``` | ||
| */ | ||
| export function getHasFiniteValueVisitor( | ||
| linkables: LinkableDictionary, | ||
| options: { stack?: NodeStack } = {}, | ||
| ): Visitor<boolean> { | ||
| const stack = options.stack ?? new NodeStack(); | ||
| const visitingDefinedTypes = new Set<DefinedTypeNode>(); | ||
| // Only `true` is cached: a `false` may only hold because of the defined types being | ||
| // visited, whereas a `true` holds whichever are, so it never needs computing again. | ||
| const finiteDefinedTypes = new Set<DefinedTypeNode>(); | ||
|
|
||
| return pipe( | ||
| mergeVisitor( | ||
| () => true, | ||
| (_, values) => values.every(Boolean), | ||
| ), | ||
| v => | ||
| extendVisitor(v, { | ||
| visitArrayType(node, { self }) { | ||
| return !hasFixedPositiveCount(node.count) || visit(node.item, self); | ||
| }, | ||
|
|
||
| visitDefinedType(node, { next }) { | ||
| if (finiteDefinedTypes.has(node)) return true; | ||
| visitingDefinedTypes.add(node); | ||
| const hasFiniteValue = next(node); | ||
| visitingDefinedTypes.delete(node); | ||
| if (hasFiniteValue) finiteDefinedTypes.add(node); | ||
| return hasFiniteValue; | ||
| }, | ||
|
|
||
| visitDefinedTypeLink(node, { self }) { | ||
| const linkedPath = linkables.getPath(stack.getPath(node.kind)); | ||
| if (!linkedPath) return true; | ||
| if (visitingDefinedTypes.has(getLastNodeFromPath(linkedPath))) return false; | ||
| return stack.visitPath(linkedPath, self); | ||
| }, | ||
|
|
||
| visitEnumType(node, { self }) { | ||
| return (node.variants ?? []).some(variant => visit(variant, self)); | ||
| }, | ||
|
lorisleiva marked this conversation as resolved.
|
||
|
|
||
| // The defined type of an enum value only references it. | ||
| visitEnumValue() { | ||
| return true; | ||
| }, | ||
|
|
||
| visitMapType(node, { self }) { | ||
| return !hasFixedPositiveCount(node.count) || (visit(node.key, self) && visit(node.value, self)); | ||
| }, | ||
|
|
||
| visitOptionType() { | ||
| return true; | ||
| }, | ||
|
|
||
| visitRemainderOptionType() { | ||
| return true; | ||
| }, | ||
|
|
||
| visitSetType(node, { self }) { | ||
| return !hasFixedPositiveCount(node.count) || visit(node.item, self); | ||
| }, | ||
|
|
||
| visitZeroableOptionType() { | ||
| return true; | ||
| }, | ||
| }), | ||
| v => recordNodeStackVisitor(v, stack), | ||
| ); | ||
| } | ||
|
|
||
| function hasFixedPositiveCount(count: CountNode): boolean { | ||
| return isNode(count, 'fixedCountNode') && count.value > 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.