aboutsummaryrefslogtreecommitdiff
path: root/src/context.ts
diff options
context:
space:
mode:
authorAkshay Nair <phenax5@gmail.com>2023-01-14 11:26:56 +0530
committerAkshay Nair <phenax5@gmail.com>2023-01-14 11:26:56 +0530
commit97817711d3eebaacc354a77aa83bc9544cc87d42 (patch)
tree887da757f2890357878c8ec2d21753efaf17a6f2 /src/context.ts
parentefe0ab336144d2d965bf355db02762f5cf539c7f (diff)
downloadts-types-lang-97817711d3eebaacc354a77aa83bc9544cc87d42.tar.gz
ts-types-lang-97817711d3eebaacc354a77aa83bc9544cc87d42.zip
feat: adds bindto with scopes
Diffstat (limited to '')
-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