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

fix modifier context
10 changes: 8 additions & 2 deletions packages/get/src/calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ export async function callModifier(
) {
const entry = await registry.importMod(mod)
if (entry) {
const ctx =
context && entry.materialize ? materialize(context) : context?.data
let ctx: any
if (entry?.useContext) {
if (context && entry.materialize) {
ctx = materialize(context)
} else {
ctx = context?.data
}
}
return entry.mod(ctx, args)
}

Expand Down
27 changes: 15 additions & 12 deletions packages/get/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ type ModEntry = {

type Info = {
ast: Program
imports: Set<string>
imports: string[]
contextMods: string[]
isMacro: boolean
}

Expand Down Expand Up @@ -86,15 +87,17 @@ export class Registry {
this.info[module] ??= Promise.resolve().then(async () => {
const source = await this.hooks.import(module)
const ast = parse(source)
const { imports, ...info } = analyze(ast)
let isMacro = info.hasUnboundSelector
for (const [mod, unbound] of info.modifiers) {
if (unbound && !isMacro) {
const entry = await this.importMod(mod)
isMacro = entry?.useContext || false
const info = analyze(ast)
const imports = [...info.imports]
const contextMods: string[] = []
for (const mod of info.modifiers) {
const entry = await this.importMod(mod)
if (entry?.useContext ?? true) {
contextMods.push(mod)
}
}
return { ast, imports, isMacro }
const isMacro = info.hasUnboundSelector || contextMods.length > 0
return { ast, imports, contextMods, isMacro }
})
return this.info[module]
}
Expand All @@ -106,15 +109,15 @@ export class Registry {
}
const key = buildImportKey(module, contextType)
this.entries[key] ??= Promise.resolve().then(async () => {
const { ast, imports } = await this.getInfo(module)
const macros: string[] = []
const { ast, imports, contextMods } = await this.getInfo(module)
const contextual = [...contextMods]
for (const i of imports) {
const depInfo = await this.getInfo(i)
if (depInfo.isMacro) {
macros.push(i)
contextual.push(i)
}
}
const simplified = desugar(ast, macros)
const simplified = desugar(ast, contextual)
const { inputs, calls, modifiers } = analyze(simplified)

const returnTypes: Record<string, TypeInfo> = {}
Expand Down
6 changes: 2 additions & 4 deletions packages/parser/src/passes/analyze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function analyze(ast: Program) {
const scope = new ScopeTracker()
const inputs = new Set<string>()
const calls = new Set<string>()
const modifiers = new Map<string, boolean>()
const modifiers = new Set<string>()
const imports = new Set<string>()
let hasUnboundSelector = false

Expand All @@ -22,9 +22,7 @@ export function analyze(ast: Program) {
hasUnboundSelector ||= !scope.context
},
ModifierExpr(node) {
const mod = node.modifier.value
const unbound = modifiers.get(mod) || !scope.context
modifiers.set(mod, unbound)
modifiers.add(node.modifier.value)
},
})

Expand Down
8 changes: 4 additions & 4 deletions packages/parser/src/passes/desugar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export type DesugarPass = (
ast: Program,
tools: {
parsers: RequestParsers
macros: string[]
contextual: string[]
},
) => Program

Expand All @@ -23,13 +23,13 @@ const visitors = [
dropDrills,
]

export function desugar(ast: Program, macros: string[] = []): Program {
export function desugar(ast: Program, contextual: string[] = []): Program {
const parsers = new RequestParsers()
const program = visitors.reduce((ast, pass) => {
parsers.reset()
return pass(ast, { parsers, macros })
return pass(ast, { parsers, contextual })
}, ast)
// inference pass `registerCalls` is included in the desugar phase
// it produces the list of called modules required for type inference
return registerCalls(program, macros)
return registerCalls(program, contextual)
}
9 changes: 5 additions & 4 deletions packages/parser/src/passes/desugar/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { QuerySyntaxError } from '@getlang/lib/errors'
import { ScopeTracker, transform } from '@getlang/walker'
import type { DesugarPass } from '../desugar.js'

export const resolveContext: DesugarPass = (ast, { parsers, macros }) => {
export const resolveContext: DesugarPass = (ast, { parsers, contextual }) => {
const scope = new ScopeTracker()

const program = transform(ast, {
Expand Down Expand Up @@ -34,16 +34,17 @@ export const resolveContext: DesugarPass = (ast, { parsers, macros }) => {

ModifierExpr(node) {
const ctx = scope.context
if (ctx?.kind === 'RequestExpr') {
const mod = node.modifier.value
const mod = node.modifier.value
if (contextual.includes(mod) && ctx?.kind === 'RequestExpr') {
// replace modifier with shared parser
return parsers.lookup(ctx, mod)
}
},

ModuleExpr(node, path) {
const ctx = scope.context
if (macros.includes(node.module.value) && ctx?.kind === 'RequestExpr') {
const module = node.module.value
if (contextual.includes(module) && ctx?.kind === 'RequestExpr') {
path.insertBefore(parsers.lookup(ctx))
}
},
Expand Down
7 changes: 5 additions & 2 deletions packages/parser/src/passes/inference/calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { isToken } from '@getlang/ast'
import { transform } from '@getlang/walker'
import { LineageTracker } from '../lineage.js'

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

function registerCall(node: Expr) {
Expand Down Expand Up @@ -38,7 +41,7 @@ export function registerCalls(ast: Program, macros: string[] = []): Program {
},

ModuleExpr(node) {
if (macros.includes(node.module.value)) {
if (contextual.includes(node.module.value)) {
return { ...node, call: true }
}
},
Expand Down
31 changes: 30 additions & 1 deletion test/modifiers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ import { describe, expect, test } from 'bun:test'
import type { Modifier, ModifierHook } from '@getlang/lib'
import { invariant } from '@getlang/lib'
import { ValueTypeError } from '@getlang/lib/errors'
import type { Fetch } from './helpers.js'
import { execute as exec } from './helpers.js'

function execute(
source: string | Record<string, string>,
name: string,
fn: Modifier,
useContext?: boolean,
fetch?: Fetch,
) {
const modifier: ModifierHook = mod => {
expect(mod).toEqual(name)
return { modifier: fn, useContext }
}
return exec(source, {}, { hooks: { modifier } })
return exec(source, {}, { fetch, hooks: { modifier } })
}

describe('modifiers', () => {
Expand All @@ -31,10 +33,37 @@ describe('modifiers', () => {
expect(ctx).toEqual(1)
return ctx + 1
},
true,
)
expect(result).toEqual(2)
})

test('without context', async () => {
const result = await execute(
`
GET http://example.com

set url = $ -> url
set value = @nocontext

extract { $url, $value }
`,
'nocontext',
ctx => {
expect(ctx).toBeUndefined()
return 123
},
false,
() =>
new Response('<!doctype html><h1>test</h1>', {
headers: {
'content-type': 'text/html',
},
}),
)
expect(result).toEqual({ url: 'http://example.com/', value: 123 })
})

test('with args', async () => {
const result = await execute(
'extract @product({ a: 7, b: 6 })',
Expand Down