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
7 changes: 7 additions & 0 deletions .changeset/chilly-bobcats-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@getlang/parser": patch
"@getlang/get": patch
"@getlang/lib": patch
---

modifier flags: `useContext` and `materialize`
66 changes: 32 additions & 34 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "get",
"license": "Apache-2.0",
"private": true,
"packageManager": "bun@1.2.21",
"packageManager": "bun@1.2.22",
"scripts": {
"fmt": "biome check --write",
"lint": "bun lint:check && bun lint:types && bun lint:unused && bun lint:repo",
Expand All @@ -18,10 +18,10 @@
"test"
],
"devDependencies": {
"@biomejs/biome": "^2.2.3",
"@biomejs/biome": "^2.2.4",
"@changesets/changelog-github": "^0.5.1",
"@changesets/cli": "^2.29.6",
"@types/bun": "^1.2.21",
"@changesets/cli": "^2.29.7",
"@types/bun": "^1.2.22",
"knip": "^5.63.1",
"sherif": "^1.6.1",
"typescript": "^5.9.2"
Expand Down
2 changes: 0 additions & 2 deletions packages/get/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
"@getlang/lib": "workspace:^0.2.0",
"@getlang/parser": "workspace:^0.4.0",
"@getlang/walker": "workspace:^0.1.0",
"acorn": "^8.15.0",
"estree-toolkit": "^1.7.13",
"lodash-es": "^4.17.21"
},
"devDependencies": {
Expand Down
4 changes: 3 additions & 1 deletion packages/get/src/calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export async function callModifier(
) {
const entry = await registry.importMod(mod)
if (entry) {
return entry.mod(context?.data, args)
const ctx =
context && entry.materialize ? materialize(context) : context?.data
return entry.mod(ctx, args)
}

invariant(context, 'Modifier requires context')
Expand Down
11 changes: 4 additions & 7 deletions packages/get/src/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export async function execute(
async SliceExpr({ slice, typeInfo }) {
try {
const ctx = scope.context
const deps = ctx && materialize(ctx)
const deps = ctx ? materialize(ctx) : {}
const ret = await hooks.slice(slice.value, deps)
const data =
ret === undefined ? new lib.NullSelection('<slice>') : ret
Expand Down Expand Up @@ -158,12 +158,9 @@ export async function execute(
},

async ModifierExpr(node) {
const data = await callModifier(
registry,
node.modifier.value,
node.args.data,
scope.context,
)
const mod = node.modifier.value
const args = materialize(node.args)
const data = await callModifier(registry, mod, args, scope.context)
return { data, typeInfo: node.typeInfo }
},

Expand Down
47 changes: 15 additions & 32 deletions packages/get/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ import { Type } from '@getlang/ast'
import type { Hooks, Modifier } from '@getlang/lib'
import { RecursiveCallError, ValueTypeError } from '@getlang/lib/errors'
import { analyze, desugar, inference, parse } from '@getlang/parser'
import type { Pattern } from 'acorn'
import { parse as acorn } from 'acorn'
import { traverse } from 'estree-toolkit'

type ModEntry = {
mod: Modifier
useContext: boolean
materialize: boolean
returnType: TypeInfo
}

Expand Down Expand Up @@ -53,31 +51,6 @@ function buildImportKey(module: string, typeInfo?: TypeInfo) {
return key
}

function inferContext(mod: Modifier) {
const src = mod.toString()
const ast = acorn(src, { ecmaVersion: 'latest' })
let useContext = false
traverse(ast, {
$: { scope: true },
Program(path) {
const fn = ast.body[0]
let ctxParam: Pattern | undefined
if (fn?.type === 'FunctionDeclaration') {
ctxParam = fn.params[0]
} else if (fn?.type === 'ExpressionStatement') {
if (fn.expression.type === 'ArrowFunctionExpression') {
ctxParam = fn.expression.params[0]
}
}
const fnScope = path.scope?.children[0]
const bindings = Object.values(fnScope?.bindings || {})
const ctxBinding = bindings.find(b => b?.identifierPath.node === ctxParam)
useContext = Boolean(ctxBinding?.references.length)
},
})
return useContext
}

export class Registry {
private info: Record<string, Promise<Info>> = {}
private entries: Record<string, Promise<Entry>> = {}
Expand All @@ -91,10 +64,20 @@ export class Registry {
if (!compiled) {
return null
}
const fn = compiled.modifier
const useContext = inferContext(fn)
const returnType = compiled.typeInfo || { type: Type.Value }
return { mod: fn, useContext, returnType }

const {
modifier: fn,
useContext = false,
materialize = true,
returnType = { type: Type.Value },
} = compiled

return {
mod: fn,
useContext,
materialize,
returnType,
}
})
return this.modifiers[mod]
}
Expand Down
4 changes: 3 additions & 1 deletion packages/lib/src/core/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export type Modifier = (context: any, options: Record<string, unknown>) => any
export type ModifierHook = (modifier: string) => MaybePromise<
| {
modifier: Modifier
typeInfo?: TypeInfo
useContext?: boolean
materialize?: boolean
returnType?: TypeInfo
}
| undefined
>
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@types/nearley": "^2.11.5",
"acorn": "^8.15.0",
"estree-toolkit": "^1.7.13",
"globals": "^16.3.0",
"globals": "^16.4.0",
"lodash-es": "^4.17.21",
"moo": "^0.5.2",
"nearley": "^2.20.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/passes/desugar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const visitors = [
dropDrills,
]

export function desugar(ast: Program, macros: string[] = []) {
export function desugar(ast: Program, macros: string[] = []): Program {
const parsers = new RequestParsers()
const program = visitors.reduce((ast, pass) => {
parsers.reset()
Expand Down
2 changes: 1 addition & 1 deletion packages/parser/src/passes/inference/calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { isToken } from '@getlang/ast'
import { transform } from '@getlang/walker'
import { LineageTracker } from '../lineage.js'

export function registerCalls(ast: Program, macros: string[] = []) {
export function registerCalls(ast: Program, macros: string[] = []): Program {
const scope = new LineageTracker()

function registerCall(node: Expr) {
Expand Down
Loading