-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Emitter framework handle circular references (#2550)
Emitter framework deals with circular reference for you and this works great if the circular flow involve declarations that can be referenced. But sometimes there is issues where we can't have a declaration and so a circular reference becomes impossible. Adding a new `circularReference` hook that gets called when we resolve a reference that is point back to itself in the resolution stack. By default it will throw resolve the declaration ref if the target is a declaration or throw an error. An emitter can decide to override this function to provide a different handling in either cases. A target language might not even support circular reference at all. --------- Co-authored-by: Mark Cowlishaw <markcowl@microsoft.com>
- Loading branch information
1 parent
2353586
commit ad2aaca
Showing
6 changed files
with
277 additions
and
53 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
common/changes/@typespec/compiler/emitter-framework-circular_2023-10-10-00-25.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": "Add new hook for handling circular references", | ||
"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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Declaration, Scope } from "./types.js"; | ||
|
||
export function scopeChain<T>(scope: Scope<T> | null) { | ||
const chain = []; | ||
while (scope) { | ||
chain.unshift(scope); | ||
scope = scope.parentScope; | ||
} | ||
|
||
return chain; | ||
} | ||
|
||
/** | ||
* Resolve relative scopes between the current scope and the target declaration. | ||
* @param target The target declaration | ||
* @param currentScope Current scope | ||
* @returns | ||
*/ | ||
export function resolveDeclarationReferenceScope<T>( | ||
target: Declaration<T>, | ||
currentScope: Scope<T> | ||
) { | ||
const targetScope = target.scope; | ||
const targetChain = scopeChain(targetScope); | ||
const currentChain = scopeChain(currentScope); | ||
let diffStart = 0; | ||
while ( | ||
targetChain[diffStart] && | ||
currentChain[diffStart] && | ||
targetChain[diffStart] === currentChain[diffStart] | ||
) { | ||
diffStart++; | ||
} | ||
|
||
const pathUp: Scope<T>[] = currentChain.slice(diffStart); | ||
const pathDown: Scope<T>[] = targetChain.slice(diffStart); | ||
|
||
const commonScope = targetChain[diffStart - 1] ?? null; | ||
return { pathUp, pathDown, commonScope }; | ||
} |
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
Oops, something went wrong.