-
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.
- Loading branch information
1 parent
c9c6f0e
commit ce6bc00
Showing
4 changed files
with
68 additions
and
46 deletions.
There are no files selected for viewing
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