-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue with referencing cycle with an inline type (#2602)
Change to the circularReference hook to pass a new `ReferenceCyle` class. Referencing a cycle which has a declaration in it shouldn't be a problem even if we reference an inline type in it. So this data structure helps with detecting such scenarios.
- Loading branch information
1 parent
8b22fae
commit 553bfc4
Showing
7 changed files
with
152 additions
and
39 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
common/changes/@typespec/compiler/circular-ref-fixes_2023-10-23-19-10.json
This file contains 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,10 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@typespec/compiler", | ||
"comment": "", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@typespec/compiler" | ||
} |
This file contains 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 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
44 changes: 44 additions & 0 deletions
44
packages/compiler/src/emitter-framework/reference-cycle.ts
This file contains 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,44 @@ | ||
import { Type, getTypeName } from "../index.js"; | ||
import { EmitEntity } from "./types.js"; | ||
|
||
export interface ReferenceCycleEntry { | ||
type: Type; | ||
entity: EmitEntity<unknown>; | ||
} | ||
|
||
/** | ||
* Represent a reference cycle. | ||
* The cycle entries will always start with a declaration if there is one in the cycle. | ||
*/ | ||
export class ReferenceCycle implements Iterable<ReferenceCycleEntry> { | ||
/** | ||
* If this cycle contains a declaration. | ||
*/ | ||
readonly containsDeclaration: boolean; | ||
|
||
#entries: ReferenceCycleEntry[]; | ||
|
||
constructor(entries: ReferenceCycleEntry[]) { | ||
const firstDeclarationIndex = entries.findIndex((entry) => entry.entity.kind === "declaration"); | ||
this.containsDeclaration = firstDeclarationIndex !== -1; | ||
this.#entries = this.containsDeclaration | ||
? [...entries.slice(firstDeclarationIndex), ...entries.slice(0, firstDeclarationIndex)] | ||
: entries; | ||
} | ||
|
||
get first(): ReferenceCycleEntry { | ||
return this.#entries[0]; | ||
} | ||
|
||
[Symbol.iterator](): Iterator<ReferenceCycleEntry> { | ||
return this.#entries[Symbol.iterator](); | ||
} | ||
|
||
[Symbol.toStringTag](): string { | ||
return [...this.#entries, this.#entries[0]].map((x) => getTypeName(x.type)).join(" -> "); | ||
} | ||
|
||
toString(): string { | ||
return this[Symbol.toStringTag](); | ||
} | ||
} |
This file contains 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 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 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