@@ -3,13 +3,11 @@ import { Type } from '@getlang/ast'
33import type { Hooks , Modifier } from '@getlang/lib'
44import { RecursiveCallError , ValueTypeError } from '@getlang/lib/errors'
55import { analyze , desugar , inference , parse } from '@getlang/parser'
6- import type { Pattern } from 'acorn'
7- import { parse as acorn } from 'acorn'
8- import { traverse } from 'estree-toolkit'
96
107type ModEntry = {
118 mod : Modifier
129 useContext : boolean
10+ materialize : boolean
1311 returnType : TypeInfo
1412}
1513
@@ -53,31 +51,6 @@ function buildImportKey(module: string, typeInfo?: TypeInfo) {
5351 return key
5452}
5553
56- function inferContext ( mod : Modifier ) {
57- const src = mod . toString ( )
58- const ast = acorn ( src , { ecmaVersion : 'latest' } )
59- let useContext = false
60- traverse ( ast , {
61- $ : { scope : true } ,
62- Program ( path ) {
63- const fn = ast . body [ 0 ]
64- let ctxParam : Pattern | undefined
65- if ( fn ?. type === 'FunctionDeclaration' ) {
66- ctxParam = fn . params [ 0 ]
67- } else if ( fn ?. type === 'ExpressionStatement' ) {
68- if ( fn . expression . type === 'ArrowFunctionExpression' ) {
69- ctxParam = fn . expression . params [ 0 ]
70- }
71- }
72- const fnScope = path . scope ?. children [ 0 ]
73- const bindings = Object . values ( fnScope ?. bindings || { } )
74- const ctxBinding = bindings . find ( b => b ?. identifierPath . node === ctxParam )
75- useContext = Boolean ( ctxBinding ?. references . length )
76- } ,
77- } )
78- return useContext
79- }
80-
8154export class Registry {
8255 private info : Record < string , Promise < Info > > = { }
8356 private entries : Record < string , Promise < Entry > > = { }
@@ -91,10 +64,20 @@ export class Registry {
9164 if ( ! compiled ) {
9265 return null
9366 }
94- const fn = compiled . modifier
95- const useContext = inferContext ( fn )
96- const returnType = compiled . typeInfo || { type : Type . Value }
97- return { mod : fn , useContext, returnType }
67+
68+ const {
69+ modifier : fn ,
70+ useContext = false ,
71+ materialize = true ,
72+ returnType = { type : Type . Value } ,
73+ } = compiled
74+
75+ return {
76+ mod : fn ,
77+ useContext,
78+ materialize,
79+ returnType,
80+ }
9881 } )
9982 return this . modifiers [ mod ]
10083 }
0 commit comments