Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/stupid-flowers-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@getlang/parser": minor
"@getlang/utils": minor
"@getlang/get": minor
"@getlang/lib": minor
---

module call context
8 changes: 7 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@
"style": {
"noUselessElse": "off",
"noNonNullAssertion": "off",
"useBlockStatements": "error"
"useBlockStatements": "error",
"useImportType": {
"level": "error",
"options": {
"style": "separatedType"
}
}
}
}
},
Expand Down
Binary file modified bun.lockb
Binary file not shown.
47 changes: 47 additions & 0 deletions packages/get/src/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { CExpr, Expr } from '@getlang/parser/ast'
import type { RootScope } from '@getlang/parser/scope'
import type { TypeInfo } from '@getlang/parser/typeinfo'
import { Type } from '@getlang/parser/typeinfo'
import type { MaybePromise } from '@getlang/utils'
import { NullSelection } from '@getlang/utils'
import { assert } from './value.js'

type Contextual = { value: any; typeInfo: TypeInfo }

export async function withContext(
scope: RootScope<any>,
node: CExpr,
visit: (node: Expr) => MaybePromise<any>,
cb: (ctx?: Contextual) => MaybePromise<any>,
): Promise<any> {
async function unwrap(
context: Contextual | undefined,
cb: (ctx?: Contextual) => MaybePromise<any>,
): Promise<any> {
if (context?.typeInfo.type === Type.List) {
const list = []
for (const item of context.value) {
const itemCtx = { value: item, typeInfo: context.typeInfo.of }
list.push(await unwrap(itemCtx, cb))
}
return list
}

context && scope.pushContext(context.value)
const value = await cb(context)
context && scope.popContext()
return value
}

let context: Contextual | undefined
if (node.context) {
let value = await visit(node.context)
const optional = node.typeInfo.type === Type.Maybe
value = optional ? value : assert(value)
if (value instanceof NullSelection) {
return value
}
context = { value, typeInfo: node.context.typeInfo }
}
return unwrap(context, cb)
}
Loading