@arkitektio/bsx 4.3.0 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (349) hide show
  1. package/README.md +39 -18
  2. package/dist/bsx/analyze.d.ts +6 -5
  3. package/dist/bsx/analyze.d.ts.map +1 -1
  4. package/dist/bsx/analyze.js +12 -10
  5. package/dist/bsx/analyze.js.map +1 -1
  6. package/dist/bsx/declared.d.ts +111 -0
  7. package/dist/bsx/declared.d.ts.map +1 -0
  8. package/dist/bsx/declared.js +311 -0
  9. package/dist/bsx/declared.js.map +1 -0
  10. package/dist/bsx/index.d.ts +1 -0
  11. package/dist/bsx/index.d.ts.map +1 -1
  12. package/dist/bsx/index.js +1 -0
  13. package/dist/bsx/index.js.map +1 -1
  14. package/dist/bsx/infer.d.ts +40 -17
  15. package/dist/bsx/infer.d.ts.map +1 -1
  16. package/dist/bsx/infer.js +105 -49
  17. package/dist/bsx/infer.js.map +1 -1
  18. package/dist/bsx/language.d.ts +6 -2
  19. package/dist/bsx/language.d.ts.map +1 -1
  20. package/dist/bsx/language.js +65 -21
  21. package/dist/bsx/language.js.map +1 -1
  22. package/dist/bsx/parse.d.ts +19 -8
  23. package/dist/bsx/parse.d.ts.map +1 -1
  24. package/dist/bsx/parse.js +49 -59
  25. package/dist/bsx/parse.js.map +1 -1
  26. package/dist/bsx/print.d.ts.map +1 -1
  27. package/dist/bsx/print.js +25 -35
  28. package/dist/bsx/print.js.map +1 -1
  29. package/dist/bsx/references.d.ts +48 -54
  30. package/dist/bsx/references.d.ts.map +1 -1
  31. package/dist/bsx/references.js +359 -159
  32. package/dist/bsx/references.js.map +1 -1
  33. package/dist/catalogs/html.d.ts +3 -3
  34. package/dist/catalogs/html.d.ts.map +1 -1
  35. package/dist/catalogs/html.js +3 -3
  36. package/dist/catalogs/html.js.map +1 -1
  37. package/dist/catalogs/index.d.ts +172 -180
  38. package/dist/catalogs/index.d.ts.map +1 -1
  39. package/dist/catalogs/index.js +6 -2
  40. package/dist/catalogs/index.js.map +1 -1
  41. package/dist/catalogs/logo.d.ts +19 -33
  42. package/dist/catalogs/logo.d.ts.map +1 -1
  43. package/dist/catalogs/logo.js +4 -5
  44. package/dist/catalogs/logo.js.map +1 -1
  45. package/dist/catalogs/parts/data.d.ts +6 -6
  46. package/dist/catalogs/parts/feedback.d.ts +15 -15
  47. package/dist/catalogs/parts/forms.d.ts +24 -24
  48. package/dist/catalogs/parts/html.d.ts +3 -3
  49. package/dist/catalogs/parts/html.d.ts.map +1 -1
  50. package/dist/catalogs/parts/html.js +1 -1
  51. package/dist/catalogs/parts/html.js.map +1 -1
  52. package/dist/catalogs/parts/icons.d.ts +11 -11
  53. package/dist/catalogs/parts/icons.js +1 -1
  54. package/dist/catalogs/parts/index.d.ts +153 -165
  55. package/dist/catalogs/parts/index.d.ts.map +1 -1
  56. package/dist/catalogs/parts/layout.d.ts +11 -1
  57. package/dist/catalogs/parts/layout.d.ts.map +1 -1
  58. package/dist/catalogs/parts/layout.js +9 -0
  59. package/dist/catalogs/parts/layout.js.map +1 -1
  60. package/dist/catalogs/parts/logo.d.ts +17 -32
  61. package/dist/catalogs/parts/logo.d.ts.map +1 -1
  62. package/dist/catalogs/parts/logo.js +13 -15
  63. package/dist/catalogs/parts/logo.js.map +1 -1
  64. package/dist/catalogs/parts/navigation.d.ts +20 -20
  65. package/dist/catalogs/parts/overlays.d.ts +55 -55
  66. package/dist/catalogs/parts/run.d.ts +5 -13
  67. package/dist/catalogs/parts/run.d.ts.map +1 -1
  68. package/dist/catalogs/parts/run.js +1 -1
  69. package/dist/catalogs/parts/scene.d.ts +9 -17
  70. package/dist/catalogs/parts/scene.d.ts.map +1 -1
  71. package/dist/catalogs/parts/scene.js +1 -5
  72. package/dist/catalogs/parts/scene.js.map +1 -1
  73. package/dist/catalogs/parts/structure.js +2 -2
  74. package/dist/catalogs/parts/toast.js +1 -1
  75. package/dist/catalogs/parts/toast.js.map +1 -1
  76. package/dist/catalogs/parts/typography.d.ts +3 -3
  77. package/dist/catalogs/parts/typography.js +1 -1
  78. package/dist/catalogs/parts/typography.js.map +1 -1
  79. package/dist/catalogs/props.d.ts +4 -4
  80. package/dist/catalogs/scene.d.ts +8 -16
  81. package/dist/catalogs/scene.d.ts.map +1 -1
  82. package/dist/catalogs/scene.js +3 -3
  83. package/dist/catalogs/scene.js.map +1 -1
  84. package/dist/catalogs/shadcn.d.ts +58 -58
  85. package/dist/catalogs/shadcn.d.ts.map +1 -1
  86. package/dist/catalogs/shadcn.js +4 -4
  87. package/dist/catalogs/shadcn.js.map +1 -1
  88. package/dist/catalogs/structure.d.ts +1 -1
  89. package/dist/catalogs/structure.d.ts.map +1 -1
  90. package/dist/catalogs/structure.js +3 -3
  91. package/dist/catalogs/structure.js.map +1 -1
  92. package/dist/catalogs/ui.d.ts +83 -73
  93. package/dist/catalogs/ui.d.ts.map +1 -1
  94. package/dist/catalogs/ui.js +4 -3
  95. package/dist/catalogs/ui.js.map +1 -1
  96. package/dist/catalogs.d.ts +1 -1
  97. package/dist/catalogs.d.ts.map +1 -1
  98. package/dist/catalogs.js +2 -2
  99. package/dist/catalogs.js.map +1 -1
  100. package/dist/logo/actionLogoSource.d.ts +3 -3
  101. package/dist/logo/actionLogoSource.d.ts.map +1 -1
  102. package/dist/logo/actionLogoSource.js +4 -4
  103. package/dist/logo/actionLogoSource.js.map +1 -1
  104. package/dist/ports.js +1 -1
  105. package/dist/ports.js.map +1 -1
  106. package/dist/renderer/BlokRenderer.d.ts +10 -6
  107. package/dist/renderer/BlokRenderer.d.ts.map +1 -1
  108. package/dist/renderer/BlokRenderer.js +23 -9
  109. package/dist/renderer/BlokRenderer.js.map +1 -1
  110. package/dist/renderer/baseCatalog.d.ts +8 -6
  111. package/dist/renderer/baseCatalog.d.ts.map +1 -1
  112. package/dist/renderer/baseCatalog.js +7 -5
  113. package/dist/renderer/baseCatalog.js.map +1 -1
  114. package/dist/renderer/bloks/control.d.ts +20 -2
  115. package/dist/renderer/bloks/control.d.ts.map +1 -1
  116. package/dist/renderer/bloks/control.js +98 -15
  117. package/dist/renderer/bloks/control.js.map +1 -1
  118. package/dist/renderer/bloks/data.d.ts +6 -6
  119. package/dist/renderer/bloks/feedback.d.ts +15 -15
  120. package/dist/renderer/bloks/forms.d.ts +24 -24
  121. package/dist/renderer/bloks/html.d.ts +2 -2
  122. package/dist/renderer/bloks/html.js +2 -2
  123. package/dist/renderer/bloks/icons.d.ts +5 -5
  124. package/dist/renderer/bloks/index.d.ts +145 -144
  125. package/dist/renderer/bloks/index.d.ts.map +1 -1
  126. package/dist/renderer/bloks/index.js +1 -1
  127. package/dist/renderer/bloks/layout.d.ts +9 -1
  128. package/dist/renderer/bloks/layout.d.ts.map +1 -1
  129. package/dist/renderer/bloks/layout.js +15 -0
  130. package/dist/renderer/bloks/layout.js.map +1 -1
  131. package/dist/renderer/bloks/logo/index.d.ts +6 -13
  132. package/dist/renderer/bloks/logo/index.d.ts.map +1 -1
  133. package/dist/renderer/bloks/logo/index.js +3 -4
  134. package/dist/renderer/bloks/logo/index.js.map +1 -1
  135. package/dist/renderer/bloks/logo/logo.fixture.json +266 -250
  136. package/dist/renderer/bloks/navigation.d.ts +20 -20
  137. package/dist/renderer/bloks/overlays.d.ts +55 -55
  138. package/dist/renderer/bloks/run/components.d.ts +2 -6
  139. package/dist/renderer/bloks/run/components.d.ts.map +1 -1
  140. package/dist/renderer/bloks/run/components.js +1 -1
  141. package/dist/renderer/bloks/scene/index.d.ts +5 -9
  142. package/dist/renderer/bloks/scene/index.d.ts.map +1 -1
  143. package/dist/renderer/bloks/scene/interaction.d.ts.map +1 -1
  144. package/dist/renderer/bloks/scene/interaction.js +3 -2
  145. package/dist/renderer/bloks/scene/interaction.js.map +1 -1
  146. package/dist/renderer/bloks/shared/props.d.ts +2 -2
  147. package/dist/renderer/bloks/toast.d.ts +1 -1
  148. package/dist/renderer/bloks/typography.d.ts +3 -3
  149. package/dist/renderer/bloks/typography.js +1 -1
  150. package/dist/renderer/catalog.d.ts +4 -7
  151. package/dist/renderer/catalog.d.ts.map +1 -1
  152. package/dist/renderer/catalog.js +7 -31
  153. package/dist/renderer/catalog.js.map +1 -1
  154. package/dist/renderer/declarations.d.ts +40 -33
  155. package/dist/renderer/declarations.d.ts.map +1 -1
  156. package/dist/renderer/declarations.js +41 -56
  157. package/dist/renderer/declarations.js.map +1 -1
  158. package/dist/renderer/declare.d.ts +31 -19
  159. package/dist/renderer/declare.d.ts.map +1 -1
  160. package/dist/renderer/declare.js +44 -18
  161. package/dist/renderer/declare.js.map +1 -1
  162. package/dist/renderer/persist.d.ts +22 -13
  163. package/dist/renderer/persist.d.ts.map +1 -1
  164. package/dist/renderer/persist.js +40 -21
  165. package/dist/renderer/persist.js.map +1 -1
  166. package/dist/renderer/runtime/catalog.d.ts +27 -7
  167. package/dist/renderer/runtime/catalog.d.ts.map +1 -1
  168. package/dist/renderer/runtime/catalog.js +30 -3
  169. package/dist/renderer/runtime/catalog.js.map +1 -1
  170. package/dist/renderer/runtime/components.d.ts +10 -4
  171. package/dist/renderer/runtime/components.d.ts.map +1 -1
  172. package/dist/renderer/runtime/components.js +8 -5
  173. package/dist/renderer/runtime/components.js.map +1 -1
  174. package/dist/renderer/runtime/context.d.ts +5 -5
  175. package/dist/renderer/runtime/context.d.ts.map +1 -1
  176. package/dist/renderer/runtime/context.js +11 -5
  177. package/dist/renderer/runtime/context.js.map +1 -1
  178. package/dist/renderer/runtime/describe.d.ts +16 -6
  179. package/dist/renderer/runtime/describe.d.ts.map +1 -1
  180. package/dist/renderer/runtime/describe.js +5 -2
  181. package/dist/renderer/runtime/describe.js.map +1 -1
  182. package/dist/renderer/runtime/hooks.d.ts.map +1 -1
  183. package/dist/renderer/runtime/hooks.js +8 -4
  184. package/dist/renderer/runtime/hooks.js.map +1 -1
  185. package/dist/renderer/runtime/index.d.ts +2 -0
  186. package/dist/renderer/runtime/index.d.ts.map +1 -1
  187. package/dist/renderer/runtime/index.js +2 -0
  188. package/dist/renderer/runtime/index.js.map +1 -1
  189. package/dist/renderer/runtime/locals.d.ts +57 -0
  190. package/dist/renderer/runtime/locals.d.ts.map +1 -0
  191. package/dist/renderer/runtime/locals.js +87 -0
  192. package/dist/renderer/runtime/locals.js.map +1 -0
  193. package/dist/renderer/runtime/preflight.d.ts.map +1 -1
  194. package/dist/renderer/runtime/preflight.js +5 -2
  195. package/dist/renderer/runtime/preflight.js.map +1 -1
  196. package/dist/renderer/runtime/scope.d.ts +14 -0
  197. package/dist/renderer/runtime/scope.d.ts.map +1 -1
  198. package/dist/renderer/runtime/scope.js +25 -3
  199. package/dist/renderer/runtime/scope.js.map +1 -1
  200. package/dist/renderer/runtime/text.d.ts +28 -0
  201. package/dist/renderer/runtime/text.d.ts.map +1 -0
  202. package/dist/renderer/runtime/text.js +82 -0
  203. package/dist/renderer/runtime/text.js.map +1 -0
  204. package/dist/renderer/runtime/tree.d.ts.map +1 -1
  205. package/dist/renderer/runtime/tree.js +7 -1
  206. package/dist/renderer/runtime/tree.js.map +1 -1
  207. package/dist/renderer/runtime/types.d.ts +8 -3
  208. package/dist/renderer/runtime/types.d.ts.map +1 -1
  209. package/dist/runtime.d.ts +0 -1
  210. package/dist/runtime.d.ts.map +1 -1
  211. package/dist/runtime.js +0 -1
  212. package/dist/runtime.js.map +1 -1
  213. package/dist/scene.d.ts +1 -1
  214. package/dist/scene.d.ts.map +1 -1
  215. package/dist/scene.js +2 -2
  216. package/dist/scene.js.map +1 -1
  217. package/dist/shadcn.d.ts +154 -153
  218. package/dist/shadcn.d.ts.map +1 -1
  219. package/dist/shadcn.js +7 -7
  220. package/dist/shadcn.js.map +1 -1
  221. package/dist/spec/bsx_v2.json +12324 -6934
  222. package/dist/spec/catalogs/base@4.json +13175 -0
  223. package/dist/spec/catalogs/base@4.json.js +13175 -0
  224. package/dist/spec/catalogs/orkestrator@1.json +489 -0
  225. package/dist/spec/catalogs/orkestrator@1.json.js +489 -0
  226. package/dist/spec/catalogs.d.ts +109 -46
  227. package/dist/spec/catalogs.d.ts.map +1 -1
  228. package/dist/spec/catalogs.js +243 -75
  229. package/dist/spec/catalogs.js.map +1 -1
  230. package/dist/spec/vocabulary.d.ts +44 -7
  231. package/dist/spec/vocabulary.d.ts.map +1 -1
  232. package/dist/spec/vocabulary.js +65 -7
  233. package/dist/spec/vocabulary.js.map +1 -1
  234. package/dist/spec/vocabulary.json +83 -43
  235. package/dist/spec/vocabulary.json.js +83 -43
  236. package/dist/vendor/core/ui/button-group.d.ts +1 -1
  237. package/dist/vendor/core/ui/button.d.ts +1 -1
  238. package/dist/vendor/core/ui/field.d.ts +1 -1
  239. package/package.json +1 -1
  240. package/src/bsx/analyze.ts +14 -12
  241. package/src/bsx/bsx.test.ts +4 -4
  242. package/src/bsx/declared.ts +346 -0
  243. package/src/bsx/index.ts +1 -0
  244. package/src/bsx/infer.test.ts +54 -52
  245. package/src/bsx/infer.ts +128 -71
  246. package/src/bsx/language.test.ts +37 -25
  247. package/src/bsx/language.ts +72 -25
  248. package/src/bsx/parse.ts +51 -64
  249. package/src/bsx/partial.test.ts +1 -1
  250. package/src/bsx/print.ts +27 -34
  251. package/src/bsx/references.test.ts +86 -83
  252. package/src/bsx/references.ts +391 -198
  253. package/src/catalogs/catalogs.test.ts +46 -14
  254. package/src/catalogs/html.ts +3 -3
  255. package/src/catalogs/index.ts +6 -2
  256. package/src/catalogs/logo.ts +4 -5
  257. package/src/catalogs/parts/html.ts +1 -1
  258. package/src/catalogs/parts/icons.ts +1 -1
  259. package/src/catalogs/parts/layout.ts +11 -0
  260. package/src/catalogs/parts/logo.ts +14 -18
  261. package/src/catalogs/parts/run.ts +1 -1
  262. package/src/catalogs/parts/scene.ts +1 -7
  263. package/src/catalogs/parts/structure.ts +2 -2
  264. package/src/catalogs/parts/toast.ts +1 -1
  265. package/src/catalogs/parts/typography.ts +1 -1
  266. package/src/catalogs/scene.ts +3 -3
  267. package/src/catalogs/shadcn.ts +4 -4
  268. package/src/catalogs/structure.ts +3 -3
  269. package/src/catalogs/ui.ts +4 -3
  270. package/src/catalogs.ts +9 -2
  271. package/src/logo/actionLogoSource.ts +4 -4
  272. package/src/ports.ts +1 -1
  273. package/src/renderer/BlokComponentHost.test.tsx +5 -5
  274. package/src/renderer/BlokRenderer.tsx +46 -14
  275. package/src/renderer/addBlokComponents.test.tsx +8 -22
  276. package/src/renderer/baseCatalog.ts +7 -5
  277. package/src/renderer/bloks/components.test.tsx +2 -2
  278. package/src/renderer/bloks/control.tsx +136 -17
  279. package/src/renderer/bloks/flow.test.tsx +1 -1
  280. package/src/renderer/bloks/html.tsx +2 -2
  281. package/src/renderer/bloks/index.ts +1 -1
  282. package/src/renderer/bloks/layout.tsx +28 -0
  283. package/src/renderer/bloks/logo/index.tsx +4 -5
  284. package/src/renderer/bloks/logo/logo.fixture.json +266 -250
  285. package/src/renderer/bloks/logo/logo.test.tsx +18 -7
  286. package/src/renderer/bloks/run/components.tsx +1 -1
  287. package/src/renderer/bloks/run/space.test.tsx +12 -4
  288. package/src/renderer/bloks/scene/interaction.tsx +11 -6
  289. package/src/renderer/bloks/scene/transform.test.ts +8 -7
  290. package/src/renderer/bloks/time.test.tsx +1 -1
  291. package/src/renderer/bloks/toast.tsx +1 -1
  292. package/src/renderer/bloks/typography.tsx +1 -1
  293. package/src/renderer/catalog.tsx +7 -31
  294. package/src/renderer/declarations.test.ts +38 -51
  295. package/src/renderer/declarations.ts +67 -88
  296. package/src/renderer/declare.test.ts +41 -0
  297. package/src/renderer/declare.ts +70 -32
  298. package/src/renderer/persist.test.ts +70 -49
  299. package/src/renderer/persist.ts +57 -34
  300. package/src/renderer/runtime/catalog.test.ts +2 -2
  301. package/src/renderer/runtime/catalog.ts +35 -10
  302. package/src/renderer/runtime/components.tsx +17 -8
  303. package/src/renderer/runtime/context.tsx +17 -10
  304. package/src/renderer/runtime/describe.ts +21 -8
  305. package/src/renderer/runtime/hooks.ts +6 -4
  306. package/src/renderer/runtime/index.ts +2 -0
  307. package/src/renderer/runtime/locals.tsx +105 -0
  308. package/src/renderer/runtime/preflight.ts +5 -2
  309. package/src/renderer/runtime/scope.tsx +31 -3
  310. package/src/renderer/runtime/text.test.tsx +65 -0
  311. package/src/renderer/runtime/text.ts +88 -0
  312. package/src/renderer/runtime/tree.tsx +16 -4
  313. package/src/renderer/runtime/types.ts +8 -3
  314. package/src/runtime.ts +0 -1
  315. package/src/scene.ts +2 -2
  316. package/src/shadcn.ts +7 -7
  317. package/src/spec/bsx_v2.json +12324 -6934
  318. package/src/spec/catalogs/base@4.json +13175 -0
  319. package/src/spec/catalogs/orkestrator@1.json +489 -0
  320. package/src/spec/catalogs.ts +259 -103
  321. package/src/spec/spec.test.ts +93 -24
  322. package/src/spec/vocabulary.json +83 -43
  323. package/src/spec/vocabulary.ts +102 -17
  324. package/dist/renderer/modules.d.ts +0 -4
  325. package/dist/renderer/modules.d.ts.map +0 -1
  326. package/dist/renderer/modules.js +0 -11
  327. package/dist/renderer/modules.js.map +0 -1
  328. package/dist/spec/catalogs/base@2.json +0 -355
  329. package/dist/spec/catalogs/base@2.json.js +0 -355
  330. package/dist/spec/catalogs/html@1.json +0 -308
  331. package/dist/spec/catalogs/html@1.json.js +0 -308
  332. package/dist/spec/catalogs/logo@1.json +0 -1687
  333. package/dist/spec/catalogs/logo@1.json.js +0 -1687
  334. package/dist/spec/catalogs/scene@1.json +0 -878
  335. package/dist/spec/catalogs/scene@1.json.js +0 -878
  336. package/dist/spec/catalogs/shadcn@1.json +0 -4002
  337. package/dist/spec/catalogs/shadcn@1.json.js +0 -4002
  338. package/dist/spec/catalogs/structure@1.json +0 -185
  339. package/dist/spec/catalogs/structure@1.json.js +0 -185
  340. package/dist/spec/catalogs/ui@1.json +0 -5349
  341. package/dist/spec/catalogs/ui@1.json.js +0 -5349
  342. package/src/renderer/modules.ts +0 -12
  343. package/src/spec/catalogs/base@2.json +0 -355
  344. package/src/spec/catalogs/html@1.json +0 -308
  345. package/src/spec/catalogs/logo@1.json +0 -1687
  346. package/src/spec/catalogs/scene@1.json +0 -878
  347. package/src/spec/catalogs/shadcn@1.json +0 -4002
  348. package/src/spec/catalogs/structure@1.json +0 -185
  349. package/src/spec/catalogs/ui@1.json +0 -5349
package/src/bsx/infer.ts CHANGED
@@ -1,22 +1,14 @@
1
1
  import type {BlokActionArgument, BlokAgentCall, BlokComponentNode, BlokComponentProp} from '../renderer/runtime/types';
2
2
  import type {BlokView} from '../spec/catalogs';
3
3
  import {LOCAL_ANY_KIND} from '../spec/vocabulary';
4
- import type {
5
- BlokCheckScope,
6
- BlokPortMatch,
7
- BlokScopeDependency,
8
- BlokScopeLocal,
9
- BlokScopeLocalPort,
10
- ResolvedBlokLocal,
11
- ResolvedBlokScope,
12
- } from './references';
4
+ import {declaredLocal, iterWithParents, LOCAL_COMPONENT, localLike, NAME_PROP} from './declared';
5
+ import type {BlokPortMatch, BlokScopeDependency, BlokScopeLocal, BlokScopeLocalPort, ResolvedBlokLocal} from './references';
13
6
 
14
7
  /**
15
- * Resolve what a blok leaves to its tree: the kinds of its locals. The port
16
- * of arkitekt-spec's
17
- * `declare/blok/infer.py` (`resolve_scope`); the server and takt resolve the
18
- * same way on save, so a source blok sent without kinds is stored the same
19
- * everywhere.
8
+ * Resolve what a blok leaves to its tree: the kinds of its `<local>`
9
+ * declarations. The port of arkitekt-spec's `declare/blok/infer.py`
10
+ * (`resolve_locals`); the server and takt resolve the same way on save, so a
11
+ * source blok sent without kinds is stored the same everywhere.
20
12
  *
21
13
  * **A local's kind**, when it is left out (a given kind, `ANY` included, is
22
14
  * kept), is the first of:
@@ -29,8 +21,11 @@ import type {
29
21
  * STRING, INT, FLOAT or BOOL;
30
22
  * 4. else ANY: an untyped value, never an error.
31
23
  *
32
- * Every field the local sets wins over the inferred type, which only fills
33
- * what it leaves out; a candidate that makes no valid port is skipped.
24
+ * A given `kind` and `like="self.<action>.<arg>"` come before all of these
25
+ * (`like` is resolved only where the blok is mounted). Only uses in the
26
+ * local's scope (its parent's subtree) count. Every field the local sets wins
27
+ * over the inferred type, which only fills what it leaves out; a candidate
28
+ * that makes no valid port is skipped.
34
29
  *
35
30
  * Nothing else is inferred: every `deps.<key>` a blok reads, whatever its
36
31
  * kind, must be declared (`deps.unknown` otherwise, its hint naming the
@@ -153,9 +148,15 @@ const toLocalPort = (port: InferredPort): BlokScopeLocalPort => ({
153
148
  export const anyLocal = (local: BlokScopeLocal): ResolvedBlokLocal => ({...ownFields(local), kind: LOCAL_ANY});
154
149
 
155
150
  /** `local` with its kind resolved by the rules above (a given kind is kept). */
156
- export const resolveLocal = (local: BlokScopeLocal, args: readonly InferredType[], binds: readonly string[]): ResolvedBlokLocal => {
151
+ export const resolveLocal = (
152
+ local: BlokScopeLocal,
153
+ args: readonly InferredType[],
154
+ binds: readonly string[],
155
+ borrowed: InferredType | null = null,
156
+ ): ResolvedBlokLocal => {
157
157
  if (kindGiven(local)) return {...local, kind: local.kind ?? LOCAL_ANY};
158
158
  const candidates: InferredType[] = [];
159
+ if (borrowed !== null) candidates.push(borrowed);
159
160
  const fromDefault = defaultType(local.default);
160
161
  if (fromDefault !== null) candidates.push(fromDefault);
161
162
  candidates.push(...args);
@@ -173,21 +174,69 @@ export const resolveLocal = (local: BlokScopeLocal, args: readonly InferredType[
173
174
 
174
175
  const actionKeyFor = (call: BlokAgentCall): string => call.operation.split('.').pop() ?? call.operation;
175
176
 
177
+ /** What a name in scope is bound to here: a `<local>`'s node id, or null for a `let` or an `into` name. */
178
+ type UseScope = ReadonlyMap<string, string | null>;
179
+
180
+ /** The local a `<local>` node declares by name: its `name` prop's `declares_value`. */
181
+ export const localDeclared = (node: BlokComponentNode): string | null => {
182
+ if (node.component !== LOCAL_COMPONENT) return null;
183
+ return node.props?.find(prop => prop.key === NAME_PROP && prop.declares_value)?.declares_value ?? null;
184
+ };
185
+
186
+ /**
187
+ * The type of an action's argument as the mount knows it: the arg match of a
188
+ * dependency's demand (`self` included); null when it does not tell.
189
+ */
190
+ export const argumentType = (
191
+ dependency: string,
192
+ action: string,
193
+ argument: string,
194
+ dependencies: ReadonlyMap<string, BlokScopeDependency>,
195
+ ): InferredType | null => {
196
+ const demand = dependencies.get(dependency)?.action_dependencies?.find(each => each.key === action)?.demand;
197
+ const match = demand?.arg_matches?.find(each => each.key === argument);
198
+ return match ? matchType(match) : null;
199
+ };
200
+
201
+ /**
202
+ * What `like="self.<action>.<arg>"` / `"deps.<key>.<action>.<arg>"` borrows:
203
+ * the argument's type, or why it borrows nothing.
204
+ */
205
+ export const borrowedType = (
206
+ like: string,
207
+ dependencies: ReadonlyMap<string, BlokScopeDependency>,
208
+ ): {typed: InferredType | null; problem: string | null} => {
209
+ const parts = like.split('.');
210
+ let dependency: string;
211
+ let action: string;
212
+ let argument: string;
213
+ if (parts[0] === 'self' && parts.length === 3) [dependency, action, argument] = ['self', parts[1], parts[2]];
214
+ else if (parts[0] === 'deps' && parts.length === 4) [dependency, action, argument] = [parts[1], parts[2], parts[3]];
215
+ else return {typed: null, problem: 'is no argument: write self.<action>.<arg> or deps.<key>.<action>.<arg>'};
216
+ const declared = dependencies.get(dependency);
217
+ if (!declared && dependency !== 'self') {
218
+ return {typed: null, problem: `names the dependency '${dependency}', which the mount does not have (${[...dependencies.keys()].sort().join(', ') || 'none'})`};
219
+ }
220
+ const actions = (declared?.action_dependencies ?? []).map(each => each.key);
221
+ if (!actions.includes(action)) {
222
+ return {typed: null, problem: `names the action '${action}', which ${dependency} does not have (${[...new Set(actions)].sort().join(', ') || 'none'})`};
223
+ }
224
+ const typed = argumentType(dependency, action, argument, dependencies);
225
+ if (typed === null) return {typed: null, problem: `names the argument '${argument}', which ${dependency}.${action} does not have, or does not type`};
226
+ return {typed, problem: null};
227
+ };
228
+
176
229
  class Uses {
230
+ /** Per local node id, the types of the task arguments it is passed to whole. */
177
231
  readonly args = new Map<string, InferredType[]>();
232
+ /** Per local node id, the bind kinds of the controls bound to it whole. */
178
233
  readonly binds = new Map<string, string[]>();
179
234
  readonly deps = new Set<string>();
180
235
 
181
- private readonly locals: ReadonlySet<string>;
182
236
  private readonly dependencies: ReadonlyMap<string, BlokScopeDependency>;
183
237
  private readonly view: BlokView | null;
184
238
 
185
- constructor(
186
- locals: ReadonlySet<string>,
187
- dependencies: ReadonlyMap<string, BlokScopeDependency>,
188
- view: BlokView | null,
189
- ) {
190
- this.locals = locals;
239
+ constructor(dependencies: ReadonlyMap<string, BlokScopeDependency>, view: BlokView | null) {
191
240
  this.dependencies = dependencies;
192
241
  this.view = view;
193
242
  }
@@ -198,69 +247,74 @@ class Uses {
198
247
  }
199
248
 
200
249
  walk(tree: BlokComponentNode): void {
201
- const scope = new Set<string>();
250
+ const scope = new Map<string, string | null>();
202
251
  const intoNames = (node: BlokComponentNode): void => {
203
- for (const prop of node.props ?? []) if (prop.agent_call && prop.declares_value) scope.add(prop.declares_value);
252
+ for (const prop of node.props ?? []) if (prop.agent_call && prop.declares_value) scope.set(prop.declares_value, null);
204
253
  node.children?.forEach(intoNames);
205
254
  };
206
255
  intoNames(tree);
207
256
  this.node(tree, scope);
208
257
  }
209
258
 
210
- private node(node: BlokComponentNode, scope: ReadonlySet<string>): void {
259
+ private node(node: BlokComponentNode, scope: UseScope): void {
211
260
  this.component(node, scope);
212
261
  const declared =
213
262
  node.component === FOREACH ? node.props?.find(prop => prop.key === FOREACH_LET && prop.declares_value)?.declares_value ?? null : null;
214
- const inner = declared !== null ? new Set([...scope, declared]) : scope;
263
+ const inner = new Map(scope);
264
+ if (declared !== null) inner.set(declared, null);
215
265
  for (const prop of node.props ?? []) this.prop(prop, declared !== null && prop.key === FOREACH_ITEMS ? scope : inner);
266
+ // Every local of a node is in scope in all of its children.
267
+ for (const child of node.children ?? []) {
268
+ const name = localDeclared(child);
269
+ if (name !== null) inner.set(name, child.id);
270
+ }
216
271
  for (const child of node.children ?? []) this.node(child, inner);
217
272
  }
218
273
 
219
- private component(node: BlokComponentNode, scope: ReadonlySet<string>): void {
274
+ private component(node: BlokComponentNode, scope: UseScope): void {
220
275
  const bind = this.view?.components.get(node.component)?.props.find(prop => prop.key === BIND_PROP);
221
276
  if (!bind) return;
222
277
  for (const prop of node.props ?? []) {
223
278
  const path = prop.key === BIND_PROP ? prop.dynamic_value?.path ?? null : null;
224
- if (path !== null && this.locals.has(path) && !scope.has(path)) {
225
- const binds = this.binds.get(path) ?? [];
279
+ const local = path !== null ? scope.get(path) ?? null : null;
280
+ if (local !== null) {
281
+ const binds = this.binds.get(local) ?? [];
226
282
  binds.push(bind.kind);
227
- this.binds.set(path, binds);
283
+ this.binds.set(local, binds);
228
284
  }
229
285
  }
230
286
  }
231
287
 
232
- private prop(prop: BlokComponentProp, scope: ReadonlySet<string>): void {
288
+ private prop(prop: BlokComponentProp, scope: UseScope): void {
233
289
  const path = prop.dynamic_value?.path;
234
290
  if (path != null) this.path(path, scope);
235
291
  if (prop.agent_call) this.agentCall(prop.agent_call, scope);
236
292
  if (prop.util_call) prop.util_call.arguments?.forEach(argument => this.argument(argument, scope));
237
293
  }
238
294
 
239
- private path(path: string, scope: ReadonlySet<string>): void {
295
+ private path(path: string, scope: UseScope): void {
240
296
  if (!scope.has(path.split('.')[0])) this.read(path);
241
297
  }
242
298
 
243
- private argument(argument: BlokActionArgument, scope: ReadonlySet<string>): void {
299
+ private argument(argument: BlokActionArgument, scope: UseScope): void {
244
300
  if (argument.value_path != null) this.path(argument.value_path, scope);
245
301
  if (argument.agent_call) this.agentCall(argument.agent_call, scope);
246
302
  if (argument.util_call) argument.util_call.arguments?.forEach(nested => this.argument(nested, scope));
247
303
  [...(argument.value_list ?? []), ...(argument.value_dict ?? [])].forEach(nested => this.argument(nested, scope));
248
304
  }
249
305
 
250
- private agentCall(call: BlokAgentCall, scope: ReadonlySet<string>): void {
306
+ private agentCall(call: BlokAgentCall, scope: UseScope): void {
251
307
  if (call.dependency !== 'self') this.deps.add(call.dependency);
252
308
  const action = actionKeyFor(call);
253
- const demand = this.dependencies.get(call.dependency)?.action_dependencies?.find(each => each.key === action)?.demand;
254
- const matches = new Map((demand?.arg_matches ?? []).flatMap(match => (match.key != null ? [[match.key, match] as const] : [])));
255
309
  for (const argument of call.arguments ?? []) {
256
310
  const path = argument.value_path;
257
- if (path != null && this.locals.has(path) && !scope.has(path) && argument.key != null) {
258
- const match = matches.get(argument.key);
259
- const typed = match ? matchType(match) : null;
311
+ const local = path != null ? scope.get(path) ?? null : null;
312
+ if (local !== null && argument.key != null) {
313
+ const typed = argumentType(call.dependency, action, argument.key, this.dependencies);
260
314
  if (typed !== null) {
261
- const found = this.args.get(path) ?? [];
315
+ const found = this.args.get(local) ?? [];
262
316
  found.push(typed);
263
- this.args.set(path, found);
317
+ this.args.set(local, found);
264
318
  }
265
319
  }
266
320
  this.argument(argument, scope);
@@ -268,41 +322,44 @@ class Uses {
268
322
  }
269
323
  }
270
324
 
271
- /** Every path a list of arguments reads (a query's variables). */
272
- const argumentPaths = (args: readonly BlokActionArgument[]): string[] => {
273
- const paths: string[] = [];
274
- const visit = (argument: BlokActionArgument): void => {
275
- if (argument.value_path != null) paths.push(argument.value_path);
276
- for (const call of [argument.util_call, argument.agent_call]) call?.arguments?.forEach(visit);
277
- [...(argument.value_list ?? []), ...(argument.value_dict ?? [])].forEach(visit);
278
- };
279
- args.forEach(visit);
280
- return paths;
281
- };
282
-
283
325
  /**
284
- * Every dependency key a tree (and its reads' variables) reads or calls,
285
- * `self` aside, sorted: what a host checks against the agents it bound
286
- * (one it did not bind reads as failed, `deps.unbound`).
326
+ * Every dependency key a tree reads or calls (its queries' variables, `<var>`
327
+ * nodes, included), `self` aside, sorted: what a host checks against the
328
+ * agents it bound (one it did not bind reads as failed, `deps.unbound`).
287
329
  */
288
- export const dependencyKeysRead = (roots: readonly BlokComponentNode[], variables: readonly BlokActionArgument[] = []): string[] => {
289
- const uses = new Uses(new Set(), new Map(), null);
330
+ export const dependencyKeysRead = (roots: readonly BlokComponentNode[]): string[] => {
331
+ const uses = new Uses(new Map(), null);
290
332
  roots.forEach(root => uses.walk(root));
291
- argumentPaths(variables).forEach(path => uses.read(path));
292
333
  return [...uses.deps].filter(key => key !== 'self').sort();
293
334
  };
294
335
 
336
+ /** A `<local>` with its kind resolved, and why its `like` borrows nothing (null when it borrows, has none, or the blok is not mounted). */
337
+ export type ResolvedLocal = {local: ResolvedBlokLocal; likeProblem: string | null};
338
+
295
339
  /**
296
- * `scope` with what `tree` tells: each local's kind (its dependencies stay as
297
- * declared). Resolving a resolved scope changes nothing.
340
+ * Every readable `<local>` of `tree` with its kind resolved (see the module),
341
+ * by node id, in tree order. `dependencies` null is a blok not mounted: a
342
+ * `like` then borrows nothing, and says nothing of it either.
298
343
  */
299
- export const resolveBlokScope = (tree: BlokComponentNode, scope: BlokCheckScope, view: BlokView | null): ResolvedBlokScope => {
300
- const uses = new Uses(
301
- new Set(scope.locals.map(local => local.key)),
302
- new Map(scope.dependencies.map(dependency => [dependency.key, dependency])),
303
- view,
304
- );
344
+ export const resolveLocals = (
345
+ tree: BlokComponentNode,
346
+ dependencies: readonly BlokScopeDependency[] | null,
347
+ view: BlokView | null,
348
+ ): Map<string, ResolvedLocal> => {
349
+ const byKey = new Map((dependencies ?? []).map(dependency => [dependency.key, dependency]));
350
+ const uses = new Uses(byKey, view);
305
351
  uses.walk(tree);
306
- const locals = scope.locals.map(local => resolveLocal(local, uses.args.get(local.key) ?? [], uses.binds.get(local.key) ?? []));
307
- return {...scope, locals};
352
+ const resolved = new Map<string, ResolvedLocal>();
353
+ for (const [node] of iterWithParents(tree)) {
354
+ if (node.component !== LOCAL_COMPONENT) continue;
355
+ const local = declaredLocal(node);
356
+ if (local === null) continue;
357
+ const like = localLike(node);
358
+ const borrowed = like !== null && dependencies !== null ? borrowedType(like, byKey) : {typed: null, problem: null};
359
+ resolved.set(node.id, {
360
+ local: resolveLocal(local as BlokScopeLocal, uses.args.get(node.id) ?? [], uses.binds.get(node.id) ?? [], borrowed.typed),
361
+ likeProblem: borrowed.problem,
362
+ });
363
+ }
364
+ return resolved;
308
365
  };
@@ -1,10 +1,11 @@
1
1
  import {describe, expect, it} from 'vitest';
2
2
  import {analyzeBsx} from './analyze';
3
3
  import {completeBsx, hoverBsx, scanCursor, signatureBsx, type BsxEnvironment} from './language';
4
- import {emptyBlokScope, type BlokCheckScope} from './references';
4
+ import {type BlokCheckScope} from './references';
5
5
 
6
+ /** Where the blok is mounted: its dependencies and args. What it is and owns is in its text. */
6
7
  const scope: BlokCheckScope = {
7
- ...emptyBlokScope('custom'),
8
+ kinds: [],
8
9
  dependencies: [
9
10
  {
10
11
  key: 'stage',
@@ -12,22 +13,25 @@ const scope: BlokCheckScope = {
12
13
  state_dependencies: [{key: 'position', demand: {matches: [{key: 'x', kind: 'FLOAT'}, {key: 'y', kind: 'FLOAT'}]}}],
13
14
  },
14
15
  ],
15
- locals: [
16
- {key: 'speed', kind: 'INT', default: 1},
17
- {key: 'form', kind: 'DICT', children: [{key: 'name', kind: 'STRING'}, {key: 'count', kind: 'INT'}]},
18
- ],
19
- queries: [
20
- {key: 'positions', service: 'mikro', document: 'query { positions { id } }'},
21
- {key: 'pages', service: 'mikro', document: 'query { pages { id } }', more: 'pages'},
22
- ],
23
16
  args: ['image'],
24
17
  };
25
18
 
26
19
  const env = (overrides: Partial<BlokCheckScope> = {}): BsxEnvironment => ({scope: {...scope, ...overrides}});
27
20
 
28
- /** Completions at `|` in the source. */
29
- const at = (marked: string, environment = env()) => completeBsx(marked.replace('|', ''), marked.indexOf('|'), environment);
30
- const labels = (marked: string, environment = env()) => at(marked, environment).map(item => item.label);
21
+ /** The `<Blok>` (a panel of `kind`) and its declarations, written before the text under test. */
22
+ const prelude = (kind: string): string =>
23
+ `<Blok kind="${kind}" context="panel">` +
24
+ '<local name="speed" kind="INT" default={1} />' +
25
+ '<local name="form" kind="DICT" children={[{key: "name", kind: "STRING"}, {key: "count", kind: "INT"}]} />' +
26
+ '<query name="positions" service="mikro" document="query { positions { id } }" />' +
27
+ '<query name="pages" service="mikro" document="query { pages { id } }" more="pages" />';
28
+
29
+ /** Completions at `|` in the source as it stands. */
30
+ const atRaw = (marked: string, environment = env()) => completeBsx(marked.replace('|', ''), marked.indexOf('|'), environment);
31
+ /** Completions at `|` in the source, inside a custom blok (or one of `kind`) that declares two locals and two queries. */
32
+ const at = (marked: string, environment = env(), kind = 'custom') => atRaw(`${prelude(kind)}${marked}`, environment);
33
+ const labels = (marked: string, environment = env(), kind = 'custom') => at(marked, environment, kind).map(item => item.label);
34
+ const rawLabels = (marked: string) => atRaw(marked).map(item => item.label);
31
35
 
32
36
  describe('scanCursor', () => {
33
37
  it('knows where the cursor is', () => {
@@ -67,11 +71,18 @@ describe('completeBsx: components and props', () => {
67
71
  expect(labels('<Card><|')).not.toContain('Model');
68
72
  expect(labels('<Scene><Model><|')).toContain('OnClick');
69
73
  expect(labels('<Scene><|')).not.toContain('OnClick');
70
- expect(labels('<|', env({context: 'SCENE'}))).toEqual(['Scene']);
71
- expect(labels('<|', env({kind: 'action', context: 'SCENE'}))).toEqual(['Scene', 'Space']);
72
- expect(labels('<|', env({context: 'LOGO'}))).toEqual(['AgentLogo']);
73
- expect(labels('<|', env({context: 'ACTION_LOGO'}))).toEqual(['ActionLogo']);
74
- expect(labels('<|', env({context: 'HTML'}))).toEqual(['Html']);
74
+ // A blok starts with its <Blok>; what its root holds is what its context takes.
75
+ expect(rawLabels('<|')).toEqual(['Blok']);
76
+ const scene = rawLabels('<Blok kind="agent" context="scene"><|');
77
+ expect(scene).toEqual(expect.arrayContaining(['Scene', 'local', 'query']));
78
+ expect(scene).not.toContain('Space');
79
+ expect(scene).not.toContain('Card');
80
+ expect(rawLabels('<Blok kind="action" context="scene"><|')).toEqual(expect.arrayContaining(['Scene', 'Space']));
81
+ expect(rawLabels('<Blok kind="agent" context="logo"><|')).toContain('Logo');
82
+ expect(rawLabels('<Blok kind="agent" context="logo"><|')).not.toContain('Card');
83
+ expect(rawLabels('<Blok kind="agent" context="panel"><|')).toEqual(expect.arrayContaining(['Card', 'local', 'query']));
84
+ // Queries are children of the <Blok>, nowhere deeper.
85
+ expect(rawLabels('<Blok kind="agent" context="panel"><Card><|')).not.toContain('query');
75
86
  });
76
87
 
77
88
  it('offers the props not yet set, required first, then id and when', () => {
@@ -101,7 +112,7 @@ describe('completeBsx: expressions', () => {
101
112
  expect(items).not.toContain('self');
102
113
  expect(items).not.toContain('task');
103
114
  expect(items).not.toContain('event');
104
- expect(labels('<Text text={|', env({kind: 'agent'}))).toContain('self');
115
+ expect(labels('<Text text={|', env(), 'agent')).toContain('self');
105
116
  });
106
117
 
107
118
  it('walks agents: states, tasks, assets, fields, and actions only in callbacks', () => {
@@ -146,7 +157,7 @@ describe('completeBsx: expressions', () => {
146
157
  expect(labels('<Text text={str.|')).toEqual(expect.arrayContaining(['upper', 'template', 'classes']));
147
158
  expect(labels('<Text text={|')).not.toContain('ui');
148
159
  expect(labels('<Button onClick={ui.|')).toEqual(['copy']);
149
- expect(labels('<Button onClick={task.|', env({kind: 'task'}))).toContain('answer');
160
+ expect(labels('<Button onClick={task.|', env(), 'task')).toContain('answer');
150
161
  });
151
162
 
152
163
  it('offers into after a task call', () => {
@@ -162,7 +173,7 @@ describe('hover and signatures', () => {
162
173
  const source = '<Text text={str.upper(deps.stage.id)} />';
163
174
  expect(hoverBsx(source, source.indexOf('upper') + 1, env())?.contents).toContain('str.upper(value)');
164
175
  expect(hoverBsx(source, source.indexOf('deps') + 1, env())?.contents).toContain('**deps**');
165
- expect(hoverBsx('<Time value={then} relative />', 2, env())?.contents).toContain('ui@1');
176
+ expect(hoverBsx('<Time value={then} relative />', 2, env())?.contents).toContain('base@4');
166
177
  const then = '<Time value={then} />';
167
178
  expect(hoverBsx(then, then.indexOf('then') + 1, env())?.contents).toContain('**then**');
168
179
  });
@@ -181,7 +192,7 @@ describe('analyzeBsx', () => {
181
192
  const analyze = (source: string, overrides: Partial<BlokCheckScope> = {}) => analyzeBsx(source, {scope: {...scope, ...overrides}});
182
193
 
183
194
  it('marks a finding at its prop, with its code', () => {
184
- const source = '<Html><Text text={deps.stage.speed} /></Html>';
195
+ const source = '<Blok kind="custom" context="panel"><Text text={deps.stage.speed} /></Blok>';
185
196
  const {markers} = analyze(source);
186
197
  expect(markers).toHaveLength(1);
187
198
  expect(markers[0]).toMatchObject({code: 'deps.unknown_state', severity: 'error'});
@@ -189,12 +200,13 @@ describe('analyzeBsx', () => {
189
200
  });
190
201
 
191
202
  it('marks warnings as warnings', () => {
192
- const {markers} = analyze('<Html><Badge className="bg-green-500" /></Html>');
203
+ const {markers} = analyze('<Blok kind="custom" context="panel"><Badge className="bg-green-500" /></Blok>');
193
204
  expect(markers).toMatchObject([{code: 'palette.raw_colour', severity: 'warning'}]);
194
205
  });
195
206
 
196
- it('marks a root the context does not take', () => {
197
- expect(analyze('<Card />', {context: 'SCENE'}).markers.map(marker => marker.code)).toContain('context.root');
207
+ it('marks a root that is no <Blok>, and a blok mounted where its kind does not go', () => {
208
+ expect(analyze('<Card />').markers.map(marker => marker.code)).toEqual(['blok.root']);
209
+ expect(analyze('<Blok kind="agent" context="panel"><Card /></Blok>', {kinds: ['action', 'assign']}).markers.map(marker => marker.code)).toEqual(['kind.placement']);
198
210
  });
199
211
 
200
212
  it('returns no tree for text that does not parse, and the refusal', () => {
@@ -1,7 +1,10 @@
1
1
  import {defaultBlokView, innerContext, type BlokView, type SpecComponent, type SpecProp} from '../spec/catalogs';
2
- import {loadVocabulary, type BlokContextName, type VocabFunction, type VocabRecordField} from '../spec/vocabulary';
2
+ import {BLOK_KINDS, kindIn, loadVocabulary, type BlokContextName, type BlokKind, type VocabFunction, type VocabRecordField} from '../spec/vocabulary';
3
+ import {BLOK_COMPONENT, declaredQueries, declaredSubscriptions, QUERY_COMPONENT, SUBSCRIPTION_COMPONENT} from './declared';
4
+ import {resolveLocals} from './infer';
3
5
  import {FOREACH_COMPONENT, FOREACH_LET_PROP, INTO_KEYWORD} from './parse';
4
- import {blokRootsOf, drawnIn, type BlokCheckScope, type BlokPortMatch, type BlokScopeDependency, type BlokScopeLocalPort} from './references';
6
+ import {parseBsxPartial} from './partial';
7
+ import {type BlokCheckScope, type BlokPortMatch, type BlokScopeDependency, type BlokScopeLocalPort, type ResolvedBlokLocal} from './references';
5
8
  import type {BsxRange} from './source';
6
9
 
7
10
  /**
@@ -12,8 +15,12 @@ import type {BsxRange} from './source';
12
15
  */
13
16
 
14
17
  export type BsxEnvironment = {
15
- /** What the blok declares beside its tree: its kind, context, dependencies, locals, queries, ... */
16
- scope: BlokCheckScope;
18
+ /**
19
+ * Where the blok is mounted (its dependencies, args, ...); absent: mounted
20
+ * nowhere. What the blok is (kind, context) and owns (locals, queries,
21
+ * subscriptions) is read off its text.
22
+ */
23
+ scope?: BlokCheckScope | null;
17
24
  /** The catalogs that draw it; the Orkestrator's when left out. */
18
25
  view?: BlokView;
19
26
  };
@@ -53,6 +60,29 @@ const isSpace = (char: string | undefined) => char !== undefined && /\s/.test(ch
53
60
 
54
61
  const viewOf = (env: BsxEnvironment): BlokView => env.view ?? defaultBlokView();
55
62
 
63
+ /** What the open `<Blok>` says of itself (its kind and context); an agent panel while it says nothing. */
64
+ const headerAt = (stack: readonly OpenElement[]): {kind: BlokKind; context: BlokContextName} => {
65
+ const root = stack[0]?.tag === BLOK_COMPONENT ? stack[0].attributes : null;
66
+ const unquote = (value: string | undefined) => (value ?? '').replace(/^["']|["']$/g, '');
67
+ const kind = unquote(root?.get('kind'));
68
+ const context = loadVocabulary().blok.contexts[unquote(root?.get('context'))];
69
+ return {kind: (BLOK_KINDS as readonly string[]).includes(kind) ? (kind as BlokKind) : 'agent', context: context ?? 'PANEL'};
70
+ };
71
+
72
+ const dependenciesOf = (env: BsxEnvironment): readonly BlokScopeDependency[] => env.scope?.dependencies ?? [];
73
+
74
+ /** What the text declares, as far as it parses: its locals (resolved), queries and subscriptions. */
75
+ const declaredIn = (env: BsxEnvironment, text: string) => {
76
+ const tree = parseBsxPartial(text, env.view).tree ?? null;
77
+ const locals = new Map<string, ResolvedBlokLocal>();
78
+ if (tree) {
79
+ for (const {local} of resolveLocals(tree, env.scope?.dependencies ?? null, viewOf(env)).values()) {
80
+ if (!locals.has(local.key)) locals.set(local.key, local);
81
+ }
82
+ }
83
+ return {locals, queries: declaredQueries(tree), subscriptions: declaredSubscriptions(tree)};
84
+ };
85
+
56
86
  /** The index of the `}` closing the brace at `start`, or -1 when it is not closed before `limit`. */
57
87
  const closingBrace = (text: string, start: number, limit: number): number => {
58
88
  let depth = 1;
@@ -246,10 +276,23 @@ const describeFunction = (fn: VocabFunction): string =>
246
276
 
247
277
  const contextAt = (env: BsxEnvironment, stack: readonly OpenElement[]): BlokContextName => {
248
278
  const view = viewOf(env);
249
- return stack.reduce<BlokContextName>(
250
- (context, element) => innerContext(view.components.get(element.tag), context),
251
- drawnIn(env.scope) as BlokContextName,
252
- );
279
+ // The <Blok> says the context its content is in.
280
+ const inner = stack[0]?.tag === BLOK_COMPONENT ? stack.slice(1) : stack;
281
+ return inner.reduce<BlokContextName>((context, element) => innerContext(view.components.get(element.tag), context), headerAt(stack).context);
282
+ };
283
+
284
+ /** What starts a blok: its `<Blok>`. */
285
+ const blokCompletion = (from: number): BsxCompletion => {
286
+ const vocabulary = loadVocabulary();
287
+ return {
288
+ label: vocabulary.blok.component,
289
+ insertText: `${vocabulary.blok.component} kind="\${1|${Object.keys(vocabulary.kinds).join(',')}|}" context="\${2|${Object.keys(vocabulary.blok.contexts).join(',')}|}">\n\t$0\n</${vocabulary.blok.component}>`,
290
+ snippet: true,
291
+ kind: 'component',
292
+ detail: 'the blok: its kind and context',
293
+ documentation: vocabulary.blok.attributes ? Object.entries(vocabulary.blok.attributes).map(([key, text]) => `**${key}**: ${text}`).join('\n\n') : undefined,
294
+ from,
295
+ };
253
296
  };
254
297
 
255
298
  const fitsUnder = (component: SpecComponent, ancestors: readonly string[], context: BlokContextName): boolean => {
@@ -287,19 +330,21 @@ const completeTag = (env: BsxEnvironment, cursor: Extract<BsxCursor, {kind: 'tag
287
330
  const view = viewOf(env);
288
331
  const {stack, from} = cursor;
289
332
  const components = [...view.components.values()];
290
- if (stack.length === 0) {
291
- const context = drawnIn(env.scope) as BlokContextName;
292
- // A Space places the run's dependencies: it starts only an action's own blok, which has a run.
293
- const roots = blokRootsOf(env.scope.context).filter(root => root !== 'Space' || env.scope.kind === 'action');
294
- const candidates = roots.length ? components.filter(component => roots.includes(component.name)) : components.filter(component => fitsUnder(component, [], context));
295
- return candidates.map(component => componentCompletion(component, from));
296
- }
333
+ if (stack.length === 0) return [blokCompletion(from)];
297
334
  const parent = view.components.get(stack[stack.length - 1].tag);
298
335
  if (parent && !parent.acceptsChildren) return [];
299
336
  const ancestors = stack.map(element => element.tag);
337
+ const header = headerAt(stack);
338
+ const atRoot = stack.length === 1 && stack[0].tag === BLOK_COMPONENT;
339
+ const declarations = new Set(Object.keys(loadVocabulary().blok.declarations));
340
+ // A scene or a logo holds one element of its own (a Space only in an action's blok, which has a run).
341
+ const content = atRoot ? loadVocabulary().blok.content[header.context]?.filter(name => name !== 'Space' || header.kind === 'action') : undefined;
300
342
  const context = contextAt(env, stack);
301
343
  return components
302
- .filter(component => fitsUnder(component, ancestors, context))
344
+ .filter(component => component.name !== BLOK_COMPONENT)
345
+ // Queries and subscriptions are children of the <Blok>.
346
+ .filter(component => atRoot || (component.name !== QUERY_COMPONENT && component.name !== SUBSCRIPTION_COMPONENT))
347
+ .filter(component => (content ? content.includes(component.name) || declarations.has(component.name) : fitsUnder(component, ancestors, context)))
303
348
  .map(component => ({
304
349
  ...componentCompletion(component, from),
305
350
  // What can only sit here (a part of its parent) comes first.
@@ -447,13 +492,15 @@ const foreachLets = (stack: readonly OpenElement[]): string[] =>
447
492
  .filter(Boolean);
448
493
 
449
494
  const dependencyOf = (env: BsxEnvironment, key: string): BlokScopeDependency | undefined =>
450
- env.scope.dependencies.find(dependency => dependency.key === key);
495
+ dependenciesOf(env).find(dependency => dependency.key === key);
451
496
 
452
497
  const completeExpression = (env: BsxEnvironment, text: string, expression: string, end: number, place: ExpressionPlace): BsxCompletion[] => {
453
498
  if (inString(expression)) return [];
454
499
  const vocabulary = loadVocabulary();
455
500
  const view = viewOf(env);
456
- const roots = vocabulary.kinds[env.scope.kind].roots;
501
+ const header = headerAt(place.stack);
502
+ const roots = kindIn(vocabulary.kinds[header.kind], header.context).roots;
503
+ const declared = declaredIn(env, text);
457
504
  const calls = place.callback !== null;
458
505
  const depth = (expression.replace(/"[^"]*"|'[^']*'/g, '').match(/\(/g) ?? []).length - (expression.replace(/"[^"]*"|'[^']*'/g, '').match(/\)/g) ?? []).length;
459
506
  // Only the callback's own one thing may be a call; arguments are values.
@@ -546,7 +593,7 @@ const completeExpression = (env: BsxEnvironment, text: string, expression: strin
546
593
  for (const root of roots) items.push(item(root, 'keyword', 'root', vocabulary.dataRoots[root]));
547
594
  if (calls) items.push(item('event', 'keyword', 'what the component hands its callback', vocabulary.dataRoots.event));
548
595
  if (place.inErrorSlot) items.push(item('error', 'keyword', 'the error that made the boundary show', vocabulary.scopedRoots.error));
549
- for (const local of env.scope.locals) items.push(item(local.key, 'path', 'local'));
596
+ for (const local of declared.locals.keys()) items.push(item(local, 'path', 'local'));
550
597
  for (const name of into) items.push(item(name, 'path', "a call's record"));
551
598
  for (const name of lets) items.push(item(name, 'path', 'foreach item'));
552
599
  for (const namespace of vocabulary.functionNamespaces) {
@@ -573,7 +620,7 @@ const completeExpression = (env: BsxEnvironment, text: string, expression: strin
573
620
  const action = dependency?.action_dependencies?.find(candidate => candidate.key === call?.[2]);
574
621
  return taskRecord(rest, action?.demand?.return_matches);
575
622
  }
576
- const local = env.scope.locals.find(candidate => candidate.key === first);
623
+ const local = declared.locals.get(first);
577
624
  if (local) {
578
625
  // A local is a port: complete the fields its port declares (a list's item is keyed `...`).
579
626
  let port: Pick<BlokScopeLocalPort, 'children'> | undefined = local;
@@ -595,10 +642,10 @@ const completeExpression = (env: BsxEnvironment, text: string, expression: strin
595
642
  case 'self':
596
643
  return agent(dependencyOf(env, 'self'), rest);
597
644
  case 'deps':
598
- if (rest.length === 0) return env.scope.dependencies.filter(dependency => dependency.key !== 'self').map(dependency => item(dependency.key, 'dependency', 'dependency'));
645
+ if (rest.length === 0) return dependenciesOf(env).filter(dependency => dependency.key !== 'self').map(dependency => item(dependency.key, 'dependency', 'dependency'));
599
646
  return agent(dependencyOf(env, rest[0]), rest.slice(1));
600
647
  case 'args':
601
- return rest.length === 0 ? (env.scope.args ?? []).map(arg => item(arg, 'path', 'arg')) : [];
648
+ return rest.length === 0 ? (env.scope?.args ?? []).map(arg => item(arg, 'path', 'arg')) : [];
602
649
  case 'task':
603
650
  return taskRecord(rest, null);
604
651
  case 'viewer': {
@@ -606,14 +653,14 @@ const completeExpression = (env: BsxEnvironment, text: string, expression: strin
606
653
  return fields ? fieldItems(fields, 'viewer') : [];
607
654
  }
608
655
  case 'queries':
609
- if (rest.length === 0) return env.scope.queries.map(query => item(query.key, 'path', `query · ${query.service ?? ''}`));
656
+ if (rest.length === 0) return declared.queries.map(query => item(query.key, 'path', `query · ${query.service ?? ''}`));
610
657
  if (rest.length === 1) {
611
658
  return [
612
659
  ...fieldItems(recordFields('query'), 'query record'),
613
660
  ...(topCall
614
661
  ? [
615
662
  item('refetch', 'function', 'read it again (stale-while-revalidate)', undefined, 'refetch()'),
616
- ...(env.scope.queries.find(query => query.key === rest[0])?.more
663
+ ...(declared.queries.find(query => query.key === rest[0])?.more
617
664
  ? [item('fetch_more', 'function', 'read the next page and append it', undefined, 'fetch_more($1)', true)]
618
665
  : []),
619
666
  ]
@@ -622,7 +669,7 @@ const completeExpression = (env: BsxEnvironment, text: string, expression: strin
622
669
  }
623
670
  return [];
624
671
  case 'subscriptions':
625
- if (rest.length === 0) return env.scope.subscriptions.map(subscription => item(subscription.key, 'path', `subscription · ${subscription.service ?? ''}`));
672
+ if (rest.length === 0) return declared.subscriptions.map(subscription => item(subscription.key, 'path', `subscription · ${subscription.service ?? ''}`));
626
673
  return rest.length === 1 ? fieldItems(recordFields('subscription'), 'subscription record') : [];
627
674
  default:
628
675
  return [];