aboutsummaryrefslogtreecommitdiff
path: root/src/context.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/context.ts')
-rw-r--r--src/context.ts23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/context.ts b/src/context.ts
index 06fe041..adc0be5 100644
--- a/src/context.ts
+++ b/src/context.ts
@@ -76,6 +76,8 @@ export const createContext = (options: CtxOptions): Ctx => {
let currentEnv = 'node'
const setEnv = (e: string) => (currentEnv = e)
+ const environment: Array<Map<string, string>> = []
+
const ctx: Ctx = {
sourceFile,
typeChecker,
@@ -112,6 +114,27 @@ export const createContext = (options: CtxOptions): Ctx => {
hasCustomEffect: (name) => customEffects[name] !== undefined,
evaluateType,
+
+ newScope() {
+ environment.unshift(new Map)
+ },
+ addToScope(name, resKey) {
+ if (environment.length === 0) ctx.newScope()
+ const curScope = environment[0]
+ curScope?.set(name, resKey)
+ },
+ clearScope() {
+ environment.shift()
+ },
+ getKeyInScope(name) {
+ return environment.find(scope => scope.has(name))?.get(name)
+ },
+ withScope: (fn) => async () => {
+ ctx.newScope()
+ const res = await fn()
+ ctx.clearScope()
+ return res
+ },
}
return ctx