@arkitektio/bsx 5.0.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 (220) 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 +10 -6
  71. package/dist/renderer/BlokRenderer.d.ts.map +1 -1
  72. package/dist/renderer/BlokRenderer.js +23 -9
  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/index.d.ts +4 -8
  97. package/dist/renderer/bloks/scene/index.d.ts.map +1 -1
  98. package/dist/renderer/bloks/scene/interaction.d.ts.map +1 -1
  99. package/dist/renderer/bloks/scene/interaction.js +3 -2
  100. package/dist/renderer/bloks/scene/interaction.js.map +1 -1
  101. package/dist/renderer/bloks/shared/props.d.ts +2 -2
  102. package/dist/renderer/bloks/typography.d.ts +3 -3
  103. package/dist/renderer/declarations.d.ts +40 -33
  104. package/dist/renderer/declarations.d.ts.map +1 -1
  105. package/dist/renderer/declarations.js +41 -56
  106. package/dist/renderer/declarations.js.map +1 -1
  107. package/dist/renderer/declare.js +4 -4
  108. package/dist/renderer/declare.js.map +1 -1
  109. package/dist/renderer/persist.d.ts +22 -13
  110. package/dist/renderer/persist.d.ts.map +1 -1
  111. package/dist/renderer/persist.js +40 -21
  112. package/dist/renderer/persist.js.map +1 -1
  113. package/dist/renderer/runtime/catalog.d.ts +4 -4
  114. package/dist/renderer/runtime/catalog.js +3 -3
  115. package/dist/renderer/runtime/components.d.ts +4 -4
  116. package/dist/renderer/runtime/components.d.ts.map +1 -1
  117. package/dist/renderer/runtime/components.js +5 -5
  118. package/dist/renderer/runtime/components.js.map +1 -1
  119. package/dist/renderer/runtime/context.d.ts +5 -5
  120. package/dist/renderer/runtime/context.d.ts.map +1 -1
  121. package/dist/renderer/runtime/context.js +11 -5
  122. package/dist/renderer/runtime/context.js.map +1 -1
  123. package/dist/renderer/runtime/describe.d.ts +3 -3
  124. package/dist/renderer/runtime/describe.d.ts.map +1 -1
  125. package/dist/renderer/runtime/describe.js +2 -2
  126. package/dist/renderer/runtime/describe.js.map +1 -1
  127. package/dist/renderer/runtime/hooks.d.ts.map +1 -1
  128. package/dist/renderer/runtime/hooks.js +8 -4
  129. package/dist/renderer/runtime/hooks.js.map +1 -1
  130. package/dist/renderer/runtime/index.d.ts +1 -0
  131. package/dist/renderer/runtime/index.d.ts.map +1 -1
  132. package/dist/renderer/runtime/index.js +1 -0
  133. package/dist/renderer/runtime/index.js.map +1 -1
  134. package/dist/renderer/runtime/locals.d.ts +57 -0
  135. package/dist/renderer/runtime/locals.d.ts.map +1 -0
  136. package/dist/renderer/runtime/locals.js +87 -0
  137. package/dist/renderer/runtime/locals.js.map +1 -0
  138. package/dist/renderer/runtime/scope.d.ts +14 -0
  139. package/dist/renderer/runtime/scope.d.ts.map +1 -1
  140. package/dist/renderer/runtime/scope.js +25 -3
  141. package/dist/renderer/runtime/scope.js.map +1 -1
  142. package/dist/renderer/runtime/tree.d.ts.map +1 -1
  143. package/dist/renderer/runtime/tree.js +7 -1
  144. package/dist/renderer/runtime/tree.js.map +1 -1
  145. package/dist/renderer/runtime/types.d.ts +8 -3
  146. package/dist/renderer/runtime/types.d.ts.map +1 -1
  147. package/dist/shadcn.d.ts +36 -43
  148. package/dist/shadcn.d.ts.map +1 -1
  149. package/dist/shadcn.js +1 -2
  150. package/dist/shadcn.js.map +1 -1
  151. package/dist/spec/bsx_v2.json +11067 -6321
  152. package/dist/spec/catalogs/{base@3.json → base@4.json} +434 -288
  153. package/dist/spec/catalogs/{base@3.json.js → base@4.json.js} +434 -288
  154. package/dist/spec/catalogs/orkestrator@1.json +10 -10
  155. package/dist/spec/catalogs/orkestrator@1.json.js +10 -10
  156. package/dist/spec/catalogs.d.ts +4 -4
  157. package/dist/spec/catalogs.d.ts.map +1 -1
  158. package/dist/spec/catalogs.js +7 -7
  159. package/dist/spec/catalogs.js.map +1 -1
  160. package/dist/spec/vocabulary.d.ts +25 -4
  161. package/dist/spec/vocabulary.d.ts.map +1 -1
  162. package/dist/spec/vocabulary.js +50 -5
  163. package/dist/spec/vocabulary.js.map +1 -1
  164. package/dist/spec/vocabulary.json +58 -32
  165. package/dist/spec/vocabulary.json.js +58 -32
  166. package/package.json +1 -1
  167. package/src/bsx/analyze.ts +14 -12
  168. package/src/bsx/bsx.test.ts +4 -4
  169. package/src/bsx/declared.ts +346 -0
  170. package/src/bsx/index.ts +1 -0
  171. package/src/bsx/infer.test.ts +42 -32
  172. package/src/bsx/infer.ts +128 -71
  173. package/src/bsx/language.test.ts +37 -25
  174. package/src/bsx/language.ts +72 -25
  175. package/src/bsx/parse.ts +34 -3
  176. package/src/bsx/print.ts +3 -0
  177. package/src/bsx/references.test.ts +82 -81
  178. package/src/bsx/references.ts +313 -178
  179. package/src/catalogs/catalogs.test.ts +3 -2
  180. package/src/catalogs/index.ts +1 -1
  181. package/src/catalogs/logo.ts +1 -2
  182. package/src/catalogs/parts/logo.ts +13 -17
  183. package/src/catalogs/parts/scene.ts +1 -7
  184. package/src/logo/actionLogoSource.ts +4 -4
  185. package/src/renderer/BlokComponentHost.test.tsx +5 -5
  186. package/src/renderer/BlokRenderer.tsx +46 -14
  187. package/src/renderer/addBlokComponents.test.tsx +1 -1
  188. package/src/renderer/baseCatalog.ts +2 -2
  189. package/src/renderer/bloks/control.tsx +119 -16
  190. package/src/renderer/bloks/logo/index.tsx +2 -3
  191. package/src/renderer/bloks/logo/logo.fixture.json +266 -250
  192. package/src/renderer/bloks/logo/logo.test.tsx +18 -7
  193. package/src/renderer/bloks/run/space.test.tsx +12 -4
  194. package/src/renderer/bloks/scene/interaction.tsx +11 -6
  195. package/src/renderer/bloks/scene/transform.test.ts +8 -7
  196. package/src/renderer/declarations.test.ts +38 -51
  197. package/src/renderer/declarations.ts +67 -88
  198. package/src/renderer/declare.test.ts +4 -4
  199. package/src/renderer/declare.ts +4 -4
  200. package/src/renderer/persist.test.ts +70 -49
  201. package/src/renderer/persist.ts +57 -34
  202. package/src/renderer/runtime/catalog.test.ts +2 -2
  203. package/src/renderer/runtime/catalog.ts +4 -4
  204. package/src/renderer/runtime/components.tsx +8 -8
  205. package/src/renderer/runtime/context.tsx +17 -10
  206. package/src/renderer/runtime/describe.ts +5 -5
  207. package/src/renderer/runtime/hooks.ts +6 -4
  208. package/src/renderer/runtime/index.ts +1 -0
  209. package/src/renderer/runtime/locals.tsx +105 -0
  210. package/src/renderer/runtime/scope.tsx +31 -3
  211. package/src/renderer/runtime/tree.tsx +16 -4
  212. package/src/renderer/runtime/types.ts +8 -3
  213. package/src/shadcn.ts +1 -2
  214. package/src/spec/bsx_v2.json +11067 -6321
  215. package/src/spec/catalogs/{base@3.json → base@4.json} +434 -288
  216. package/src/spec/catalogs/orkestrator@1.json +10 -10
  217. package/src/spec/catalogs.ts +8 -8
  218. package/src/spec/spec.test.ts +19 -16
  219. package/src/spec/vocabulary.json +58 -32
  220. package/src/spec/vocabulary.ts +69 -10
package/src/bsx/parse.ts CHANGED
@@ -5,7 +5,7 @@ import type {
5
5
  BlokComponentProp,
6
6
  BlokUtilCall,
7
7
  } from '../renderer/runtime/types';
8
- import {defaultBlokView, type BlokView} from '../spec/catalogs';
8
+ import {blokViewFor, defaultBlokView, type BlokView} from '../spec/catalogs';
9
9
  import {isExplicitId, loadVocabulary, type BlokContextName} from '../spec/vocabulary';
10
10
  import {
11
11
  blokError,
@@ -62,6 +62,9 @@ export const FOREACH_COMPONENT = 'foreach';
62
62
  export const FOREACH_LET_PROP = 'let';
63
63
  export const FOREACH_ITEMS_PROP = 'items';
64
64
  export const FOREACH_KEY_PROP = 'key';
65
+ const LOCAL_COMPONENT = 'local';
66
+ const LOCAL_NAME_PROP = 'name';
67
+ const CATALOG_ATTRIBUTE = 'catalog';
65
68
  export const INTO_KEYWORD = 'into';
66
69
  export const EVENT_ROOT = 'event';
67
70
  export const REFETCH_FUNCTION = 'query.refetch';
@@ -538,6 +541,17 @@ class Lowering {
538
541
 
539
542
  private prop(component: string, attribute: MarkupAttribute): BlokComponentProp {
540
543
  const value = attribute.value;
544
+ if (component === LOCAL_COMPONENT && attribute.name === LOCAL_NAME_PROP) {
545
+ // A local declares its name, as a foreach's let does.
546
+ if (typeof value !== 'string' || !isLocalName(value)) {
547
+ throw this.fail('local.attribute', attribute.offset, {
548
+ name: typeof value === 'string' ? value : '{...}',
549
+ key: LOCAL_NAME_PROP,
550
+ problem: 'is not a name: write name="x", a plain identifier',
551
+ });
552
+ }
553
+ return {key: attribute.name, static_value: value, declares_value: value};
554
+ }
541
555
  if (value === true) return {key: attribute.name, static_value: true};
542
556
  if (typeof value === 'string') {
543
557
  if (attribute.name === FOREACH_LET_PROP && component === FOREACH_COMPONENT) {
@@ -775,14 +789,31 @@ class Lowering {
775
789
  const utf8Length = (source: string): number => new TextEncoder().encode(source).length;
776
790
 
777
791
  /** Parse a v2 BSX source into the tree a blok stores, and where each node is. Throws a `BsxSyntaxError`. */
778
- export const parseBsx = (source: string, view: BlokView = defaultBlokView()): {tree: BlokComponentNode; sources: BsxSourceMap} => {
792
+ /** The view of the catalog a source's `<Blok catalog=...>` names: the app's own when it names none (`blokViewFor`). */
793
+ const rootView = (root: MarkupElement): BlokView => {
794
+ const catalog = root.attributes.find(attribute => attribute.name === CATALOG_ATTRIBUTE)?.value;
795
+ return typeof catalog === 'string' ? blokViewFor(catalog) : defaultBlokView();
796
+ };
797
+
798
+ /** The catalog a BSX source's `<Blok catalog=...>` names, read off its markup alone; null for none. Throws a `BsxSyntaxError`. */
799
+ export const sourceCatalog = (source: string): string | null => {
800
+ const locator = new Locator(source);
801
+ const catalog = new Markup(source, locator).document().attributes.find(attribute => attribute.name === CATALOG_ATTRIBUTE)?.value;
802
+ return typeof catalog === 'string' ? catalog : null;
803
+ };
804
+
805
+ /**
806
+ * Parse a v2 BSX source into the tree a blok stores, and where each node is.
807
+ * Without a view, the one of the catalog its `<Blok catalog>` names. Throws a `BsxSyntaxError`.
808
+ */
809
+ export const parseBsx = (source: string, view?: BlokView): {tree: BlokComponentNode; sources: BsxSourceMap} => {
779
810
  const locator = new Locator(source);
780
811
  if (utf8Length(source) > MAX_SOURCE_BYTES) {
781
812
  throw blokError(locator, 'bsx.syntax', 0, {detail: `a blok's source is at most ${MAX_SOURCE_BYTES} bytes`});
782
813
  }
783
814
  const markup = new Markup(source, locator);
784
815
  const root = markup.document();
785
- const lowering = new Lowering(view, locator, markup);
816
+ const lowering = new Lowering(view ?? rootView(root), locator, markup);
786
817
  const tree = lowering.node(root, `${root.name}[0]`, null);
787
818
  return {tree, sources: lowering.sources};
788
819
  };
package/src/bsx/print.ts CHANGED
@@ -256,6 +256,9 @@ class Printer {
256
256
  if (node.component === FOREACH_COMPONENT && key === FOREACH_LET_PROP && prop.static_value === prop.declares_value) {
257
257
  return `${key}=${quoted(prop.declares_value)}`;
258
258
  }
259
+ if (node.component === 'local' && key === 'name' && prop.static_value === prop.declares_value) {
260
+ return `${key}=${quoted(prop.declares_value)}`;
261
+ }
259
262
  return refuse(`<${node.component}> (${node.id}): ${key} declares '${prop.declares_value}', which BSX cannot spell`);
260
263
  }
261
264
  if (prop.util_call) {
@@ -1,28 +1,32 @@
1
1
  import {describe, expect, it} from 'vitest';
2
2
  import corpus from '../spec/bsx_v2.json';
3
- import {baseBlokView, blokView, type BlokView} from '../spec/catalogs';
4
- import {resolveBlokScope} from './infer';
3
+ import {baseBlokView, blokViewFor, type BlokView} from '../spec/catalogs';
4
+ import {blokCatalog} from './declared';
5
+ import {resolveLocals} from './infer';
5
6
  import {parseBsx} from './parse';
6
- import {checkBlokTree, dependencyHint, emptyBlokScope, type BlokCheckScope} from './references';
7
+ import {checkBlokTree, dependencyHint, mountedBlokScope, type BlokCheckScope} from './references';
8
+ import type {BlokComponentNode} from '../renderer/runtime/types';
7
9
 
8
- type CorpusScope = BlokCheckScope & {catalog?: boolean};
10
+ /** Where a corpus case is checked: the spec's `mount`, and whether a catalog is read at all. */
11
+ type CorpusMount = Omit<BlokCheckScope, 'catalog'> & {catalog: boolean};
12
+
13
+ const scopeOf = (mount: CorpusMount): BlokCheckScope => ({kinds: mount.kinds, dependencies: mount.dependencies, args: mount.args});
9
14
  type CheckedCase = {
10
15
  name: string;
11
16
  source: string;
12
- scope: CorpusScope;
13
- /** The locals after `resolve_scope`, each kinded: nothing else is resolved. */
14
- resolved: {locals: unknown[]};
17
+ mount: CorpusMount;
18
+ /** Each `<local>` after `resolve_locals`, by node id, kinded: nothing else is resolved. */
19
+ resolved: {locals: Record<string, unknown>};
15
20
  codes: string[];
16
21
  };
17
22
 
18
- /** The view a case is checked with: none (the language alone), the base catalog's, or that of the catalog it is written for. */
19
- const caseView = (scope: CorpusScope): BlokView | null => {
20
- if (scope.catalog === false) return null;
21
- return scope.target == null ? baseBlokView() : blokView(scope.target);
23
+ /** The view a case is checked with: none (the language alone), else that of the catalog its <Blok> names (the base's for none). */
24
+ const caseView = (mount: CorpusMount, tree: BlokComponentNode): BlokView | null => {
25
+ if (mount.catalog === false) return null;
26
+ const catalog = blokCatalog(tree);
27
+ return catalog === null ? baseBlokView() : blokViewFor(catalog);
22
28
  };
23
29
 
24
- const caseScope = (scope: CorpusScope): BlokCheckScope => scope;
25
-
26
30
  /** The wire spells "nothing" as null or leaves it out (as bsx.test.ts compares trees). */
27
31
  const withoutNulls = (value: unknown): unknown => {
28
32
  if (Array.isArray(value)) return value.map(withoutNulls);
@@ -32,7 +36,6 @@ const withoutNulls = (value: unknown): unknown => {
32
36
  return value;
33
37
  };
34
38
 
35
- /** The fields the spec's models dump at their defaults; the resolver leaves them out. */
36
39
  const PYDANTIC_DEFAULTS: Readonly<Record<string, unknown>> = {
37
40
  optional: false,
38
41
  auto_resolvable: false,
@@ -58,7 +61,7 @@ const withoutDefaults = (value: unknown): unknown => {
58
61
  };
59
62
 
60
63
  const checked = (corpus.cases as ReadonlyArray<Record<string, unknown>>).filter(
61
- (entry): entry is CheckedCase & Record<string, unknown> => 'scope' in entry && 'codes' in entry,
64
+ (entry): entry is CheckedCase & Record<string, unknown> => 'mount' in entry && 'codes' in entry,
62
65
  );
63
66
 
64
67
  describe('the validator against the corpus', () => {
@@ -68,8 +71,7 @@ describe('the validator against the corpus', () => {
68
71
 
69
72
  it.each(checked.map(entry => [entry.name, entry] as const))('%s', (_name, entry) => {
70
73
  const {tree} = parseBsx(entry.source);
71
- const view = caseView(entry.scope);
72
- const findings = checkBlokTree(tree, caseScope(entry.scope), view);
74
+ const findings = checkBlokTree(tree, scopeOf(entry.mount), caseView(entry.mount, tree));
73
75
  expect(findings.map(found => found.code).sort()).toEqual([...entry.codes].sort());
74
76
  });
75
77
  });
@@ -77,50 +79,48 @@ describe('the validator against the corpus', () => {
77
79
  describe('inference against the corpus', () => {
78
80
  it.each(checked.map(entry => [entry.name, entry] as const))('%s', (_name, entry) => {
79
81
  const {tree} = parseBsx(entry.source);
80
- const resolved = resolveBlokScope(tree, caseScope(entry.scope), caseView(entry.scope));
82
+ const resolved = resolveLocals(tree, entry.mount.dependencies, caseView(entry.mount, tree));
81
83
  expect(Object.keys(entry.resolved)).toEqual(['locals']);
82
- expect(withoutDefaults({locals: resolved.locals})).toEqual(withoutDefaults(entry.resolved));
83
- // Dependencies are never inferred: they stay as declared.
84
- expect(resolved.dependencies).toBe(caseScope(entry.scope).dependencies);
85
- });
86
-
87
- it('is idempotent', () => {
88
- for (const entry of checked) {
89
- const {tree} = parseBsx(entry.source);
90
- const once = resolveBlokScope(tree, caseScope(entry.scope), null);
91
- const twice = resolveBlokScope(tree, once, null);
92
- expect(twice).toEqual(once);
93
- }
84
+ const locals = Object.fromEntries([...resolved].map(([node, {local}]) => [node, local]));
85
+ expect(withoutDefaults(locals)).toEqual(withoutDefaults(entry.resolved.locals));
94
86
  });
95
87
  });
96
88
 
89
+ /** A panel blok of `kind` around `body`. */
90
+ const blok = (kind: string, body: string, attributes = '') => `<Blok kind="${kind}" context="panel"${attributes}>${body}</Blok>`;
91
+ /** Mounted with no dependencies: every one it reads is undeclared. */
92
+ const mounted = mountedBlokScope();
93
+
97
94
  describe('findings', () => {
98
95
  it('name the node and prop they are about, with a filled message', () => {
99
- const {tree} = parseBsx('<Html><Card><Text text={deps.nope.x} /></Card></Html>');
100
- const [found] = checkBlokTree(tree, emptyBlokScope('custom'));
101
- expect(found).toMatchObject({code: 'deps.unknown', level: 'error', nodeId: 'Html[0]/Card[0]/Text[0]', propKey: 'text'});
96
+ const {tree} = parseBsx(blok('custom', '<Card><Text text={deps.nope.x} /></Card>'));
97
+ const [found] = checkBlokTree(tree, mounted);
98
+ expect(found).toMatchObject({code: 'deps.unknown', level: 'error', nodeId: 'Blok[0]/Card[0]/Text[0]', propKey: 'text'});
102
99
  expect(found.message).toBe(
103
100
  'deps.nope.x: \'nope\' is not a dependency of the blok (its dependencies: none). Fix: dependencies=[AgentDependencyInput(key="nope")].',
104
101
  );
105
102
  });
106
103
 
107
104
  it('hint the declaration a custom blok misses, for a read, a call and a query variable', () => {
108
- const {tree} = parseBsx('<Html><Card><Text text={queries.positions.status} /><Button onClick={deps.stage.move(x: 1)} /></Card></Html>');
109
- const scope: BlokCheckScope = {
110
- ...emptyBlokScope('custom'),
111
- dependencies: [{key: 'robot'}],
112
- queries: [{key: 'positions', service: 'mikro', document: 'query { positions { id } }', variables: [{key: 'agent', value_path: 'deps.laser.id'}]}],
113
- };
114
- const found = checkBlokTree(tree, scope).filter(finding => finding.code === 'deps.unknown');
105
+ const {tree} = parseBsx(
106
+ blok(
107
+ 'custom',
108
+ '<query name="positions" service="mikro" document="query { positions { id } }"><var key="agent" value={deps.laser.id} /></query><Card><Text text={queries.positions.status} /><Button onClick={deps.stage.move(x: 1)} /></Card>',
109
+ ),
110
+ );
111
+ const found = checkBlokTree(tree, mountedBlokScope([], [{key: 'robot'}])).filter(finding => finding.code === 'deps.unknown');
115
112
  expect(found.map(finding => finding.message).sort()).toEqual([
116
- 'onClick of <Button> (Html[0]/Card[0]/Button[0]): \'stage\' is not a dependency of the blok (its dependencies: robot). Fix: dependencies=[AgentDependencyInput(key="stage")].',
113
+ 'onClick of <Button> (Blok[0]/Card[0]/Button[0]): \'stage\' is not a dependency of the blok (its dependencies: robot). Fix: dependencies=[AgentDependencyInput(key="stage")].',
117
114
  'deps.laser.id: \'laser\' is not a dependency of the blok (its dependencies: robot). Fix: dependencies=[AgentDependencyInput(key="laser")].',
118
115
  ].sort());
119
116
  });
120
117
 
118
+ it('take what the mount provides as written where the blok is mounted nowhere', () => {
119
+ expect(checkBlokTree(parseBsx(blok('custom', '<Text text={deps.nope.x} /><Button onClick={deps.stage.move(x: 1)} />')).tree)).toEqual([]);
120
+ });
121
+
121
122
  it('hint per kind what a blok reading an undeclared dependency does about it, as the spec does', () => {
122
- const {tree} = parseBsx('<Html><Text text={deps.laser.power} /></Html>');
123
- const message = (kind: BlokCheckScope['kind']): string => checkBlokTree(tree, emptyBlokScope(kind))[0]?.message ?? '';
123
+ const message = (kind: string): string => checkBlokTree(parseBsx(blok(kind, '<Text text={deps.laser.power} />')).tree, mounted)[0]?.message ?? '';
124
124
  expect(message('custom')).toContain('Fix: dependencies=[AgentDependencyInput(key="laser")].');
125
125
  expect(message('task')).toContain('Fix: dependencies=[AgentDependencyInput(key="laser")].');
126
126
  expect(message('action')).toContain('Fix: add a dependency parameter `laser` to the action.');
@@ -130,93 +130,94 @@ describe('findings', () => {
130
130
 
131
131
  it('scope a let to its loop and an into name to the whole tree', () => {
132
132
  const {tree} = parseBsx(
133
- '<Html><Card><foreach items={args.xs} let="p" key={p.id}><Button onClick={self.go(to: p.id) into run} /></foreach><Text text={run.status} /></Card></Html>',
133
+ blok('agent', '<Card><foreach items={args.xs} let="p" key={p.id}><Button onClick={self.go(to: p.id) into run} /></foreach><Text text={run.status} /></Card>'),
134
134
  );
135
- expect(checkBlokTree(tree, emptyBlokScope('agent'))).toEqual([]);
135
+ expect(checkBlokTree(tree, mounted)).toEqual([]);
136
+ });
137
+
138
+ it('scope a local to its parent\'s subtree', () => {
139
+ const {tree} = parseBsx(blok('agent', '<Card><local name="open" default={false} /><Switch bind={open} /></Card><Switch bind={open} />'));
140
+ // Outside its scope the name is neither a local to write nor a root to read (as the spec says).
141
+ expect(checkBlokTree(tree, mounted).map(found => [found.code, found.nodeId])).toEqual([['local.unknown', 'Blok[0]/Switch[0]'], ['roots.unknown', 'Blok[0]/Switch[0]']]);
136
142
  });
137
143
 
138
144
  it('fill the bind and argument kind messages', () => {
139
- const {tree} = parseBsx('<Html><Card><Switch bind={count} /><Button onClick={self.go(to: count)} /></Card></Html>');
140
- const scope: BlokCheckScope = {
141
- ...emptyBlokScope('agent'),
142
- dependencies: [{key: 'self', action_dependencies: [{key: 'go', demand: {arg_matches: [{key: 'to', kind: 'BOOL'}]}}]}],
143
- locals: [{key: 'count', kind: 'INT', default: 1}],
144
- };
145
+ const {tree} = parseBsx(blok('agent', '<local name="count" kind="INT" default={1} /><Card><Switch bind={count} /><Button onClick={self.go(to: count)} /></Card>'));
146
+ const scope = mountedBlokScope([], [{key: 'self', action_dependencies: [{key: 'go', demand: {arg_matches: [{key: 'to', kind: 'BOOL'}]}}]}]);
145
147
  const findings = checkBlokTree(tree, scope);
146
148
  expect(findings.map(found => found.code)).toEqual(['local.bind_kind', 'local.arg_kind']);
147
- expect(findings[0]).toMatchObject({nodeId: 'Html[0]/Card[0]/Switch[0]', propKey: 'bind'});
149
+ expect(findings[0]).toMatchObject({nodeId: 'Blok[0]/Card[0]/Switch[0]', propKey: 'bind'});
148
150
  expect(findings[0].message).toBe('bind of <Switch> writes a BOOL, but count is a INT local (a BOOL control writes BOOL).');
149
151
  expect(findings[1].message).toBe('self.go: to takes a BOOL, but count is a INT local.');
150
152
  });
151
153
 
152
154
  it('bind error only inside an error slot', () => {
153
- const {tree} = parseBsx('<Html><suspense><error><Alert>{error.message}</Alert></error><Text /></suspense></Html>');
154
- expect(checkBlokTree(tree, emptyBlokScope('agent'))).toEqual([]);
155
+ const {tree} = parseBsx(blok('agent', '<suspense><error><Alert>{error.message}</Alert></error><Text /></suspense>'));
156
+ expect(checkBlokTree(tree, mounted)).toEqual([]);
155
157
  });
156
158
  });
157
159
 
160
+ const codes = (source: string, scope: BlokCheckScope = mounted) => checkBlokTree(parseBsx(source).tree, scope).map(found => found.code);
161
+
158
162
  describe('what an assign blok places and reads of the run it prepares', () => {
159
- const codes = (source: string, scope: BlokCheckScope) => checkBlokTree(parseBsx(source).tree, scope).map(found => found.code);
160
- const assign = {...emptyBlokScope('assign'), args: ['image', 'exposure']};
163
+ const assign = mountedBlokScope([], [], ['image', 'exposure']);
161
164
 
162
165
  it('reads assign.given.<key>, assign.missing and assign.ready', () => {
163
- const source =
164
- '<Html><if when={assign.given.image}><Text text={args.image} /></if><arg key="exposure" /><args /><deps /><Button onClick={assign.start()} disabled={!assign.ready}>Run</Button></Html>';
166
+ const source = blok(
167
+ 'assign',
168
+ '<if when={assign.given.image}><Text text={args.image} /></if><arg key="exposure" /><args /><deps /><Button onClick={assign.start()} disabled={!assign.ready}>Run</Button>',
169
+ );
165
170
  expect(codes(source, assign)).toEqual([]);
166
171
  });
167
172
 
168
173
  it('refuses a given flag for an argument the action has not, and a field assign has not', () => {
169
- expect(codes('<Html><Text text={assign.given.nope} /></Html>', assign)).toEqual(['args.unknown']);
170
- expect(codes('<Html><Text text={assign.nope} /></Html>', assign)).toEqual(['path.unknown']);
174
+ expect(codes(blok('assign', '<Text text={assign.given.nope} />'), assign)).toEqual(['args.unknown']);
175
+ expect(codes(blok('assign', '<Text text={assign.nope} />'), assign)).toEqual(['path.unknown']);
171
176
  });
172
177
 
173
178
  it('keeps <arg>, <deps>, <dep> and the assign root to an assign blok', () => {
174
- const task = emptyBlokScope('task');
175
- expect(codes('<Html><arg key="image" /></Html>', task)).toEqual(['catalog.kind']);
176
- expect(codes('<Html><deps /><dep key="robot" /></Html>', task)).toEqual(['catalog.kind', 'catalog.kind']);
179
+ expect(codes(blok('task', '<arg key="image" />'))).toEqual(['catalog.kind']);
180
+ expect(codes(blok('task', '<deps /><dep key="robot" />'))).toEqual(['catalog.kind', 'catalog.kind']);
177
181
  // The language alone knows no kind of a component: that is the catalog's to say.
178
- expect(checkBlokTree(parseBsx('<Html><arg key="image" /></Html>').tree, task, null)).toEqual([]);
179
- expect(codes('<Html><Text text={assign.ready} /></Html>', task)).toEqual(['roots.not_in_kind']);
182
+ expect(checkBlokTree(parseBsx(blok('task', '<arg key="image" />')).tree, mounted, null)).toEqual([]);
183
+ expect(codes(blok('task', '<Text text={assign.ready} />'))).toEqual(['roots.not_in_kind']);
180
184
  });
181
185
  });
182
186
 
183
187
  describe('what a blok does to the task it is about', () => {
184
- const codes = (source: string, scope: BlokCheckScope) => checkBlokTree(parseBsx(source).tree, scope).map(found => found.code);
185
-
186
188
  it('cancels, pauses, resumes, steps and interrupts it, and reads whether it may', () => {
187
- const source =
188
- '<Html><Button onClick={task.cancel()} disabled={!task.can.cancel}>Stop</Button><Button onClick={task.pause()}>Pause</Button><Button onClick={task.resume()}>Resume</Button><Button onClick={task.step()}>Step</Button><Button onClick={task.interrupt()}>Kill</Button><Text text={deps.robot.connected ? task.started_at : task.status} /></Html>';
189
- const scope = {...emptyBlokScope('action'), dependencies: [{key: 'robot'}]} as BlokCheckScope;
190
- expect(codes(source, scope)).toEqual([]);
189
+ const source = blok(
190
+ 'action',
191
+ '<Button onClick={task.cancel()} disabled={!task.can.cancel}>Stop</Button><Button onClick={task.pause()}>Pause</Button><Button onClick={task.resume()}>Resume</Button><Button onClick={task.step()}>Step</Button><Button onClick={task.interrupt()}>Kill</Button><Text text={deps.robot.connected ? task.started_at : task.status} />',
192
+ );
193
+ expect(codes(source, mountedBlokScope([], [{key: 'robot'}]))).toEqual([]);
191
194
  });
192
195
 
193
196
  it('starts no run again: a rerun is the page\'s, never a blok\'s', () => {
194
- expect(() => parseBsx('<Html><Button onClick={task.rerun()}>Again</Button></Html>')).toThrow(/calls nothing/);
197
+ expect(() => parseBsx(blok('action', '<Button onClick={task.rerun()}>Again</Button>'))).toThrow(/calls nothing/);
195
198
  });
196
199
 
197
200
  it('refuses them where there is no task', () => {
198
- expect(codes('<Html><Button onClick={task.cancel()}>Stop</Button></Html>', emptyBlokScope('assign'))).toEqual(['roots.not_in_kind']);
201
+ expect(codes(blok('assign', '<Button onClick={task.cancel()}>Stop</Button>'))).toEqual(['roots.not_in_kind']);
199
202
  });
200
203
  });
201
204
 
202
205
  describe('what a blok places of its task, and what it leaves the host', () => {
203
- const codes = (source: string, scope: BlokCheckScope) => checkBlokTree(parseBsx(source).tree, scope).map(found => found.code);
204
-
205
206
  it('places the result, the status line and the time slider, and says how much chrome it wants', () => {
206
- const source = '<Html chrome="none"><status /><ask /><returns only="image" /><timeline /><Text text={task.paused ? "waiting" : task.duration} /></Html>';
207
- expect(codes(source, emptyBlokScope('action'))).toEqual([]);
207
+ const source = blok('action', '<status /><ask /><returns only="image" /><timeline /><Text text={task.paused ? "waiting" : task.duration} />', ' chrome="none"');
208
+ expect(codes(source)).toEqual([]);
208
209
  });
209
210
 
210
211
  it('warns where no chrome leaves the asks to a blok that places none, and where a part is claimed from inside a branch', () => {
211
- expect(codes('<Html chrome="none"><status /></Html>', emptyBlokScope('action'))).toEqual(['chrome.no_ask']);
212
- expect(codes('<Html><if when={task.running}><timeline /></if></Html>', emptyBlokScope('action'))).toEqual(['part.conditional']);
212
+ expect(codes(blok('action', '<status />', ' chrome="none"'))).toEqual(['chrome.no_ask']);
213
+ expect(codes(blok('action', '<if when={task.running}><timeline /></if>'))).toEqual(['part.conditional']);
213
214
  });
214
215
 
215
216
  it('keeps <returns> and <status> to a blok that has a task', () => {
216
- expect(codes('<Html><returns /><status /></Html>', emptyBlokScope('assign'))).toEqual(['catalog.kind', 'catalog.kind']);
217
+ expect(codes(blok('assign', '<returns /><status />'))).toEqual(['catalog.kind', 'catalog.kind']);
217
218
  });
218
219
 
219
220
  it('reads what an assign blok knows of starting', () => {
220
- expect(codes('<Html><Text text={assign.blocked} /><Button disabled={assign.starting} onClick={assign.start()}>Run</Button></Html>', emptyBlokScope('assign'))).toEqual([]);
221
+ expect(codes(blok('assign', '<Text text={assign.blocked} /><Button disabled={assign.starting} onClick={assign.start()}>Run</Button>'))).toEqual([]);
221
222
  });
222
223
  });