@arkitektio/bsx 6.2.1 → 6.4.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.
- package/dist/bsx/infer.d.ts.map +1 -1
- package/dist/bsx/infer.js +4 -0
- package/dist/bsx/infer.js.map +1 -1
- package/dist/bsx/language.js +3 -3
- package/dist/bsx/language.js.map +1 -1
- package/dist/bsx/parse.d.ts +1 -0
- package/dist/bsx/parse.d.ts.map +1 -1
- package/dist/bsx/parse.js +8 -1
- package/dist/bsx/parse.js.map +1 -1
- package/dist/bsx/print.d.ts.map +1 -1
- package/dist/bsx/print.js +2 -2
- package/dist/bsx/print.js.map +1 -1
- package/dist/bsx/references.d.ts +2 -0
- package/dist/bsx/references.d.ts.map +1 -1
- package/dist/bsx/references.js +27 -0
- package/dist/bsx/references.js.map +1 -1
- package/dist/renderer/bloks/control.d.ts.map +1 -1
- package/dist/renderer/bloks/control.js +13 -5
- package/dist/renderer/bloks/control.js.map +1 -1
- package/dist/spec/bsx_v2.json +431 -1
- package/dist/spec/catalogs/base@4.json +9 -2
- package/dist/spec/catalogs/base@4.json.js +9 -2
- package/dist/spec/vocabulary.json +14 -6
- package/dist/spec/vocabulary.json.js +14 -6
- package/package.json +1 -1
- package/src/bsx/infer.ts +4 -0
- package/src/bsx/language.ts +3 -3
- package/src/bsx/parse.ts +7 -1
- package/src/bsx/print.ts +2 -1
- package/src/bsx/references.ts +25 -0
- package/src/renderer/bloks/control.tsx +18 -4
- package/src/renderer/bloks/flow.test.tsx +11 -0
- package/src/spec/bsx_v2.json +431 -1
- package/src/spec/catalogs/base@4.json +9 -2
- package/src/spec/spec.test.ts +2 -2
- package/src/spec/vocabulary.json +14 -6
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"vocab": 2,
|
|
3
|
-
"minor":
|
|
3
|
+
"minor": 4,
|
|
4
4
|
"description": "The blok language, version 2: what a blok may read, call and store. The spec's parser, the server (rekuest_core, takt) and the renderer (orkestrator) load this file or are generated from it. The design is the blok-vocab-v2 proposal of 2026-10-25. Three layers, each versioned on its own: this language (vocab, and minor for what it gained since); the base catalog, everything every renderer draws; and the app catalogs that extend it with what one frontend draws beyond (see catalogs). A server holds a blok to the language alone, never to a catalog: whoever draws a blok checks it against the catalog it has, and an author's tooling checks it when asked.",
|
|
5
5
|
"roots": {
|
|
6
6
|
"data": {
|
|
@@ -93,6 +93,13 @@
|
|
|
93
93
|
}
|
|
94
94
|
},
|
|
95
95
|
"records": {
|
|
96
|
+
"structure": {
|
|
97
|
+
"description": "What a STRUCTURE value (a state field, an argument, a let over a list of them) reads as below it: the reference it travels as. Paths below these two are refused.",
|
|
98
|
+
"fields": [
|
|
99
|
+
{"key": "object", "kind": "STRING", "description": "The id of the object in the service that owns it."},
|
|
100
|
+
{"key": "__identifier", "kind": "STRING", "description": "The structure's identifier (@lovekit/stream)."}
|
|
101
|
+
]
|
|
102
|
+
},
|
|
96
103
|
"task": {
|
|
97
104
|
"description": "One shape for task, deps.<key>.tasks.<action>, self.tasks.<action> and every into name.",
|
|
98
105
|
"fields": [
|
|
@@ -168,7 +175,7 @@
|
|
|
168
175
|
"stale": "Known, but the source stopped updating (agent offline): the last value, flagged.",
|
|
169
176
|
"failed": "Cannot be known (no access, agent gone, query error), with an error."
|
|
170
177
|
},
|
|
171
|
-
"always_ready": ["locals", "event", "let", "literals"],
|
|
178
|
+
"always_ready": ["locals", "event", "let", "index", "literals"],
|
|
172
179
|
"propagation": [
|
|
173
180
|
"If any input of an expression is pending, the result is pending; else if any failed, the result is failed; else if any is stale, the result is stale.",
|
|
174
181
|
"logic.choose and <if> read only the branch taken; logic.all and logic.any stop at the first operand that decides them.",
|
|
@@ -333,7 +340,7 @@
|
|
|
333
340
|
"description": "How a v2 blok is stored: ComponentNodeInput {id, component, props?, children?}; ComponentPropInput {key, static_value? | dynamic_value {path} | util_call | agent_call (+ declares_value)}; ActionArgumentInput {key?, value_literal? | value_path? | util_call? | value_list? | value_dict?}. A prop or argument with no value field is null.",
|
|
334
341
|
"ids": "An explicit id matches explicit_id; a derived id is the parent's id plus /<Component>[<n>] (n: occurrence among same-named siblings), the root's <Component>[0]. A derived id always ends in ']'. A text node is <parent>/#<n> (n: text-run index) and nothing may target it.",
|
|
335
342
|
"explicit_id": "^[A-Za-z_][A-Za-z0-9_.:-]*$",
|
|
336
|
-
"foreach": "foreach props: items (expression), let (static_value and declares_value the name), key (expression, optional). With a key each item's subtree takes its runtime ids from it: <id>@<key>; without one, from its position: <id>@#<n> (n: the 0-based index). A foreach without key is stored with no key prop at all; the validator warns foreach.position_key when its items are known not to be scalars.",
|
|
343
|
+
"foreach": "foreach props: items (expression), let (static_value and declares_value the name), index (optional; static_value and declares_value the name: an INT, the item's 0-based position in items, in scope where let is), key (expression, optional). With a key each item's subtree takes its runtime ids from it: <id>@<key>; without one, from its position: <id>@#<n> (n: the 0-based index). A foreach without key is stored with no key prop at all; the validator warns foreach.position_key when its items are known not to be scalars.",
|
|
337
344
|
"if": "if props: when; children: the then-branch, then the else nodes (component else, an else-if with a when prop; only the last else may lack when).",
|
|
338
345
|
"into": "agent_call prop with declares_value <name>: a task record only that call writes. Allowed inside foreach; one name is written by one call site.",
|
|
339
346
|
"text": "Text is a child like any other: each run of text between elements is stored as a node {id: <parent>/#<n>, component: \"#text\", props: [value]}, its value a literal or an expression; several parts of one run desugar to str.template(template, values). No element can be called #text, so only text makes one. Parsing needs no catalog. What a component does with its text is its renderer's: one with a text_prop makes its text children that prop (a Button its label), any other that takes children draws them in place; a component that sets its text_prop as an attribute too, or takes neither text nor children, is a catalog finding (catalog.text). A tree stored before text was a node holds the text_prop itself, and implicit Text or span nodes under the same ids: a renderer draws both."
|
|
@@ -417,7 +424,8 @@
|
|
|
417
424
|
"bsx.text_callback": {"level": "error", "message": "Text holds a call or an effect; only a callback prop may.", "hint": "Put the call in a callback prop (onClick={{...}}), never between tags."},
|
|
418
425
|
"bsx.else_outside_if": {"level": "error", "message": "<else> is a child of <if>, after its then-branch.", "hint": "Move the <else> inside its <if>, after the then-branch children."},
|
|
419
426
|
"bsx.else_order": {"level": "error", "message": "<else> comes after the then-branch, and only the last <else> may lack when.", "hint": "Put the <else> branches last in the <if>; only the final one may leave out when."},
|
|
420
|
-
"bsx.foreach": {"level": "error", "message": "<foreach> needs items={{...}} and let=\"name\" (key={{...}}
|
|
427
|
+
"bsx.foreach": {"level": "error", "message": "<foreach> needs items={{...}} and let=\"name\" (key={{...}} and index=\"name\" are optional): {missing}."},
|
|
428
|
+
"bsx.index": {"level": "error", "message": "index={index!r} is not a name.", "hint": "Write index=\"i\": a plain identifier the children read as the item's position, from 0."},
|
|
421
429
|
"bsx.let": {"level": "error", "message": "let={let!r} is not a name.", "hint": "Write let=\"name\": a plain identifier the children read as name.field."},
|
|
422
430
|
"expr.syntax": {"level": "error", "message": "{detail}", "hint": "Expressions are a JavaScript subset: paths, literals, calls with name: value arguments, == != > >= < <=, && || !, ?? and c ? a : b."},
|
|
423
431
|
"expr.arithmetic": {"level": "error", "message": "{operator} is not an operator of the blok language; call math.{function}(a, b)."},
|
|
@@ -438,9 +446,9 @@
|
|
|
438
446
|
"callback.not_callback": {"level": "error", "message": "{key} of <{component}> is not a callback prop, so it cannot hold {what}.", "hint": "Use a callback prop of <{component}> (the catalog lists them as CALLBACK), or compute {key} without a call."},
|
|
439
447
|
"callback.unbound": {"level": "error", "message": "{key} of <{component}> is a callback prop: it holds one task call or one effect (queries.<name>.refetch(), queries.<name>.fetch_more(...), ui.copy, task.answer, assign.start).", "hint": "Give {key} a task call (deps.<key>.<action>(name: value)) or one effect, not a value."},
|
|
440
448
|
"callback.event_field": {"level": "error", "message": "event has no field {field!r} in {key} of <{component}> (its fields: {fields}).", "hint": "Read one of the fields listed for {key}: {fields}."},
|
|
441
|
-
"roots.unknown": {"level": "error", "message": "{path} reads {root!r}, which is no root, local, loop variable or record of this blok.", "hint": "Read a root this blok has (deps.<key>, args.<key>, queries.<name>, ...), a declared local, a foreach let, or an into record."},
|
|
449
|
+
"roots.unknown": {"level": "error", "message": "{path} reads {root!r}, which is no root, local, loop variable or record of this blok.", "hint": "Read a root this blok has (deps.<key>, args.<key>, queries.<name>, ...), a declared local, a foreach let or index, or an into record."},
|
|
442
450
|
"roots.not_in_kind": {"level": "error", "message": "{path}: a{n} {kind} blok has no {root!r} (it reads {roots}).", "hint": "Read one of {roots} instead."},
|
|
443
|
-
"roots.reserved": {"level": "error", "message": "{name!r} is reserved: no local, let or into may use it."},
|
|
451
|
+
"roots.reserved": {"level": "error", "message": "{name!r} is reserved: no local, let, index or into may use it."},
|
|
444
452
|
"roots.shadow": {"level": "error", "message": "{name!r} is declared twice: as {first} and as {second}."},
|
|
445
453
|
"roots.event_outside_callback": {"level": "error", "message": "{path} reads event outside a callback.", "hint": "Read event only inside a callback prop; outside one, bind a control to a local and read the local."},
|
|
446
454
|
"roots.error_outside_boundary": {"level": "error", "message": "{path} reads error outside an <error> slot.", "hint": "Read error only inside the <error> slot of a <suspense>."},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export default {
|
|
2
2
|
"vocab": 2,
|
|
3
|
-
"minor":
|
|
3
|
+
"minor": 4,
|
|
4
4
|
"description": "The blok language, version 2: what a blok may read, call and store. The spec's parser, the server (rekuest_core, takt) and the renderer (orkestrator) load this file or are generated from it. The design is the blok-vocab-v2 proposal of 2026-10-25. Three layers, each versioned on its own: this language (vocab, and minor for what it gained since); the base catalog, everything every renderer draws; and the app catalogs that extend it with what one frontend draws beyond (see catalogs). A server holds a blok to the language alone, never to a catalog: whoever draws a blok checks it against the catalog it has, and an author's tooling checks it when asked.",
|
|
5
5
|
"roots": {
|
|
6
6
|
"data": {
|
|
@@ -93,6 +93,13 @@ export default {
|
|
|
93
93
|
}
|
|
94
94
|
},
|
|
95
95
|
"records": {
|
|
96
|
+
"structure": {
|
|
97
|
+
"description": "What a STRUCTURE value (a state field, an argument, a let over a list of them) reads as below it: the reference it travels as. Paths below these two are refused.",
|
|
98
|
+
"fields": [
|
|
99
|
+
{"key": "object", "kind": "STRING", "description": "The id of the object in the service that owns it."},
|
|
100
|
+
{"key": "__identifier", "kind": "STRING", "description": "The structure's identifier (@lovekit/stream)."}
|
|
101
|
+
]
|
|
102
|
+
},
|
|
96
103
|
"task": {
|
|
97
104
|
"description": "One shape for task, deps.<key>.tasks.<action>, self.tasks.<action> and every into name.",
|
|
98
105
|
"fields": [
|
|
@@ -168,7 +175,7 @@ export default {
|
|
|
168
175
|
"stale": "Known, but the source stopped updating (agent offline): the last value, flagged.",
|
|
169
176
|
"failed": "Cannot be known (no access, agent gone, query error), with an error."
|
|
170
177
|
},
|
|
171
|
-
"always_ready": ["locals", "event", "let", "literals"],
|
|
178
|
+
"always_ready": ["locals", "event", "let", "index", "literals"],
|
|
172
179
|
"propagation": [
|
|
173
180
|
"If any input of an expression is pending, the result is pending; else if any failed, the result is failed; else if any is stale, the result is stale.",
|
|
174
181
|
"logic.choose and <if> read only the branch taken; logic.all and logic.any stop at the first operand that decides them.",
|
|
@@ -333,7 +340,7 @@ export default {
|
|
|
333
340
|
"description": "How a v2 blok is stored: ComponentNodeInput {id, component, props?, children?}; ComponentPropInput {key, static_value? | dynamic_value {path} | util_call | agent_call (+ declares_value)}; ActionArgumentInput {key?, value_literal? | value_path? | util_call? | value_list? | value_dict?}. A prop or argument with no value field is null.",
|
|
334
341
|
"ids": "An explicit id matches explicit_id; a derived id is the parent's id plus /<Component>[<n>] (n: occurrence among same-named siblings), the root's <Component>[0]. A derived id always ends in ']'. A text node is <parent>/#<n> (n: text-run index) and nothing may target it.",
|
|
335
342
|
"explicit_id": "^[A-Za-z_][A-Za-z0-9_.:-]*$",
|
|
336
|
-
"foreach": "foreach props: items (expression), let (static_value and declares_value the name), key (expression, optional). With a key each item's subtree takes its runtime ids from it: <id>@<key>; without one, from its position: <id>@#<n> (n: the 0-based index). A foreach without key is stored with no key prop at all; the validator warns foreach.position_key when its items are known not to be scalars.",
|
|
343
|
+
"foreach": "foreach props: items (expression), let (static_value and declares_value the name), index (optional; static_value and declares_value the name: an INT, the item's 0-based position in items, in scope where let is), key (expression, optional). With a key each item's subtree takes its runtime ids from it: <id>@<key>; without one, from its position: <id>@#<n> (n: the 0-based index). A foreach without key is stored with no key prop at all; the validator warns foreach.position_key when its items are known not to be scalars.",
|
|
337
344
|
"if": "if props: when; children: the then-branch, then the else nodes (component else, an else-if with a when prop; only the last else may lack when).",
|
|
338
345
|
"into": "agent_call prop with declares_value <name>: a task record only that call writes. Allowed inside foreach; one name is written by one call site.",
|
|
339
346
|
"text": "Text is a child like any other: each run of text between elements is stored as a node {id: <parent>/#<n>, component: \"#text\", props: [value]}, its value a literal or an expression; several parts of one run desugar to str.template(template, values). No element can be called #text, so only text makes one. Parsing needs no catalog. What a component does with its text is its renderer's: one with a text_prop makes its text children that prop (a Button its label), any other that takes children draws them in place; a component that sets its text_prop as an attribute too, or takes neither text nor children, is a catalog finding (catalog.text). A tree stored before text was a node holds the text_prop itself, and implicit Text or span nodes under the same ids: a renderer draws both."
|
|
@@ -417,7 +424,8 @@ export default {
|
|
|
417
424
|
"bsx.text_callback": {"level": "error", "message": "Text holds a call or an effect; only a callback prop may.", "hint": "Put the call in a callback prop (onClick={{...}}), never between tags."},
|
|
418
425
|
"bsx.else_outside_if": {"level": "error", "message": "<else> is a child of <if>, after its then-branch.", "hint": "Move the <else> inside its <if>, after the then-branch children."},
|
|
419
426
|
"bsx.else_order": {"level": "error", "message": "<else> comes after the then-branch, and only the last <else> may lack when.", "hint": "Put the <else> branches last in the <if>; only the final one may leave out when."},
|
|
420
|
-
"bsx.foreach": {"level": "error", "message": "<foreach> needs items={{...}} and let=\"name\" (key={{...}}
|
|
427
|
+
"bsx.foreach": {"level": "error", "message": "<foreach> needs items={{...}} and let=\"name\" (key={{...}} and index=\"name\" are optional): {missing}."},
|
|
428
|
+
"bsx.index": {"level": "error", "message": "index={index!r} is not a name.", "hint": "Write index=\"i\": a plain identifier the children read as the item's position, from 0."},
|
|
421
429
|
"bsx.let": {"level": "error", "message": "let={let!r} is not a name.", "hint": "Write let=\"name\": a plain identifier the children read as name.field."},
|
|
422
430
|
"expr.syntax": {"level": "error", "message": "{detail}", "hint": "Expressions are a JavaScript subset: paths, literals, calls with name: value arguments, == != > >= < <=, && || !, ?? and c ? a : b."},
|
|
423
431
|
"expr.arithmetic": {"level": "error", "message": "{operator} is not an operator of the blok language; call math.{function}(a, b)."},
|
|
@@ -438,9 +446,9 @@ export default {
|
|
|
438
446
|
"callback.not_callback": {"level": "error", "message": "{key} of <{component}> is not a callback prop, so it cannot hold {what}.", "hint": "Use a callback prop of <{component}> (the catalog lists them as CALLBACK), or compute {key} without a call."},
|
|
439
447
|
"callback.unbound": {"level": "error", "message": "{key} of <{component}> is a callback prop: it holds one task call or one effect (queries.<name>.refetch(), queries.<name>.fetch_more(...), ui.copy, task.answer, assign.start).", "hint": "Give {key} a task call (deps.<key>.<action>(name: value)) or one effect, not a value."},
|
|
440
448
|
"callback.event_field": {"level": "error", "message": "event has no field {field!r} in {key} of <{component}> (its fields: {fields}).", "hint": "Read one of the fields listed for {key}: {fields}."},
|
|
441
|
-
"roots.unknown": {"level": "error", "message": "{path} reads {root!r}, which is no root, local, loop variable or record of this blok.", "hint": "Read a root this blok has (deps.<key>, args.<key>, queries.<name>, ...), a declared local, a foreach let, or an into record."},
|
|
449
|
+
"roots.unknown": {"level": "error", "message": "{path} reads {root!r}, which is no root, local, loop variable or record of this blok.", "hint": "Read a root this blok has (deps.<key>, args.<key>, queries.<name>, ...), a declared local, a foreach let or index, or an into record."},
|
|
442
450
|
"roots.not_in_kind": {"level": "error", "message": "{path}: a{n} {kind} blok has no {root!r} (it reads {roots}).", "hint": "Read one of {roots} instead."},
|
|
443
|
-
"roots.reserved": {"level": "error", "message": "{name!r} is reserved: no local, let or into may use it."},
|
|
451
|
+
"roots.reserved": {"level": "error", "message": "{name!r} is reserved: no local, let, index or into may use it."},
|
|
444
452
|
"roots.shadow": {"level": "error", "message": "{name!r} is declared twice: as {first} and as {second}."},
|
|
445
453
|
"roots.event_outside_callback": {"level": "error", "message": "{path} reads event outside a callback.", "hint": "Read event only inside a callback prop; outside one, bind a control to a local and read the local."},
|
|
446
454
|
"roots.error_outside_boundary": {"level": "error", "message": "{path} reads error outside an <error> slot.", "hint": "Read error only inside the <error> slot of a <suspense>."},
|
package/package.json
CHANGED
package/src/bsx/infer.ts
CHANGED
|
@@ -37,6 +37,7 @@ const ITEM_KEY = '...';
|
|
|
37
37
|
const BIND_PROP = 'bind';
|
|
38
38
|
const FOREACH = 'foreach';
|
|
39
39
|
const FOREACH_LET = 'let';
|
|
40
|
+
const FOREACH_INDEX = 'index';
|
|
40
41
|
const FOREACH_ITEMS = 'items';
|
|
41
42
|
/** The bind kinds a local takes from its control; a LIST, DICT or ANY control tells too little. */
|
|
42
43
|
const BIND_INFERRED: ReadonlySet<string> = new Set(['STRING', 'INT', 'FLOAT', 'BOOL']);
|
|
@@ -260,8 +261,11 @@ class Uses {
|
|
|
260
261
|
this.component(node, scope);
|
|
261
262
|
const declared =
|
|
262
263
|
node.component === FOREACH ? node.props?.find(prop => prop.key === FOREACH_LET && prop.declares_value)?.declares_value ?? null : null;
|
|
264
|
+
const index =
|
|
265
|
+
node.component === FOREACH ? node.props?.find(prop => prop.key === FOREACH_INDEX && prop.declares_value)?.declares_value ?? null : null;
|
|
263
266
|
const inner = new Map(scope);
|
|
264
267
|
if (declared !== null) inner.set(declared, null);
|
|
268
|
+
if (index !== null) inner.set(index, null);
|
|
265
269
|
for (const prop of node.props ?? []) this.prop(prop, declared !== null && prop.key === FOREACH_ITEMS ? scope : inner);
|
|
266
270
|
// Every local of a node is in scope in all of its children.
|
|
267
271
|
for (const child of node.children ?? []) {
|
package/src/bsx/language.ts
CHANGED
|
@@ -2,7 +2,7 @@ import {defaultBlokView, innerContext, type BlokView, type SpecComponent, type S
|
|
|
2
2
|
import {BLOK_KINDS, kindIn, loadVocabulary, type BlokContextName, type BlokKind, type VocabFunction, type VocabRecordField} from '../spec/vocabulary';
|
|
3
3
|
import {BLOK_COMPONENT, declaredQueries, declaredSubscriptions, QUERY_COMPONENT, SUBSCRIPTION_COMPONENT} from './declared';
|
|
4
4
|
import {resolveLocals} from './infer';
|
|
5
|
-
import {FOREACH_COMPONENT, FOREACH_LET_PROP, INTO_KEYWORD} from './parse';
|
|
5
|
+
import {FOREACH_COMPONENT, FOREACH_INDEX_PROP, FOREACH_LET_PROP, INTO_KEYWORD} from './parse';
|
|
6
6
|
import {parseBsxPartial} from './partial';
|
|
7
7
|
import {type BlokCheckScope, type BlokPortMatch, type BlokScopeDependency, type BlokScopeLocalPort, type ResolvedBlokLocal} from './references';
|
|
8
8
|
import type {BsxRange} from './source';
|
|
@@ -488,7 +488,7 @@ const intoNames = (text: string): string[] => [...new Set([...text.matchAll(new
|
|
|
488
488
|
const foreachLets = (stack: readonly OpenElement[]): string[] =>
|
|
489
489
|
stack
|
|
490
490
|
.filter(element => element.tag === FOREACH_COMPONENT)
|
|
491
|
-
.
|
|
491
|
+
.flatMap(element => [element.attributes.get(FOREACH_LET_PROP) ?? '', element.attributes.get(FOREACH_INDEX_PROP) ?? ''])
|
|
492
492
|
.filter(Boolean);
|
|
493
493
|
|
|
494
494
|
const dependencyOf = (env: BsxEnvironment, key: string): BlokScopeDependency | undefined =>
|
|
@@ -704,7 +704,7 @@ export const completeBsx = (text: string, offset: number, env: BsxEnvironment):
|
|
|
704
704
|
return cursor.attr === 'className' ? classCompletions(cursor.value, cursor.from + cursor.value.length) : [];
|
|
705
705
|
case 'attrExpression': {
|
|
706
706
|
const end = cursor.from + cursor.expression.length;
|
|
707
|
-
if (cursor.tag === FOREACH_COMPONENT && cursor.attr === FOREACH_LET_PROP) return [];
|
|
707
|
+
if (cursor.tag === FOREACH_COMPONENT && (cursor.attr === FOREACH_LET_PROP || cursor.attr === FOREACH_INDEX_PROP)) return [];
|
|
708
708
|
if (cursor.attr === 'className' && inString(cursor.expression)) {
|
|
709
709
|
const quote = Math.max(cursor.expression.lastIndexOf('"'), cursor.expression.lastIndexOf("'"));
|
|
710
710
|
return classCompletions(cursor.expression.slice(quote + 1), end);
|
package/src/bsx/parse.ts
CHANGED
|
@@ -60,6 +60,7 @@ export const IF_COMPONENT = 'if';
|
|
|
60
60
|
export const ELSE_COMPONENT = 'else';
|
|
61
61
|
export const FOREACH_COMPONENT = 'foreach';
|
|
62
62
|
export const FOREACH_LET_PROP = 'let';
|
|
63
|
+
export const FOREACH_INDEX_PROP = 'index';
|
|
63
64
|
export const FOREACH_ITEMS_PROP = 'items';
|
|
64
65
|
export const FOREACH_KEY_PROP = 'key';
|
|
65
66
|
const LOCAL_COMPONENT = 'local';
|
|
@@ -468,6 +469,10 @@ class Lowering {
|
|
|
468
469
|
if (declared && (typeof declared.value !== 'string' || !isLocalName(declared.value))) {
|
|
469
470
|
throw this.fail('bsx.let', declared.offset, {let: typeof declared.value === 'string' ? declared.value : '{...}'});
|
|
470
471
|
}
|
|
472
|
+
const index = attributes.get(FOREACH_INDEX_PROP);
|
|
473
|
+
if (index && (typeof index.value !== 'string' || !isLocalName(index.value))) {
|
|
474
|
+
throw this.fail('bsx.index', index.offset, {index: typeof index.value === 'string' ? index.value : '{...}'});
|
|
475
|
+
}
|
|
471
476
|
for (const name of ['items', 'key']) {
|
|
472
477
|
const attribute = attributes.get(name);
|
|
473
478
|
if (attribute && (typeof attribute.value === 'string' || attribute.value === true)) {
|
|
@@ -554,12 +559,13 @@ class Lowering {
|
|
|
554
559
|
}
|
|
555
560
|
if (value === true) return {key: attribute.name, static_value: true};
|
|
556
561
|
if (typeof value === 'string') {
|
|
557
|
-
if (attribute.name === FOREACH_LET_PROP && component === FOREACH_COMPONENT) {
|
|
562
|
+
if ((attribute.name === FOREACH_LET_PROP || attribute.name === FOREACH_INDEX_PROP) && component === FOREACH_COMPONENT) {
|
|
558
563
|
return {key: attribute.name, static_value: value, declares_value: value};
|
|
559
564
|
}
|
|
560
565
|
return {key: attribute.name, static_value: value};
|
|
561
566
|
}
|
|
562
567
|
if (attribute.name === FOREACH_LET_PROP && component === FOREACH_COMPONENT) throw this.fail('bsx.let', attribute.offset, {let: '{...}'});
|
|
568
|
+
if (attribute.name === FOREACH_INDEX_PROP && component === FOREACH_COMPONENT) throw this.fail('bsx.index', attribute.offset, {index: '{...}'});
|
|
563
569
|
return this.value(attribute.name, value, attribute.offset);
|
|
564
570
|
}
|
|
565
571
|
|
package/src/bsx/print.ts
CHANGED
|
@@ -12,6 +12,7 @@ import {
|
|
|
12
12
|
CLASSES_FUNCTION,
|
|
13
13
|
FETCH_MORE_FUNCTION,
|
|
14
14
|
FOREACH_COMPONENT,
|
|
15
|
+
FOREACH_INDEX_PROP,
|
|
15
16
|
FOREACH_LET_PROP,
|
|
16
17
|
LEGACY_IMPLICIT_TEXTS,
|
|
17
18
|
TEXT_NODE,
|
|
@@ -253,7 +254,7 @@ class Printer {
|
|
|
253
254
|
return `${key}={${this.agentCall(prop.agent_call)}${into}}`;
|
|
254
255
|
}
|
|
255
256
|
if (prop.declares_value != null) {
|
|
256
|
-
if (node.component === FOREACH_COMPONENT && key === FOREACH_LET_PROP && prop.static_value === prop.declares_value) {
|
|
257
|
+
if (node.component === FOREACH_COMPONENT && (key === FOREACH_LET_PROP || key === FOREACH_INDEX_PROP) && prop.static_value === prop.declares_value) {
|
|
257
258
|
return `${key}=${quoted(prop.declares_value)}`;
|
|
258
259
|
}
|
|
259
260
|
if (node.component === 'local' && key === 'name' && prop.static_value === prop.declares_value) {
|
package/src/bsx/references.ts
CHANGED
|
@@ -224,6 +224,7 @@ const TOAST_TASK_PROP = 'task';
|
|
|
224
224
|
const CLASS_NAME = 'className';
|
|
225
225
|
const FOREACH = 'foreach';
|
|
226
226
|
const FOREACH_LET = 'let';
|
|
227
|
+
const FOREACH_INDEX = 'index';
|
|
227
228
|
const FOREACH_ITEMS = 'items';
|
|
228
229
|
const FOREACH_KEY = 'key';
|
|
229
230
|
/** An item of these kinds is no scalar: a foreach over it without key is keyed by position (`foreach.position_key`). */
|
|
@@ -265,6 +266,12 @@ const recordMatch = (
|
|
|
265
266
|
}),
|
|
266
267
|
});
|
|
267
268
|
|
|
269
|
+
/** A STRUCTURE as what may be read below it: the `structure` record (`object`, `__identifier`). */
|
|
270
|
+
const structureMatch = (structure: BlokPortMatch): BlokPortMatch => ({
|
|
271
|
+
...recordMatch(structure.key || 'structure', loadVocabulary().records.structure.fields),
|
|
272
|
+
nullable: structure.nullable ?? null,
|
|
273
|
+
});
|
|
274
|
+
|
|
268
275
|
const taskRecordMatch = (key: string, returns?: readonly BlokPortMatch[] | null): BlokPortMatch =>
|
|
269
276
|
recordMatch(
|
|
270
277
|
key,
|
|
@@ -321,6 +328,11 @@ export const foreachLet = (node: BlokComponentNode): string | null => {
|
|
|
321
328
|
return node.props?.find(prop => prop.key === FOREACH_LET && prop.declares_value)?.declares_value ?? null;
|
|
322
329
|
};
|
|
323
330
|
|
|
331
|
+
/** The name a `foreach` gives each item's position (`index="i"`), or null when it names none. */
|
|
332
|
+
export const foreachIndex = (node: BlokComponentNode): string | null => {
|
|
333
|
+
if (node.component !== FOREACH) return null;
|
|
334
|
+
return node.props?.find(prop => prop.key === FOREACH_INDEX && prop.declares_value)?.declares_value ?? null;
|
|
335
|
+
};
|
|
324
336
|
|
|
325
337
|
/** A `<local>` in scope: its node and the port it resolved to. */
|
|
326
338
|
type LocalBinding = {local: true; node: string; name: string; port: ResolvedBlokLocal};
|
|
@@ -671,6 +683,8 @@ class Checker {
|
|
|
671
683
|
const items = node.props?.find(prop => prop.key === FOREACH_ITEMS) ?? null;
|
|
672
684
|
inner.set(declared, this.bindLet(declared, items, {node, key: FOREACH_ITEMS, callback: false, inError}, scope));
|
|
673
685
|
}
|
|
686
|
+
const index = foreachIndex(node);
|
|
687
|
+
if (index !== null) inner.set(index, this.bindIndex(index, {node, key: FOREACH_INDEX, callback: false, inError}, new Map(inner)));
|
|
674
688
|
for (const prop of node.props ?? []) {
|
|
675
689
|
// A foreach reads its items outside the loop, its key inside it.
|
|
676
690
|
const propScope = declared !== null && prop.key === FOREACH_ITEMS ? scope : inner;
|
|
@@ -1042,6 +1056,15 @@ class Checker {
|
|
|
1042
1056
|
if (call.operation === FETCH_MORE_FUNCTION && query.more == null) this.add('query.fetch_more', where.node.id, {path, name}, where.key);
|
|
1043
1057
|
}
|
|
1044
1058
|
|
|
1059
|
+
/** What a foreach's `index` is bound to: the item's 0-based position. `scope` holds the loop variable already. */
|
|
1060
|
+
private bindIndex(name: string, where: Where, scope: Scope): Binding {
|
|
1061
|
+
if (this.vocabulary.reserved.has(name)) this.add('roots.reserved', where.node.id, {name});
|
|
1062
|
+
else if (scope.has(name)) {
|
|
1063
|
+
this.add('roots.shadow', where.node.id, {name, first: bindingWhat(name, scope.get(name), this.into), second: `the index of <foreach> (${where.node.id})`});
|
|
1064
|
+
}
|
|
1065
|
+
return {key: name, kind: 'INT'};
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1045
1068
|
private bindLet(name: string, items: BlokComponentProp | null, where: Where, scope: Scope): Binding {
|
|
1046
1069
|
if (this.vocabulary.reserved.has(name)) this.add('roots.reserved', where.node.id, {name});
|
|
1047
1070
|
else if (scope.has(name)) {
|
|
@@ -1298,6 +1321,8 @@ class Checker {
|
|
|
1298
1321
|
for (let index = 0; index < rest.length; index += 1) {
|
|
1299
1322
|
const part = rest[index];
|
|
1300
1323
|
if (current === null || isOpen(current)) return current;
|
|
1324
|
+
// A structure reads below it as the reference it travels as.
|
|
1325
|
+
if (current.kind === 'STRUCTURE' && !current.children?.length) current = structureMatch(current);
|
|
1301
1326
|
const children = current.children ?? [];
|
|
1302
1327
|
let child = children.find(candidate => candidate.key === part) ?? null;
|
|
1303
1328
|
if (!child && children.length === 1 && children[0].key === ITEM) child = children[0];
|
|
@@ -31,7 +31,7 @@ import {blokPlatform} from '../../platform';
|
|
|
31
31
|
* - `<if when={...}>`: its children are the then-branch, then `<else when>`
|
|
32
32
|
* (else-if) and a last `<else>`; only the active branch is rendered and read.
|
|
33
33
|
* - `<foreach items={...} let="p" key={p.id}>`: once per item, keyed; without
|
|
34
|
-
* `key`, keyed by position.
|
|
34
|
+
* `key`, keyed by position. `index="i"` names the item's position, from 0.
|
|
35
35
|
* - `<match value={...}>` with `<case is={...}>` and `<default>`.
|
|
36
36
|
* - `<suspense>` with `<fallback>` and `<error>`: React's model, the error
|
|
37
37
|
* boundary folded in; a pending read inside shows the fallback, a failed
|
|
@@ -62,15 +62,23 @@ const ForeachIteration = (props: {
|
|
|
62
62
|
itemPath?: string;
|
|
63
63
|
/** The item itself, for sources that have no addressable path. */
|
|
64
64
|
item: unknown;
|
|
65
|
+
/** The name the item's position is read by (`index="i"`), when the foreach names one. */
|
|
66
|
+
indexName?: string;
|
|
67
|
+
/** The item's 0-based position in the list. */
|
|
68
|
+
index: number;
|
|
65
69
|
childIds: string[];
|
|
66
70
|
buildChild: (id: string, basePath?: string) => React.ReactNode;
|
|
67
71
|
}) => {
|
|
68
|
-
const {buildChild, childIds, item, itemKey, itemPath, scopeName} = props;
|
|
72
|
+
const {buildChild, childIds, index, indexName, item, itemKey, itemPath, scopeName} = props;
|
|
69
73
|
|
|
70
74
|
// A path-backed item aliases to its path, so a bound control inside the loop
|
|
71
75
|
// writes the real backing list. Without a path the item is handed down by value.
|
|
72
76
|
const aliases = React.useMemo(() => (itemPath ? {[scopeName]: itemPath} : undefined), [itemPath, scopeName]);
|
|
73
|
-
|
|
77
|
+
// The position has no path: it is always handed down by value.
|
|
78
|
+
const values = React.useMemo(() => {
|
|
79
|
+
const position = indexName ? {[indexName]: index} : undefined;
|
|
80
|
+
return itemPath ? position : {[scopeName]: item, ...position};
|
|
81
|
+
}, [index, indexName, item, itemPath, scopeName]);
|
|
74
82
|
|
|
75
83
|
return (
|
|
76
84
|
<BlokItemKeyProvider itemKey={itemKey}>
|
|
@@ -87,6 +95,7 @@ const foreachSchema = z
|
|
|
87
95
|
.object({
|
|
88
96
|
items: z.array(z.unknown()).nullish().describe('The list to iterate.'),
|
|
89
97
|
let: z.string().describe('The name each item is read by inside the loop (`let="p"`, read as `p.name`).'),
|
|
98
|
+
index: z.string().optional().describe('A name for the item\'s position, from 0 (`index="i"`), read bare inside the loop.'),
|
|
90
99
|
key: z
|
|
91
100
|
.unknown()
|
|
92
101
|
.optional()
|
|
@@ -136,6 +145,8 @@ export const Foreach = createBlokComponent(
|
|
|
136
145
|
const blok = useBlok(component, schema);
|
|
137
146
|
const items = useValue(blok.items);
|
|
138
147
|
const scopeName = blok['let'].prop?.declares_value ?? (typeof blok['let'].prop?.static_value === 'string' ? blok['let'].prop.static_value : '');
|
|
148
|
+
const indexProp = blok.index.prop;
|
|
149
|
+
const indexName = indexProp?.declares_value ?? (typeof indexProp?.static_value === 'string' ? indexProp.static_value : undefined);
|
|
139
150
|
const resolve = useScopedResolver();
|
|
140
151
|
const childIds = React.useMemo(() => component.children?.map(child => child.id) ?? [], [component]);
|
|
141
152
|
const scope = useBlokScope();
|
|
@@ -150,7 +161,8 @@ export const Foreach = createBlokComponent(
|
|
|
150
161
|
const keys = keyProp
|
|
151
162
|
? items.map((item, index) => {
|
|
152
163
|
const path = itemPath(index);
|
|
153
|
-
const
|
|
164
|
+
const position = indexName ? {[indexName]: index} : {};
|
|
165
|
+
const resolved = resolve(keyProp, path ? position : {[scopeName]: item, ...position}, path ? {[scopeName]: path} : undefined);
|
|
154
166
|
return resolved.ok ? resolved.value : null;
|
|
155
167
|
})
|
|
156
168
|
: null;
|
|
@@ -165,6 +177,8 @@ export const Foreach = createBlokComponent(
|
|
|
165
177
|
scopeName={scopeName}
|
|
166
178
|
itemPath={itemPath(index)}
|
|
167
179
|
item={item}
|
|
180
|
+
indexName={indexName || undefined}
|
|
181
|
+
index={index}
|
|
168
182
|
childIds={childIds}
|
|
169
183
|
buildChild={buildChild}
|
|
170
184
|
/>
|
|
@@ -85,6 +85,17 @@ describe('foreach', () => {
|
|
|
85
85
|
expect([...document.querySelectorAll('p')].map(element => element.textContent)).toEqual(['one', 'two', 'three']);
|
|
86
86
|
});
|
|
87
87
|
|
|
88
|
+
it('names each item\'s position with index, for a path-backed list and a computed one', () => {
|
|
89
|
+
render(
|
|
90
|
+
'<Card><foreach items={xs} let="x" index="i"><Text>{math.add(a: i, b: 1)}. {x}</Text></foreach><foreach items={list.reverse(xs)} let="y" index="j" key={j}><Text>r{j} {y}</Text></foreach></Card>',
|
|
91
|
+
{xs: ['one', 'two']},
|
|
92
|
+
);
|
|
93
|
+
expect(screen.queryByText('1. one')).not.toBeNull();
|
|
94
|
+
expect(screen.queryByText('2. two')).not.toBeNull();
|
|
95
|
+
expect(screen.queryByText('r0 two')).not.toBeNull();
|
|
96
|
+
expect(screen.queryByText('r1 one')).not.toBeNull();
|
|
97
|
+
});
|
|
98
|
+
|
|
88
99
|
describe('inside another foreach', () => {
|
|
89
100
|
const rows = [
|
|
90
101
|
{id: 'r1', cells: [{id: 'c1', name: 'One'}, {id: 'c2', name: 'Two'}]},
|