@arkitektio/bsx 5.0.0 → 6.1.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 (230) hide show
  1. package/README.md +2 -2
  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 +6 -1
  23. package/dist/bsx/parse.d.ts.map +1 -1
  24. package/dist/bsx/parse.js +32 -3
  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 +3 -0
  28. package/dist/bsx/print.js.map +1 -1
  29. package/dist/bsx/references.d.ts +45 -51
  30. package/dist/bsx/references.d.ts.map +1 -1
  31. package/dist/bsx/references.js +265 -138
  32. package/dist/bsx/references.js.map +1 -1
  33. package/dist/catalogs/html.d.ts +2 -2
  34. package/dist/catalogs/index.d.ts +49 -71
  35. package/dist/catalogs/index.d.ts.map +1 -1
  36. package/dist/catalogs/index.js +1 -1
  37. package/dist/catalogs/logo.d.ts +14 -28
  38. package/dist/catalogs/logo.d.ts.map +1 -1
  39. package/dist/catalogs/logo.js +1 -2
  40. package/dist/catalogs/logo.js.map +1 -1
  41. package/dist/catalogs/parts/data.d.ts +1 -1
  42. package/dist/catalogs/parts/feedback.d.ts +3 -3
  43. package/dist/catalogs/parts/forms.d.ts +4 -4
  44. package/dist/catalogs/parts/html.d.ts +2 -2
  45. package/dist/catalogs/parts/icons.d.ts +2 -2
  46. package/dist/catalogs/parts/index.d.ts +35 -57
  47. package/dist/catalogs/parts/index.d.ts.map +1 -1
  48. package/dist/catalogs/parts/logo.d.ts +10 -25
  49. package/dist/catalogs/parts/logo.d.ts.map +1 -1
  50. package/dist/catalogs/parts/logo.js +12 -14
  51. package/dist/catalogs/parts/logo.js.map +1 -1
  52. package/dist/catalogs/parts/navigation.d.ts +4 -4
  53. package/dist/catalogs/parts/overlays.d.ts +11 -11
  54. package/dist/catalogs/parts/run.d.ts +2 -10
  55. package/dist/catalogs/parts/run.d.ts.map +1 -1
  56. package/dist/catalogs/parts/scene.d.ts +7 -15
  57. package/dist/catalogs/parts/scene.d.ts.map +1 -1
  58. package/dist/catalogs/parts/scene.js +1 -5
  59. package/dist/catalogs/parts/scene.js.map +1 -1
  60. package/dist/catalogs/parts/typography.d.ts +3 -3
  61. package/dist/catalogs/props.d.ts +2 -2
  62. package/dist/catalogs/scene.d.ts +5 -13
  63. package/dist/catalogs/scene.d.ts.map +1 -1
  64. package/dist/catalogs/shadcn.d.ts +11 -11
  65. package/dist/catalogs/ui.d.ts +16 -16
  66. package/dist/logo/actionLogoSource.d.ts +3 -3
  67. package/dist/logo/actionLogoSource.d.ts.map +1 -1
  68. package/dist/logo/actionLogoSource.js +4 -4
  69. package/dist/logo/actionLogoSource.js.map +1 -1
  70. package/dist/renderer/BlokRenderer.d.ts +16 -6
  71. package/dist/renderer/BlokRenderer.d.ts.map +1 -1
  72. package/dist/renderer/BlokRenderer.js +32 -13
  73. package/dist/renderer/BlokRenderer.js.map +1 -1
  74. package/dist/renderer/baseCatalog.d.ts +3 -3
  75. package/dist/renderer/baseCatalog.js +2 -2
  76. package/dist/renderer/bloks/control.d.ts +18 -2
  77. package/dist/renderer/bloks/control.d.ts.map +1 -1
  78. package/dist/renderer/bloks/control.js +85 -14
  79. package/dist/renderer/bloks/control.js.map +1 -1
  80. package/dist/renderer/bloks/data.d.ts +1 -1
  81. package/dist/renderer/bloks/feedback.d.ts +3 -3
  82. package/dist/renderer/bloks/forms.d.ts +4 -4
  83. package/dist/renderer/bloks/html.d.ts +2 -2
  84. package/dist/renderer/bloks/icons.d.ts +1 -1
  85. package/dist/renderer/bloks/index.d.ts +30 -37
  86. package/dist/renderer/bloks/index.d.ts.map +1 -1
  87. package/dist/renderer/bloks/logo/index.d.ts +1 -8
  88. package/dist/renderer/bloks/logo/index.d.ts.map +1 -1
  89. package/dist/renderer/bloks/logo/index.js +1 -2
  90. package/dist/renderer/bloks/logo/index.js.map +1 -1
  91. package/dist/renderer/bloks/logo/logo.fixture.json +266 -250
  92. package/dist/renderer/bloks/navigation.d.ts +4 -4
  93. package/dist/renderer/bloks/overlays.d.ts +11 -11
  94. package/dist/renderer/bloks/run/components.d.ts +1 -5
  95. package/dist/renderer/bloks/run/components.d.ts.map +1 -1
  96. package/dist/renderer/bloks/scene/Scene.d.ts.map +1 -1
  97. package/dist/renderer/bloks/scene/Scene.js +4 -2
  98. package/dist/renderer/bloks/scene/Scene.js.map +1 -1
  99. package/dist/renderer/bloks/scene/host.d.ts +2 -0
  100. package/dist/renderer/bloks/scene/host.d.ts.map +1 -1
  101. package/dist/renderer/bloks/scene/host.js +8 -0
  102. package/dist/renderer/bloks/scene/host.js.map +1 -1
  103. package/dist/renderer/bloks/scene/index.d.ts +4 -8
  104. package/dist/renderer/bloks/scene/index.d.ts.map +1 -1
  105. package/dist/renderer/bloks/scene/interaction.d.ts.map +1 -1
  106. package/dist/renderer/bloks/scene/interaction.js +3 -2
  107. package/dist/renderer/bloks/scene/interaction.js.map +1 -1
  108. package/dist/renderer/bloks/shared/props.d.ts +2 -2
  109. package/dist/renderer/bloks/typography.d.ts +3 -3
  110. package/dist/renderer/declarations.d.ts +40 -33
  111. package/dist/renderer/declarations.d.ts.map +1 -1
  112. package/dist/renderer/declarations.js +41 -56
  113. package/dist/renderer/declarations.js.map +1 -1
  114. package/dist/renderer/declare.js +4 -4
  115. package/dist/renderer/declare.js.map +1 -1
  116. package/dist/renderer/persist.d.ts +22 -13
  117. package/dist/renderer/persist.d.ts.map +1 -1
  118. package/dist/renderer/persist.js +40 -21
  119. package/dist/renderer/persist.js.map +1 -1
  120. package/dist/renderer/runtime/catalog.d.ts +4 -4
  121. package/dist/renderer/runtime/catalog.js +3 -3
  122. package/dist/renderer/runtime/components.d.ts +4 -4
  123. package/dist/renderer/runtime/components.d.ts.map +1 -1
  124. package/dist/renderer/runtime/components.js +5 -5
  125. package/dist/renderer/runtime/components.js.map +1 -1
  126. package/dist/renderer/runtime/context.d.ts +5 -5
  127. package/dist/renderer/runtime/context.d.ts.map +1 -1
  128. package/dist/renderer/runtime/context.js +11 -5
  129. package/dist/renderer/runtime/context.js.map +1 -1
  130. package/dist/renderer/runtime/describe.d.ts +3 -3
  131. package/dist/renderer/runtime/describe.d.ts.map +1 -1
  132. package/dist/renderer/runtime/describe.js +2 -2
  133. package/dist/renderer/runtime/describe.js.map +1 -1
  134. package/dist/renderer/runtime/hooks.d.ts.map +1 -1
  135. package/dist/renderer/runtime/hooks.js +8 -4
  136. package/dist/renderer/runtime/hooks.js.map +1 -1
  137. package/dist/renderer/runtime/index.d.ts +1 -0
  138. package/dist/renderer/runtime/index.d.ts.map +1 -1
  139. package/dist/renderer/runtime/index.js +1 -0
  140. package/dist/renderer/runtime/index.js.map +1 -1
  141. package/dist/renderer/runtime/locals.d.ts +57 -0
  142. package/dist/renderer/runtime/locals.d.ts.map +1 -0
  143. package/dist/renderer/runtime/locals.js +87 -0
  144. package/dist/renderer/runtime/locals.js.map +1 -0
  145. package/dist/renderer/runtime/scope.d.ts +14 -0
  146. package/dist/renderer/runtime/scope.d.ts.map +1 -1
  147. package/dist/renderer/runtime/scope.js +25 -3
  148. package/dist/renderer/runtime/scope.js.map +1 -1
  149. package/dist/renderer/runtime/tree.d.ts.map +1 -1
  150. package/dist/renderer/runtime/tree.js +7 -1
  151. package/dist/renderer/runtime/tree.js.map +1 -1
  152. package/dist/renderer/runtime/types.d.ts +8 -3
  153. package/dist/renderer/runtime/types.d.ts.map +1 -1
  154. package/dist/shadcn.d.ts +36 -43
  155. package/dist/shadcn.d.ts.map +1 -1
  156. package/dist/shadcn.js +1 -2
  157. package/dist/shadcn.js.map +1 -1
  158. package/dist/spec/bsx_v2.json +11067 -6321
  159. package/dist/spec/catalogs/{base@3.json → base@4.json} +434 -288
  160. package/dist/spec/catalogs/{base@3.json.js → base@4.json.js} +434 -288
  161. package/dist/spec/catalogs/orkestrator@1.json +10 -10
  162. package/dist/spec/catalogs/orkestrator@1.json.js +10 -10
  163. package/dist/spec/catalogs.d.ts +4 -4
  164. package/dist/spec/catalogs.d.ts.map +1 -1
  165. package/dist/spec/catalogs.js +7 -7
  166. package/dist/spec/catalogs.js.map +1 -1
  167. package/dist/spec/vocabulary.d.ts +25 -4
  168. package/dist/spec/vocabulary.d.ts.map +1 -1
  169. package/dist/spec/vocabulary.js +50 -5
  170. package/dist/spec/vocabulary.js.map +1 -1
  171. package/dist/spec/vocabulary.json +58 -32
  172. package/dist/spec/vocabulary.json.js +58 -32
  173. package/package.json +1 -1
  174. package/src/bsx/analyze.ts +14 -12
  175. package/src/bsx/bsx.test.ts +4 -4
  176. package/src/bsx/declared.ts +346 -0
  177. package/src/bsx/index.ts +1 -0
  178. package/src/bsx/infer.test.ts +42 -32
  179. package/src/bsx/infer.ts +128 -71
  180. package/src/bsx/language.test.ts +37 -25
  181. package/src/bsx/language.ts +72 -25
  182. package/src/bsx/parse.ts +34 -3
  183. package/src/bsx/print.ts +3 -0
  184. package/src/bsx/references.test.ts +82 -81
  185. package/src/bsx/references.ts +313 -178
  186. package/src/catalogs/catalogs.test.ts +3 -2
  187. package/src/catalogs/index.ts +1 -1
  188. package/src/catalogs/logo.ts +1 -2
  189. package/src/catalogs/parts/logo.ts +13 -17
  190. package/src/catalogs/parts/scene.ts +1 -7
  191. package/src/logo/actionLogoSource.ts +4 -4
  192. package/src/renderer/BlokComponentHost.test.tsx +5 -5
  193. package/src/renderer/BlokRenderer.test.tsx +22 -0
  194. package/src/renderer/BlokRenderer.tsx +70 -20
  195. package/src/renderer/addBlokComponents.test.tsx +1 -1
  196. package/src/renderer/baseCatalog.ts +2 -2
  197. package/src/renderer/bloks/control.tsx +119 -16
  198. package/src/renderer/bloks/logo/index.tsx +2 -3
  199. package/src/renderer/bloks/logo/logo.fixture.json +266 -250
  200. package/src/renderer/bloks/logo/logo.test.tsx +18 -7
  201. package/src/renderer/bloks/run/space.test.tsx +12 -4
  202. package/src/renderer/bloks/scene/Scene.tsx +4 -2
  203. package/src/renderer/bloks/scene/host.tsx +11 -0
  204. package/src/renderer/bloks/scene/interaction.tsx +11 -6
  205. package/src/renderer/bloks/scene/transform.test.ts +8 -7
  206. package/src/renderer/declarations.test.ts +38 -51
  207. package/src/renderer/declarations.ts +67 -88
  208. package/src/renderer/declare.test.ts +4 -4
  209. package/src/renderer/declare.ts +4 -4
  210. package/src/renderer/persist.test.ts +70 -49
  211. package/src/renderer/persist.ts +57 -34
  212. package/src/renderer/runtime/catalog.test.ts +2 -2
  213. package/src/renderer/runtime/catalog.ts +4 -4
  214. package/src/renderer/runtime/components.tsx +8 -8
  215. package/src/renderer/runtime/context.tsx +17 -10
  216. package/src/renderer/runtime/describe.ts +5 -5
  217. package/src/renderer/runtime/hooks.ts +6 -4
  218. package/src/renderer/runtime/index.ts +1 -0
  219. package/src/renderer/runtime/locals.tsx +105 -0
  220. package/src/renderer/runtime/scope.tsx +31 -3
  221. package/src/renderer/runtime/tree.tsx +16 -4
  222. package/src/renderer/runtime/types.ts +8 -3
  223. package/src/shadcn.ts +1 -2
  224. package/src/spec/bsx_v2.json +11067 -6321
  225. package/src/spec/catalogs/{base@3.json → base@4.json} +434 -288
  226. package/src/spec/catalogs/orkestrator@1.json +10 -10
  227. package/src/spec/catalogs.ts +8 -8
  228. package/src/spec/spec.test.ts +19 -16
  229. package/src/spec/vocabulary.json +58 -32
  230. package/src/spec/vocabulary.ts +69 -10
@@ -0,0 +1,346 @@
1
+ import type {BlokActionArgument, BlokComponentNode, BlokComponentProp} from '../renderer/runtime/types';
2
+ import {finding, loadVocabulary, type BlokContextName, type BlokFinding, type BlokKind} from '../spec/vocabulary';
3
+ import {portIsValid} from './infer';
4
+
5
+ /**
6
+ * What a blok says of itself in its tree: its `<Blok>` header and its
7
+ * declarations. The port of arkitekt-spec's `blok/declared.py`.
8
+ *
9
+ * Every blok is one `<Blok kind=... context=...>` element (vocabulary `blok`):
10
+ *
11
+ * - the header (`blokHeader`): its kind, its context, the catalog it is
12
+ * written for and the host's chrome, all literals on the root;
13
+ * - its declarations, each a node of its own that draws nothing: `<local>`
14
+ * anywhere (first among its parent's children, in scope in its parent's
15
+ * subtree), `<query>` and `<subscription>` as children of the `<Blok>`, each
16
+ * with `<var>` children for its variables.
17
+ *
18
+ * Nothing here checks a rule beyond what makes a declaration readable at all;
19
+ * the checker (`references.ts`) says the rest.
20
+ */
21
+
22
+ export const BLOK_COMPONENT = 'Blok';
23
+ export const LOCAL_COMPONENT = 'local';
24
+ export const QUERY_COMPONENT = 'query';
25
+ export const SUBSCRIPTION_COMPONENT = 'subscription';
26
+ export const VAR_COMPONENT = 'var';
27
+ /** The components that declare what a blok owns and draw nothing. */
28
+ export const DECLARATION_COMPONENTS: ReadonlySet<string> = new Set([LOCAL_COMPONENT, QUERY_COMPONENT, SUBSCRIPTION_COMPONENT, VAR_COMPONENT]);
29
+ /** The name a local, a query or a subscription is read by. */
30
+ export const NAME_PROP = 'name';
31
+ /** A local's borrowed port: `self.<action>.<arg>` or `deps.<key>.<action>.<arg>`. */
32
+ export const LIKE_PROP = 'like';
33
+ const KEY_PROP = 'key';
34
+ const VALUE_PROP = 'value';
35
+ export const KIND_ATTRIBUTE = 'kind';
36
+ export const CONTEXT_ATTRIBUTE = 'context';
37
+ export const CATALOG_ATTRIBUTE = 'catalog';
38
+ export const CHROME_ATTRIBUTE = 'chrome';
39
+
40
+ /** What a `<local>` may say beside its name and `like`: a port's fields and `persist` (`BlokLocalInput` but its key). */
41
+ export const LOCAL_FIELDS: readonly string[] = [
42
+ 'children',
43
+ 'choices',
44
+ 'default',
45
+ 'description',
46
+ 'dimension',
47
+ 'effects',
48
+ 'identifier',
49
+ 'kind',
50
+ 'label',
51
+ 'nullable',
52
+ 'persist',
53
+ 'proposed_units',
54
+ 'reference_unit',
55
+ 'requires',
56
+ 'validators',
57
+ 'widget',
58
+ ];
59
+
60
+ const PORT_KINDS: ReadonlySet<string> = new Set([
61
+ 'INT',
62
+ 'STRING',
63
+ 'STRUCTURE',
64
+ 'LIST',
65
+ 'BOOL',
66
+ 'DICT',
67
+ 'FLOAT',
68
+ 'DATE',
69
+ 'UNION',
70
+ 'ENUM',
71
+ 'MODEL',
72
+ 'MEMORY_STRUCTURE',
73
+ 'INTERFACE',
74
+ 'QUANTITY',
75
+ ]);
76
+ const LOCAL_ANY = 'ANY';
77
+
78
+ /** What a `<Blok>` says of itself. */
79
+ export type BlokHeader = {
80
+ kind: BlokKind;
81
+ /** `PANEL`, `SCENE` or `LOGO`. */
82
+ context: BlokContextName;
83
+ catalog: string | null;
84
+ chrome: string | null;
85
+ /** The root's id. */
86
+ node: string;
87
+ };
88
+
89
+ const staticProp = (node: BlokComponentNode, key: string): BlokComponentProp | undefined => node.props?.find(prop => prop.key === key);
90
+
91
+ const isLiteral = (prop: BlokComponentProp): boolean => prop.dynamic_value == null && prop.util_call == null && prop.agent_call == null;
92
+
93
+ const literalText = (node: BlokComponentNode, key: string): string | null => {
94
+ const prop = staticProp(node, key);
95
+ return prop !== undefined && isLiteral(prop) && typeof prop.static_value === 'string' ? prop.static_value : null;
96
+ };
97
+
98
+ /** Why `tree` is no readable `<Blok>`, or null: `blok.root` or `blok.attribute`. */
99
+ export const headerProblem = (tree: BlokComponentNode): BlokFinding | null => {
100
+ const vocabulary = loadVocabulary();
101
+ if (tree.component !== vocabulary.blok.component) return finding('blok.root', {component: tree.component}, {nodeId: tree.id});
102
+ const kinds = Object.keys(vocabulary.kinds).join('|');
103
+ const problem = (text: string) => finding('blok.attribute', {problem: text, kinds}, {nodeId: tree.id});
104
+ for (const key of [KIND_ATTRIBUTE, CONTEXT_ATTRIBUTE]) {
105
+ const prop = staticProp(tree, key);
106
+ if (prop === undefined) return problem(`needs ${key}="..."`);
107
+ if (!isLiteral(prop) || typeof prop.static_value !== 'string') return problem(`takes ${key} as a literal string`);
108
+ }
109
+ const kind = literalText(tree, KIND_ATTRIBUTE) ?? '';
110
+ if (!(kind in vocabulary.kinds)) return problem(`has no kind '${kind}'`);
111
+ const context = literalText(tree, CONTEXT_ATTRIBUTE) ?? '';
112
+ if (!(context in vocabulary.blok.contexts)) {
113
+ return problem(`has no context '${context}' (${Object.keys(vocabulary.blok.contexts).join(', ')})`);
114
+ }
115
+ for (const key of [CATALOG_ATTRIBUTE, CHROME_ATTRIBUTE]) {
116
+ const prop = staticProp(tree, key);
117
+ if (prop !== undefined && (!isLiteral(prop) || typeof prop.static_value !== 'string')) return problem(`takes ${key} as a literal string`);
118
+ }
119
+ return null;
120
+ };
121
+
122
+ /** The header of a blok, or null when its root is no readable `<Blok>` (`headerProblem` says why). */
123
+ export const blokHeader = (tree: BlokComponentNode): BlokHeader | null => {
124
+ if (headerProblem(tree) !== null) return null;
125
+ const vocabulary = loadVocabulary();
126
+ return {
127
+ kind: literalText(tree, KIND_ATTRIBUTE) as BlokKind,
128
+ context: vocabulary.blok.contexts[literalText(tree, CONTEXT_ATTRIBUTE) ?? ''],
129
+ catalog: literalText(tree, CATALOG_ATTRIBUTE),
130
+ chrome: literalText(tree, CHROME_ATTRIBUTE),
131
+ node: tree.id,
132
+ };
133
+ };
134
+
135
+ /** The catalog a blok's `<Blok>` names; null for the base catalog (and for no `<Blok>`). */
136
+ export const blokCatalog = (tree: BlokComponentNode): string | null =>
137
+ tree.component === BLOK_COMPONENT ? literalText(tree, CATALOG_ATTRIBUTE) : null;
138
+
139
+ /** Whether `node` declares something (a local, a query, ...) rather than drawing. */
140
+ export const isDeclaration = (node: {component: string}): boolean => DECLARATION_COMPONENTS.has(node.component);
141
+
142
+ /** The children of `node` that draw: its declarations left out. */
143
+ export const content = (node: BlokComponentNode): BlokComponentNode[] => (node.children ?? []).filter(child => !isDeclaration(child));
144
+
145
+ /** The name a `<local>` declares, or null when it has none. */
146
+ export const localName = (node: BlokComponentNode): string | null => {
147
+ const value = staticProp(node, NAME_PROP)?.static_value;
148
+ return typeof value === 'string' ? value : null;
149
+ };
150
+
151
+ /** The argument a `<local>` borrows its port from (`self.move.to`), or null. */
152
+ export const localLike = (node: BlokComponentNode): string | null => {
153
+ const value = staticProp(node, LIKE_PROP)?.static_value;
154
+ return typeof value === 'string' ? value : null;
155
+ };
156
+
157
+ /** A local as written in its `<local>` node: a port (`BlokLocalInput`), its kind left out unless it gives one. */
158
+ export type DeclaredLocal = {
159
+ key: string;
160
+ kind?: string | null;
161
+ persist?: boolean | null;
162
+ [field: string]: unknown;
163
+ };
164
+
165
+ const isRecord = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null && !Array.isArray(value);
166
+
167
+ /** Why the fields of a local make no valid port, or null. */
168
+ const localFieldsProblem = (fields: Record<string, unknown>): string | null => {
169
+ const kind = fields.kind;
170
+ if (kind !== undefined && (typeof kind !== 'string' || (kind !== LOCAL_ANY && !PORT_KINDS.has(kind)))) return `kind: '${String(kind)}' is no port kind`;
171
+ for (const key of ['label', 'description', 'identifier', 'reference_unit', 'dimension']) {
172
+ if (fields[key] !== undefined && typeof fields[key] !== 'string') return `${key}: a string`;
173
+ }
174
+ for (const key of ['nullable', 'persist']) {
175
+ if (fields[key] !== undefined && typeof fields[key] !== 'boolean') return `${key}: a bool`;
176
+ }
177
+ for (const key of ['children', 'choices', 'effects', 'validators', 'requires', 'proposed_units']) {
178
+ if (fields[key] !== undefined && !Array.isArray(fields[key])) return `${key}: a list`;
179
+ }
180
+ if (fields.widget !== undefined && !isRecord(fields.widget)) return 'widget: an object';
181
+ if (fields.kind !== undefined && fields.kind !== LOCAL_ANY && !portIsValid(fields as Parameters<typeof portIsValid>[0])) return 'its port is incomplete for its kind';
182
+ return null;
183
+ };
184
+
185
+ const localFields = (node: BlokComponentNode, name: string): Record<string, unknown> => {
186
+ const fields: Record<string, unknown> = {key: name};
187
+ for (const prop of node.props ?? []) {
188
+ if (prop.key === NAME_PROP || prop.key === LIKE_PROP || !LOCAL_FIELDS.includes(prop.key)) continue;
189
+ if (prop.static_value !== undefined && prop.static_value !== null) fields[prop.key] = prop.static_value;
190
+ }
191
+ return fields;
192
+ };
193
+
194
+ /** Why a `<local>` declares nothing readable, or null: `local.attribute`. */
195
+ export const localProblem = (node: BlokComponentNode): BlokFinding | null => {
196
+ const declared = localName(node);
197
+ const name = declared ?? '?';
198
+ const problem = (key: string, text: string) => finding('local.attribute', {name, key, problem: text}, {nodeId: node.id});
199
+ if (declared === null) return problem('name', 'is missing: write name="x"');
200
+ for (const prop of node.props ?? []) {
201
+ if (prop.key === NAME_PROP || prop.key === LIKE_PROP) continue;
202
+ if (!LOCAL_FIELDS.includes(prop.key)) return problem(prop.key, `is no field of a local (${[...LOCAL_FIELDS].sort().join(', ')}, like)`);
203
+ if (!isLiteral(prop)) return problem(prop.key, 'is not a literal');
204
+ }
205
+ if (node.children?.length) return problem('children', 'are not elements: a local holds nothing');
206
+ const wrong = localFieldsProblem(localFields(node, declared));
207
+ if (wrong !== null) return problem('its port', `is no valid port (${wrong})`);
208
+ return null;
209
+ };
210
+
211
+ /** A readable `<local>` as the port it declares (`localProblem` null), its kind left out unless it gives one. */
212
+ export const declaredLocal = (node: BlokComponentNode): DeclaredLocal | null => {
213
+ const name = localName(node);
214
+ if (name === null || localProblem(node) !== null) return null;
215
+ return localFields(node, name) as DeclaredLocal;
216
+ };
217
+
218
+ /** Every node of `tree` with its ancestors, depth first. */
219
+ export const iterWithParents = (tree: BlokComponentNode): Array<[BlokComponentNode, readonly BlokComponentNode[]]> => {
220
+ const found: Array<[BlokComponentNode, readonly BlokComponentNode[]]> = [];
221
+ const visit = (node: BlokComponentNode, ancestors: readonly BlokComponentNode[]) => {
222
+ found.push([node, ancestors]);
223
+ for (const child of node.children ?? []) visit(child, [...ancestors, node]);
224
+ };
225
+ visit(tree, []);
226
+ return found;
227
+ };
228
+
229
+ /** Every readable `<local>` of `tree`, by its node id, in tree order. */
230
+ export const declaredLocals = (tree: BlokComponentNode): Map<string, DeclaredLocal> => {
231
+ const found = new Map<string, DeclaredLocal>();
232
+ for (const [node] of iterWithParents(tree)) {
233
+ if (node.component !== LOCAL_COMPONENT) continue;
234
+ const local = declaredLocal(node);
235
+ if (local !== null) found.set(node.id, local);
236
+ }
237
+ return found;
238
+ };
239
+
240
+ /** A `<var key value>` as the argument it is stored as, or null when it is none. */
241
+ export const varArgument = (node: BlokComponentNode): BlokActionArgument | null => {
242
+ const key = staticProp(node, KEY_PROP);
243
+ if (key === undefined || typeof key.static_value !== 'string') return null;
244
+ const value = staticProp(node, VALUE_PROP);
245
+ if (value === undefined || (value.static_value == null && value.dynamic_value == null && value.util_call == null && value.agent_call == null)) {
246
+ return {key: key.static_value};
247
+ }
248
+ if (value.agent_call != null) return null;
249
+ if (value.dynamic_value?.path != null) return {key: key.static_value, value_path: value.dynamic_value.path};
250
+ if (value.util_call != null) return {key: key.static_value, util_call: value.util_call};
251
+ return {key: key.static_value, value_literal: value.static_value};
252
+ };
253
+
254
+ /** A declared read as the wire's `BlokQueryInput` spells it. */
255
+ export type DeclaredQuery = {
256
+ key: string;
257
+ service: string;
258
+ document: string;
259
+ variables: BlokActionArgument[];
260
+ poll?: number | null;
261
+ more?: string | null;
262
+ };
263
+
264
+ /** A declared stream as the wire's `BlokSubscriptionInput` spells it. */
265
+ export type DeclaredSubscription = {
266
+ key: string;
267
+ service: string;
268
+ document: string;
269
+ variables: BlokActionArgument[];
270
+ keep?: number | null;
271
+ history?: string | null;
272
+ };
273
+
274
+ const READ_FIELDS: Readonly<Record<string, readonly string[]>> = {
275
+ [QUERY_COMPONENT]: ['poll', 'more'],
276
+ [SUBSCRIPTION_COMPONENT]: ['keep', 'history'],
277
+ };
278
+ const REQUIRED_READ_PROPS = [NAME_PROP, 'service', 'document'];
279
+
280
+ const readFieldProblem = (component: string, key: string, value: unknown): string | null => {
281
+ if (!(READ_FIELDS[component] ?? []).includes(key)) return `${key}: Extra inputs are not permitted`;
282
+ if (key === 'poll' && !(typeof value === 'number' && value > 0)) return 'poll: Input should be a number greater than 0';
283
+ if (key === 'keep' && !(typeof value === 'number' && Number.isInteger(value) && value >= 1)) return 'keep: Input should be an integer of at least 1';
284
+ if ((key === 'more' || key === 'history') && typeof value !== 'string') return `${key}: Input should be a valid string`;
285
+ return null;
286
+ };
287
+
288
+ /** Why a `<query>` or `<subscription>` declares nothing readable, or null: `query.declaration`. */
289
+ export const declarationProblem = (node: BlokComponentNode): BlokFinding | null => {
290
+ const problem = (text: string, at: string = node.id) => finding('query.declaration', {component: node.component, problem: text}, {nodeId: at});
291
+ for (const key of REQUIRED_READ_PROPS) {
292
+ const prop = staticProp(node, key);
293
+ if (prop === undefined || !isLiteral(prop) || typeof prop.static_value !== 'string' || !prop.static_value) {
294
+ return problem(`needs ${key}="..." as a literal string`);
295
+ }
296
+ }
297
+ for (const prop of node.props ?? []) {
298
+ if (!REQUIRED_READ_PROPS.includes(prop.key) && !isLiteral(prop)) return problem(`takes ${prop.key} as a literal`);
299
+ }
300
+ for (const child of node.children ?? []) {
301
+ if (child.component !== VAR_COMPONENT) return problem(`holds only <var> children, not <${child.component}>`, child.id);
302
+ if (varArgument(child) === null) return problem('has a <var> without key="..." or with a task call as its value', child.id);
303
+ }
304
+ for (const prop of node.props ?? []) {
305
+ if (REQUIRED_READ_PROPS.includes(prop.key) || prop.static_value === undefined || prop.static_value === null) continue;
306
+ const wrong = readFieldProblem(node.component, prop.key, prop.static_value);
307
+ if (wrong !== null) return problem(wrong);
308
+ }
309
+ return null;
310
+ };
311
+
312
+ const declaredRead = (node: BlokComponentNode): Record<string, unknown> => {
313
+ const fields: Record<string, unknown> = {};
314
+ for (const prop of node.props ?? []) {
315
+ if (prop.static_value !== undefined && prop.static_value !== null) fields[prop.key === NAME_PROP ? 'key' : prop.key] = prop.static_value;
316
+ }
317
+ fields.variables = (node.children ?? []).flatMap(child => {
318
+ const argument = varArgument(child);
319
+ return argument ? [argument] : [];
320
+ });
321
+ return fields;
322
+ };
323
+
324
+ const rootChildren = (tree: BlokComponentNode | null | undefined): BlokComponentNode[] => tree?.children ?? [];
325
+
326
+ /** The readable `<query>` children of a blok's `<Blok>`, in order. */
327
+ export const declaredQueries = (tree: BlokComponentNode | null | undefined): DeclaredQuery[] =>
328
+ rootChildren(tree)
329
+ .filter(child => child.component === QUERY_COMPONENT && declarationProblem(child) === null)
330
+ .map(child => declaredRead(child) as DeclaredQuery);
331
+
332
+ /** The readable `<subscription>` children of a blok's `<Blok>`, in order. */
333
+ export const declaredSubscriptions = (tree: BlokComponentNode | null | undefined): DeclaredSubscription[] =>
334
+ rootChildren(tree)
335
+ .filter(child => child.component === SUBSCRIPTION_COMPONENT && declarationProblem(child) === null)
336
+ .map(child => declaredRead(child) as DeclaredSubscription);
337
+
338
+ /**
339
+ * The runtime id of a node rendered inside foreach items: its node id plus one
340
+ * `@<item key>` per enclosing foreach, outermost first (`Blok[0]/foreach[0]/local[0]@7`,
341
+ * `...@#2` for an item keyed by its position). A node id never holds `@`.
342
+ */
343
+ export const runtimeIdOf = (nodeId: string, itemKeys: readonly string[]): string => [nodeId, ...itemKeys].join('@');
344
+
345
+ /** The node a runtime id is of (`Blok[0]/foreach[0]/local[0]@7` → `Blok[0]/foreach[0]/local[0]`). */
346
+ export const nodeOfRuntimeId = (runtimeId: string): string => runtimeId.split('@')[0];
package/src/bsx/index.ts CHANGED
@@ -6,3 +6,4 @@ export * from './references';
6
6
  export * from './source';
7
7
  export * from './infer';
8
8
  export * from './partial';
9
+ export * from './declared';
@@ -1,13 +1,16 @@
1
1
  import {describe, expect, it} from 'vitest';
2
2
  import {blokView, defaultBlokView, parseSpecCatalog} from '../spec/catalogs';
3
- import {defaultType, portIsValid, resolveBlokScope, resolveLocal} from './infer';
3
+ import {defaultType, portIsValid, resolveLocal, resolveLocals} from './infer';
4
4
  import {parseBsx} from './parse';
5
5
  import {printBsx} from './print';
6
- import {checkBlokTree, emptyBlokScope, literalHint, type BlokCheckScope} from './references';
6
+ import {checkBlokTree, literalHint, mountedBlokScope, type BlokCheckScope, type BlokScopeDependency} from './references';
7
7
 
8
- const codes = (source: string, scope: BlokCheckScope): string[] => checkBlokTree(parseBsx(source).tree, scope).map(found => found.code);
8
+ const codes = (source: string, scope: BlokCheckScope = mountedBlokScope()): string[] => checkBlokTree(parseBsx(source).tree, scope).map(found => found.code);
9
9
 
10
- const moveTo: BlokCheckScope['dependencies'][number] = {
10
+ /** A panel blok of `kind` around `body`. */
11
+ const blok = (kind: string, body: string) => `<Blok kind="${kind}" context="panel">${body}</Blok>`;
12
+
13
+ const moveTo: BlokScopeDependency = {
11
14
  key: 'self',
12
15
  action_dependencies: [
13
16
  {
@@ -35,14 +38,22 @@ describe('local kind inference', () => {
35
38
  });
36
39
 
37
40
  it('follows the precedence default, then task argument, then bind', () => {
38
- const scope = (local: BlokCheckScope['locals'][number]): BlokCheckScope => ({...emptyBlokScope('agent'), dependencies: [moveTo], locals: [local]});
39
- const resolved = (source: string, local: BlokCheckScope['locals'][number]) =>
40
- resolveBlokScope(parseBsx(source).tree, scope(local), defaultBlokView()).locals[0];
41
- const both = '<Html><Card><Slider bind={p} /><Button onClick={self.move_to(position: p)} /></Card></Html>';
42
- expect(resolved(both, {key: 'p'}).kind).toBe('INT');
43
- expect(resolved(both, {key: 'p', default: 'x'}).kind).toBe('STRING');
44
- expect(resolved('<Html><Slider bind={p} /></Html>', {key: 'p'}).kind).toBe('FLOAT');
45
- expect(resolved('<Html><Text text={p} /></Html>', {key: 'p'}).kind).toBe('ANY');
41
+ const resolved = (body: string, local: string) =>
42
+ [...resolveLocals(parseBsx(blok('agent', `${local}${body}`)).tree, [moveTo], defaultBlokView()).values()][0].local;
43
+ const both = '<Card><Slider bind={p} /><Button onClick={self.move_to(position: p)} /></Card>';
44
+ expect(resolved(both, '<local name="p" />').kind).toBe('INT');
45
+ expect(resolved(both, '<local name="p" default="x" />').kind).toBe('STRING');
46
+ expect(resolved('<Slider bind={p} />', '<local name="p" />').kind).toBe('FLOAT');
47
+ expect(resolved('<Text text={p} />', '<local name="p" />').kind).toBe('ANY');
48
+ });
49
+
50
+ it('counts only the uses inside the local\'s scope', () => {
51
+ const tree = parseBsx(blok('agent', '<Card><local name="p" /><Text text={p} /></Card><Card><local name="p" /><Slider bind={p} /></Card>')).tree;
52
+ const kinds = [...resolveLocals(tree, [moveTo], defaultBlokView())].map(([node, {local}]) => [node, local.kind]);
53
+ expect(kinds).toEqual([
54
+ ['Blok[0]/Card[0]/local[0]', 'ANY'],
55
+ ['Blok[0]/Card[1]/local[0]', 'FLOAT'],
56
+ ]);
46
57
  });
47
58
 
48
59
  it('infers only a kind left out: a given one, ANY included, is kept', () => {
@@ -58,20 +69,20 @@ describe('local kind inference', () => {
58
69
  });
59
70
 
60
71
  it('lets an ANY local skip the argument, bind and default checks', () => {
61
- const scope: BlokCheckScope = {...emptyBlokScope('agent'), dependencies: [moveTo], locals: [{key: 'p', kind: 'ANY', default: 'x'}]};
62
- expect(codes('<Html><Card><Switch bind={p} /><Button onClick={self.move_to(position: p)} /><Text text={p.a.b} /></Card></Html>', scope)).toEqual([]);
72
+ const scope = mountedBlokScope([], [moveTo]);
73
+ expect(
74
+ codes(blok('agent', '<local name="p" kind="ANY" default="x" /><Card><Switch bind={p} /><Button onClick={self.move_to(position: p)} /><Text text={p.a.b} /></Card>'), scope),
75
+ ).toEqual([]);
63
76
  });
64
77
 
65
78
  it('checks the remaining sites against the kind found', () => {
66
- const scope: BlokCheckScope = {...emptyBlokScope('agent'), dependencies: [moveTo], locals: [{key: 'p'}]};
67
- expect(codes('<Html><Card><Switch bind={p} /><Button onClick={self.move_to(position: p)} /></Card></Html>', scope)).toEqual(['local.bind_kind']);
79
+ const scope = mountedBlokScope([], [moveTo]);
80
+ expect(codes(blok('agent', '<local name="p" /><Card><Switch bind={p} /><Button onClick={self.move_to(position: p)} /></Card>'), scope)).toEqual(['local.bind_kind']);
68
81
  });
69
82
 
70
83
  it('never infers a dependency: a custom blok reading an undeclared one is refused', () => {
71
- const tree = parseBsx('<Html><Card><Text text={deps.zeta.x} /><Button onClick={deps.alpha.go()} /><Text text={deps.mid.y} /></Card></Html>').tree;
72
- const scope: BlokCheckScope = {...emptyBlokScope('custom'), dependencies: [{key: 'mid', state_dependencies: []}]};
73
- expect(resolveBlokScope(tree, scope, null).dependencies).toBe(scope.dependencies);
74
- const found = checkBlokTree(tree, scope, null);
84
+ const tree = parseBsx(blok('custom', '<Card><Text text={deps.zeta.x} /><Button onClick={deps.alpha.go()} /><Text text={deps.mid.y} /></Card>')).tree;
85
+ const found = checkBlokTree(tree, mountedBlokScope([], [{key: 'mid', state_dependencies: []}]), null);
75
86
  expect(found.map(finding => finding.code)).toEqual(['deps.unknown', 'deps.unknown', 'deps.unknown_state']);
76
87
  expect(found[0].message).toContain('Fix: dependencies=[AgentDependencyInput(key="zeta")].');
77
88
  expect(found[1].message).toContain('Fix: dependencies=[AgentDependencyInput(key="alpha")].');
@@ -80,15 +91,14 @@ describe('local kind inference', () => {
80
91
 
81
92
  describe('new warnings', () => {
82
93
  it('warns a keyless foreach over objects, not over scalars', () => {
83
- const scope = emptyBlokScope('agent');
84
- expect(codes('<Html><foreach items={[{a: 1}]} let="x"><Text>{x.a}</Text></foreach></Html>', scope)).toEqual(['foreach.position_key']);
85
- expect(codes('<Html><foreach items={[1, 2]} let="x"><Text>{x}</Text></foreach></Html>', scope)).toEqual([]);
86
- expect(codes('<Html><foreach items={args.xs} let="x"><Text>{x}</Text></foreach></Html>', scope)).toEqual([]);
94
+ expect(codes(blok('agent', '<foreach items={[{a: 1}]} let="x"><Text>{x.a}</Text></foreach>'))).toEqual(['foreach.position_key']);
95
+ expect(codes(blok('agent', '<foreach items={[1, 2]} let="x"><Text>{x}</Text></foreach>'))).toEqual([]);
96
+ expect(codes(blok('agent', '<foreach items={args.xs} let="x"><Text>{x}</Text></foreach>'))).toEqual([]);
87
97
  });
88
98
 
89
99
  it('warns reading an unnamed return', () => {
90
- expect(codes('<Html><Text text={task.returns.return0} /></Html>', emptyBlokScope('task'))).toEqual(['returns.unnamed']);
91
- expect(codes('<Html><Text text={task.returns.image} /></Html>', emptyBlokScope('task'))).toEqual([]);
100
+ expect(codes(blok('task', '<Text text={task.returns.return0} />'))).toEqual(['returns.unnamed']);
101
+ expect(codes(blok('task', '<Text text={task.returns.image} />'))).toEqual([]);
92
102
  });
93
103
 
94
104
  it('hints the literal a string meant', () => {
@@ -106,17 +116,17 @@ describe('new warnings', () => {
106
116
 
107
117
  describe('a catalog only adds', () => {
108
118
  it('refuses one that redefines a component of the base catalog, the language included', () => {
109
- const redefining = (name: string) => parseSpecCatalog({name: 'mine', components: [{name, context: 'HTML', props: [{key: 'bind', kind: 'STRING'}]}]}, 'mine');
110
- expect(() => blokView(redefining('Slider'))).toThrow(/redefines <Slider> of base@3: a catalog only adds/);
119
+ const redefining = (name: string) => parseSpecCatalog({name: 'mine', components: [{name, context: 'PANEL', props: [{key: 'bind', kind: 'STRING'}]}]}, 'mine');
120
+ expect(() => blokView(redefining('Slider'))).toThrow(/redefines <Slider> of base@4: a catalog only adds/);
111
121
  expect(() => blokView(redefining('foreach'))).toThrow(/redefines <foreach>/);
112
122
  });
113
123
 
114
124
  it('draws what it adds on top of the base', () => {
115
- const view = blokView(parseSpecCatalog({name: 'mine', components: [{name: 'Gauge', context: 'HTML', props: []}]}, 'mine'));
125
+ const view = blokView(parseSpecCatalog({name: 'mine', components: [{name: 'Gauge', context: 'PANEL', props: []}]}, 'mine'));
116
126
  expect(view.components.get('Gauge')?.catalog).toBe('mine');
117
- expect(view.components.get('Slider')?.catalog).toBe('base@3');
118
- expect(view.label).toBe('base@3 + mine');
119
- const found = checkBlokTree(parseBsx('<Html><Card><Gauge /></Card></Html>').tree, emptyBlokScope('agent'), view);
127
+ expect(view.components.get('Slider')?.catalog).toBe('base@4');
128
+ expect(view.label).toBe('base@4 + mine');
129
+ const found = checkBlokTree(parseBsx(blok('agent', '<Card><Gauge /></Card>')).tree, mountedBlokScope(), view);
120
130
  expect(found).toEqual([]);
121
131
  });
122
132
  });