microsoft/typespec

Public

mirrored from https://github.com/microsoft/typespecAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/fix-diff-highlight-issue

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

packages/asset-emitter/src/reference-cycle.ts

44lines · modecode

1import { getTypeName, type Type } from "@typespec/compiler";
2import type { EmitEntity } from "./types.js";
3
4export interface ReferenceCycleEntry {
5 type: Type;
6 entity: EmitEntity<unknown>;
7}
8
9/**
10 * Represent a reference cycle.
11 * The cycle entries will always start with a declaration if there is one in the cycle.
12 */
13export class ReferenceCycle implements Iterable<ReferenceCycleEntry> {
14 /**
15 * If this cycle contains a declaration.
16 */
17 readonly containsDeclaration: boolean;
18
19 #entries: ReferenceCycleEntry[];
20
21 constructor(entries: ReferenceCycleEntry[]) {
22 const firstDeclarationIndex = entries.findIndex((entry) => entry.entity.kind === "declaration");
23 this.containsDeclaration = firstDeclarationIndex !== -1;
24 this.#entries = this.containsDeclaration
25 ? [...entries.slice(firstDeclarationIndex), ...entries.slice(0, firstDeclarationIndex)]
26 : entries;
27 }
28
29 get first(): ReferenceCycleEntry {
30 return this.#entries[0];
31 }
32
33 [Symbol.iterator](): Iterator<ReferenceCycleEntry> {
34 return this.#entries[Symbol.iterator]();
35 }
36
37 [Symbol.toStringTag](): string {
38 return [...this.#entries, this.#entries[0]].map((x) => getTypeName(x.type)).join(" -> ");
39 }
40
41 toString(): string {
42 return this[Symbol.toStringTag]();
43 }
44}