@protobase/workflows 0.99.15 → 0.100.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 (48) hide show
  1. package/dist/definition/check-aws.js +80 -0
  2. package/dist/definition/check-definition.d.ts +4 -0
  3. package/dist/definition/check-definition.js +27 -10
  4. package/dist/definition/check-reads.js +9 -4
  5. package/dist/definition/check-schema.js +19 -7
  6. package/dist/definition/dataflow/check-dataflow.js +36 -0
  7. package/dist/definition/dataflow/dependencies.d.ts +17 -0
  8. package/dist/definition/dataflow/dependencies.js +88 -0
  9. package/dist/definition/expression-fields.d.ts +5 -0
  10. package/dist/definition/expression-fields.js +1 -0
  11. package/dist/definition/expressions.d.ts +5 -0
  12. package/dist/definition/graph.js +1 -1
  13. package/dist/definition/parts.d.ts +21 -0
  14. package/dist/definition/parts.js +21 -0
  15. package/dist/definition/pins/check-pins.js +40 -0
  16. package/dist/definition/pins/pin-sources.d.ts +33 -0
  17. package/dist/definition/pins/pin-sources.js +78 -0
  18. package/dist/definition/pins/pin-type.d.ts +19 -0
  19. package/dist/definition/pins/pin-type.js +61 -0
  20. package/dist/definition/pins/references.d.ts +32 -0
  21. package/dist/definition/pins/references.js +118 -0
  22. package/dist/definition/pins/resolve-schema.d.ts +21 -0
  23. package/dist/definition/pins/resolve-schema.js +62 -0
  24. package/dist/definition/reads.js +17 -10
  25. package/dist/definition/schema.d.ts +285 -191
  26. package/dist/definition/schema.js +275 -245
  27. package/dist/definition/types.d.ts +16 -4
  28. package/dist/definition/types.js +5 -0
  29. package/dist/definition/visible.js +6 -4
  30. package/dist/index.d.ts +11 -3
  31. package/dist/index.js +12 -3
  32. package/dist/plugin/index.d.ts +2 -1
  33. package/dist/plugin/index.js +2 -1
  34. package/dist/plugin/value-check.d.ts +9 -0
  35. package/dist/plugin/value-check.js +24 -0
  36. package/dist/run/block-state.js +1 -1
  37. package/dist/run/context.d.ts +2 -1
  38. package/dist/run/dataflow.js +183 -0
  39. package/dist/run/position.js +16 -11
  40. package/dist/run/recovery.js +22 -3
  41. package/dist/run/rerun-from.d.ts +8 -5
  42. package/dist/run/rerun-from.js +61 -5
  43. package/dist/run/rerun.d.ts +4 -3
  44. package/dist/run/rerun.js +16 -7
  45. package/dist/run/result-states.js +3 -3
  46. package/dist/run/scope.js +5 -4
  47. package/dist/run/segment.js +5 -2
  48. package/package.json +1 -1
@@ -0,0 +1,78 @@
1
+ import { expressionSource } from "../expressions.js";
2
+ import { innerMachine } from "../inner.js";
3
+ import { resolveSchema, schemaAt } from "./resolve-schema.js";
4
+ //#region src/definition/pins/pin-sources.ts
5
+ const argumentsOf = (state) => {
6
+ const args = "Arguments" in state ? state.Arguments : void 0;
7
+ return typeof args === "object" && args !== null && !Array.isArray(args) ? args : {};
8
+ };
9
+ const statesOutsideBlocks = (machine) => Object.entries(machine.States).flatMap(([name, state]) => {
10
+ const inner = state.Type === "Map" ? innerMachine(state) : void 0;
11
+ return [[name, state], ...inner ? statesOutsideBlocks(inner) : []];
12
+ });
13
+ /** The state outside any Block that keeps its whole result in `variable`, when exactly one does; its name. */
14
+ const resultState = (machine, variable) => {
15
+ const keeping = statesOutsideBlocks(machine).filter(([, state]) => {
16
+ return ("Assign" in state ? state.Assign : void 0)?.[variable] !== void 0;
17
+ });
18
+ if (keeping.length !== 1) return;
19
+ const [name, state] = keeping[0];
20
+ return expressionSource(state.Assign[variable])?.trim() === "$states.result" ? [name, state] : void 0;
21
+ };
22
+ /** A Task's result as its type gives it, with what its arguments name filled in; undefined for any other state or an unknown type. */
23
+ const resultSchema = (state, schemas) => state.Type === "Task" && schemas.outputs[state.Resource] ? resolveSchema(schemas.outputs[state.Resource], argumentsOf(state), schemas.records) : void 0;
24
+ /** The run's input as the trigger gives it, with what its input names filled in; undefined for a manual workflow. */
25
+ const triggerSchema = (definition, schemas) => {
26
+ const [id, input] = Object.entries(definition.trigger ?? {})[0] ?? [];
27
+ if (id === void 0 || id === "manual" || !schemas.outputs[id]) return;
28
+ return resolveSchema(schemas.outputs[id], input ?? {}, schemas.records);
29
+ };
30
+ /**
31
+ * The schema of the value a reference reads, in a state outside any Block: the trigger's run input, or the result a
32
+ * state keeps whole in a variable, down the reference's path; undefined when nothing says, so it is of any type.
33
+ */
34
+ /** What `$states.context.Steps.<name>` gives in data order: a state's status, its error and cause. */
35
+ const stepStatusSchema = {
36
+ type: "object",
37
+ title: "Step status",
38
+ properties: {
39
+ Status: { enum: [
40
+ "SUCCEEDED",
41
+ "FAILED",
42
+ "TIMED_OUT",
43
+ "ABORTED",
44
+ "SKIPPED"
45
+ ] },
46
+ Error: { type: ["string", "null"] },
47
+ Cause: { type: ["string", "null"] }
48
+ }
49
+ };
50
+ const referenceSchema = (definition, reference, schemas) => {
51
+ const { base, path } = reference;
52
+ if ("input" in base) return schemaAt(triggerSchema(definition, schemas), path);
53
+ if ("step" in base) return schemaAt(stepStatusSchema, path);
54
+ const found = resultState(definition.stateMachine, base.variable);
55
+ return found && schemaAt(resultSchema(found[1], schemas), path);
56
+ };
57
+ /** A Task's input as its type takes it, with what its arguments name filled in, such as the record a records step writes. */
58
+ const inputSchema = (state, schemas) => state.Type === "Task" && schemas.inputs[state.Resource] ? resolveSchema(schemas.inputs[state.Resource], argumentsOf(state), schemas.records) : void 0;
59
+ /** Every Task of a machine outside any Block, with its path, the ones in Maps too: a Block's steps read only its own input. */
60
+ /** Every Block of a machine outside any other Block, with its path, the ones in Maps too. */
61
+ const blocksOutsideBlocks = (machine, scope = []) => Object.entries(machine.States).flatMap(([name, state]) => {
62
+ if (state.Type === "Block") return [{
63
+ path: [...scope, name].join("/"),
64
+ state
65
+ }];
66
+ const inner = state.Type === "Map" ? innerMachine(state) : void 0;
67
+ return inner ? blocksOutsideBlocks(inner, [...scope, name]) : [];
68
+ });
69
+ const tasksOutsideBlocks = (machine, scope = []) => Object.entries(machine.States).flatMap(([name, state]) => {
70
+ const here = [{
71
+ path: [...scope, name].join("/"),
72
+ state
73
+ }];
74
+ const inner = state.Type === "Map" ? innerMachine(state) : void 0;
75
+ return [...here, ...inner ? tasksOutsideBlocks(inner, [...scope, name]) : []];
76
+ }).filter(({ state }) => state.Type === "Task");
77
+ //#endregion
78
+ export { argumentsOf, blocksOutsideBlocks, inputSchema, referenceSchema, resultSchema, resultState, stepStatusSchema, tasksOutsideBlocks, triggerSchema };
@@ -0,0 +1,19 @@
1
+ //#region src/definition/pins/pin-type.d.ts
2
+ type JsonSchema = Record<string, unknown>;
3
+ /** What a pin's value can be, as JSON Schema's types name it. */
4
+ export type PinKind = 'string' | 'integer' | 'number' | 'boolean' | 'object' | 'array' | 'null';
5
+ /** A pin's type: the kinds its value can be, or `any` when its schema says nothing, with the schema's title. */
6
+ export type PinType = {
7
+ kinds: readonly PinKind[] | 'any';
8
+ title?: string;
9
+ };
10
+ /** The type of a pin whose value `schema` describes. */
11
+ export declare const pinType: (schema: JsonSchema | undefined) => PinType;
12
+ /**
13
+ * Whether a value of `source` fits a pin of `target`: every kind the source can be, the target takes; a whole number
14
+ * fits a number, and a pin of any type fits and takes anything, as its value is known only when the run gets it.
15
+ */
16
+ export declare const fits: (source: PinType, target: PinType) => boolean;
17
+ /** A type in words, for a problem's message: its title, or what it can be, such as `text or a whole number`. */
18
+ export declare const pinTypeText: (type: PinType) => string;
19
+ //#endregion
@@ -0,0 +1,61 @@
1
+ //#region src/definition/pins/pin-type.ts
2
+ const kinds = [
3
+ "string",
4
+ "integer",
5
+ "number",
6
+ "boolean",
7
+ "object",
8
+ "array",
9
+ "null"
10
+ ];
11
+ const isSchema = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
12
+ const declaredKinds = (schema) => (Array.isArray(schema.type) ? schema.type : schema.type === void 0 ? [] : [schema.type]).filter((kind) => kinds.includes(kind));
13
+ const kindsOf = (schema) => {
14
+ const known = declaredKinds(schema);
15
+ const branches = [schema.anyOf, schema.oneOf].find(Array.isArray);
16
+ if (branches) {
17
+ const each = branches.filter(isSchema).map(kindsOf);
18
+ if (each.includes("any")) return known.length > 0 ? known : "any";
19
+ const either = [...new Set(each.flat())];
20
+ return known.length > 0 ? either.filter((kind) => known.includes(kind) || kind === "integer" && known.includes("number")) : either;
21
+ }
22
+ if (known.length > 0) return known;
23
+ if (Array.isArray(schema.enum) && schema.enum.every((value) => typeof value === "string")) return ["string"];
24
+ if (isSchema(schema.properties)) return ["object"];
25
+ return "any";
26
+ };
27
+ /** The type of a pin whose value `schema` describes. */
28
+ const pinType = (schema) => {
29
+ if (!schema) return { kinds: "any" };
30
+ const title = typeof schema.title === "string" ? schema.title : void 0;
31
+ return {
32
+ kinds: kindsOf(schema),
33
+ ...title !== void 0 && { title }
34
+ };
35
+ };
36
+ /**
37
+ * Whether a value of `source` fits a pin of `target`: every kind the source can be, the target takes; a whole number
38
+ * fits a number, and a pin of any type fits and takes anything, as its value is known only when the run gets it.
39
+ */
40
+ const fits = (source, target) => {
41
+ if (source.kinds === "any" || target.kinds === "any") return true;
42
+ const taken = target.kinds;
43
+ return source.kinds.every((kind) => taken.includes(kind) || kind === "integer" && taken.includes("number"));
44
+ };
45
+ const kindNames = {
46
+ string: "text",
47
+ integer: "a whole number",
48
+ number: "a number",
49
+ boolean: "true or false",
50
+ object: "an object",
51
+ array: "a list",
52
+ null: "null"
53
+ };
54
+ /** A type in words, for a problem's message: its title, or what it can be, such as `text or a whole number`. */
55
+ const pinTypeText = (type) => {
56
+ if (type.title !== void 0) return type.title;
57
+ if (type.kinds === "any") return "any value";
58
+ return type.kinds.map((kind) => kindNames[kind]).join(" or ");
59
+ };
60
+ //#endregion
61
+ export { fits, pinType, pinTypeText };
@@ -0,0 +1,32 @@
1
+ //#region src/definition/pins/references.d.ts
2
+ /** Where a read starts: the run's input, a variable a state assigns, or in data order a state's status by its name. */
3
+ export type ReadBase = {
4
+ input: true;
5
+ } | {
6
+ variable: string;
7
+ } | {
8
+ step: string;
9
+ };
10
+ /** A value that is one read by name and nothing else: where it starts, and the fields it goes down. */
11
+ export type Reference = {
12
+ base: ReadBase;
13
+ path: string[];
14
+ };
15
+ /**
16
+ * The read a value is, when it is an expression of one read by name, such as `{% $fetch.body.ip %}` or
17
+ * `{% $states.context.Execution.Input.record.name %}`; undefined for any other value, such as text, an expression that
18
+ * computes, or `$states.result`.
19
+ */
20
+ export declare const parseReference: (value: unknown) => Reference | undefined;
21
+ /** The JSONata read of a reference, such as `$fetch.body.ip`. */
22
+ export declare const readText: ({ base, path }: Reference) => string;
23
+ /** A reference as the value of a field: `{% <read> %}`. */
24
+ export declare const referenceValue: (reference: Reference) => string;
25
+ /**
26
+ * Every read by name in a value's expression, however the expression combines them, such as `$fetch.body.ip` and
27
+ * `$uuid.body.uuid` in `{% $fetch.body.ip & ' / ' & $uuid.body.uuid %}`: each where it starts and the plain names it
28
+ * goes down. A name the expression binds itself, and JSONata's functions, are no reads. None for a value that is no
29
+ * expression or does not parse.
30
+ */
31
+ export declare const expressionReferences: (value: unknown) => Reference[];
32
+ //#endregion
@@ -0,0 +1,118 @@
1
+ import { expressionSource, parse } from "../expressions.js";
2
+ import { boundAtRoot, boundWithin, expressionReads } from "../reads.js";
3
+ //#region src/definition/pins/references.ts
4
+ const inputRead = "$states.context.Execution.Input";
5
+ const stepsRead = "$states.context.Steps";
6
+ const plainKey = /^[A-Za-z_][A-Za-z0-9_]*$/;
7
+ const segment = /^\.(?:([A-Za-z_][A-Za-z0-9_]*)|`([^`]+)`)/;
8
+ const pathOf = (rest) => {
9
+ const path = [];
10
+ let left = rest;
11
+ while (left !== "") {
12
+ const match = segment.exec(left);
13
+ if (!match) return;
14
+ path.push(match[1] ?? match[2]);
15
+ left = left.slice(match[0].length);
16
+ }
17
+ return path;
18
+ };
19
+ /**
20
+ * The read a value is, when it is an expression of one read by name, such as `{% $fetch.body.ip %}` or
21
+ * `{% $states.context.Execution.Input.record.name %}`; undefined for any other value, such as text, an expression that
22
+ * computes, or `$states.result`.
23
+ */
24
+ const parseReference = (value) => {
25
+ const source = expressionSource(value)?.trim();
26
+ if (source === void 0) return;
27
+ if (source === inputRead || source.startsWith(`${inputRead}.`)) {
28
+ const path = pathOf(source.slice(31));
29
+ return path && {
30
+ base: { input: true },
31
+ path
32
+ };
33
+ }
34
+ if (source.startsWith(`${stepsRead}.`)) {
35
+ const path = pathOf(source.slice(21));
36
+ return path && path.length > 0 ? {
37
+ base: { step: path[0] },
38
+ path: path.slice(1)
39
+ } : void 0;
40
+ }
41
+ const variable = /^\$([A-Za-z_][A-Za-z0-9_]*)/.exec(source);
42
+ if (!variable || variable[1] === "states") return;
43
+ const path = pathOf(source.slice(variable[0].length));
44
+ return path && {
45
+ base: { variable: variable[1] },
46
+ path
47
+ };
48
+ };
49
+ const keyText = (key) => plainKey.test(key) && ![
50
+ "true",
51
+ "false",
52
+ "null"
53
+ ].includes(key) ? key : `\`${key}\``;
54
+ /** The JSONata read of a reference, such as `$fetch.body.ip`. */
55
+ const readText = ({ base, path }) => ["input" in base ? inputRead : "step" in base ? `${stepsRead}.${keyText(base.step)}` : `$${base.variable}`, ...path.map(keyText)].join(".");
56
+ /** A reference as the value of a field: `{% <read> %}`. */
57
+ const referenceValue = (reference) => `{% ${readText(reference)} %}`;
58
+ const isNode = (value) => typeof value === "object" && value !== null && typeof value.type === "string";
59
+ const namesAfter = (steps) => {
60
+ const names = [];
61
+ for (const step of steps) {
62
+ if (step.type !== "name" || step.stages !== void 0 || step.predicate !== void 0) break;
63
+ names.push(String(step.value));
64
+ }
65
+ return names;
66
+ };
67
+ const statesReference = (names) => {
68
+ if (names[0] !== "context") return;
69
+ if (names[1] === "Execution" && names[2] === "Input") return {
70
+ base: { input: true },
71
+ path: names.slice(3)
72
+ };
73
+ if (names[1] === "Steps" && names[2] !== void 0) return {
74
+ base: { step: names[2] },
75
+ path: names.slice(3)
76
+ };
77
+ };
78
+ /**
79
+ * Every read by name in a value's expression, however the expression combines them, such as `$fetch.body.ip` and
80
+ * `$uuid.body.uuid` in `{% $fetch.body.ip & ' / ' & $uuid.body.uuid %}`: each where it starts and the plain names it
81
+ * goes down. A name the expression binds itself, and JSONata's functions, are no reads. None for a value that is no
82
+ * expression or does not parse.
83
+ */
84
+ const expressionReferences = (value) => {
85
+ const source = expressionSource(value);
86
+ if (source === void 0) return [];
87
+ const parsed = parse(source);
88
+ if ("error" in parsed) return [];
89
+ const free = expressionReads(parsed.expression).variables;
90
+ const found = /* @__PURE__ */ new Map();
91
+ const add = (reference) => reference && found.set(readText(reference), reference);
92
+ const inPath = /* @__PURE__ */ new WeakSet();
93
+ const visit = (node, outer) => {
94
+ if (Array.isArray(node)) return node.forEach((item) => visit(item, outer));
95
+ if (!isNode(node)) return;
96
+ const bound = boundWithin(node, outer);
97
+ const workflows = (name) => free.has(name) && !bound.has(name);
98
+ if (node.type === "path" && Array.isArray(node.steps) && node.steps[0]?.type === "variable") {
99
+ const [first, ...rest] = node.steps;
100
+ inPath.add(first);
101
+ const name = String(first.value);
102
+ if (name === "states") add(statesReference(namesAfter(rest)));
103
+ else if (workflows(name)) add({
104
+ base: { variable: name },
105
+ path: namesAfter(rest)
106
+ });
107
+ } else if (node.type === "variable" && !inPath.has(node) && workflows(String(node.value))) add({
108
+ base: { variable: String(node.value) },
109
+ path: []
110
+ });
111
+ for (const [key, child] of Object.entries(node)) if (key !== "position" && key !== "value" && key !== "type") visit(child, bound);
112
+ };
113
+ const ast = parsed.expression.ast();
114
+ visit(ast, boundAtRoot(ast));
115
+ return [...found.values()];
116
+ };
117
+ //#endregion
118
+ export { expressionReferences, parseReference, readText, referenceValue };
@@ -0,0 +1,21 @@
1
+ //#region src/definition/pins/resolve-schema.d.ts
2
+ type JsonSchema = Record<string, unknown>;
3
+ /** How a record is used: as a step gives it (`read`), or as a create or an update takes it (`write`). */
4
+ export type RecordUse = 'read' | 'write';
5
+ /** A resource's record as JSON Schema, for a use; undefined for a resource the app does not have. */
6
+ export type RecordSchemas = (resource: string, use: RecordUse) => JsonSchema | undefined;
7
+ /**
8
+ * A type's schema with what its arguments name filled in: `{ "x-record-of": "<argument>" }` is the record of the
9
+ * resource that argument names, for the `x-record-use` it says (default `read`), and `{ "x-schema-of": "<argument>" }`
10
+ * is the schema that argument holds, such as an HTTP answer's declared body, and `{ "x-one-or-many-of": "<argument>",
11
+ * "anyOf": [<one>, <list>] }` is one or the list as that argument holds an object or a list. An argument that is an
12
+ * expression, or a resource the app does not have, leaves the value of any type.
13
+ */
14
+ export declare const resolveSchema: (schema: JsonSchema, args: Record<string, unknown>, records: RecordSchemas) => JsonSchema;
15
+ /**
16
+ * The schema of the value at `path` below a value of `schema`: a property's, or a map's `additionalProperties`; for a
17
+ * schema of several kinds, any of the branches' schemas for it. Undefined when the schema, or one of its branches,
18
+ * does not say, so the value is of any type.
19
+ */
20
+ export declare const schemaAt: (schema: JsonSchema | undefined, path: readonly string[]) => JsonSchema | undefined;
21
+ //#endregion
@@ -0,0 +1,62 @@
1
+ //#region src/definition/pins/resolve-schema.ts
2
+ const isSchema = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
3
+ /**
4
+ * A type's schema with what its arguments name filled in: `{ "x-record-of": "<argument>" }` is the record of the
5
+ * resource that argument names, for the `x-record-use` it says (default `read`), and `{ "x-schema-of": "<argument>" }`
6
+ * is the schema that argument holds, such as an HTTP answer's declared body, and `{ "x-one-or-many-of": "<argument>",
7
+ * "anyOf": [<one>, <list>] }` is one or the list as that argument holds an object or a list. An argument that is an
8
+ * expression, or a resource the app does not have, leaves the value of any type.
9
+ */
10
+ const resolveSchema = (schema, args, records) => {
11
+ const resolve = (node) => {
12
+ if (Array.isArray(node)) return node.map(resolve);
13
+ if (!isSchema(node)) return node;
14
+ const recordOf = node["x-record-of"];
15
+ if (typeof recordOf === "string") {
16
+ const resource = args[recordOf];
17
+ const use = node["x-record-use"] === "write" ? "write" : "read";
18
+ return (typeof resource === "string" && !resource.startsWith("{%") ? records(resource, use) : void 0) ?? withoutKeys(node, ["x-record-of", "x-record-use"]);
19
+ }
20
+ const oneOrMany = node["x-one-or-many-of"];
21
+ if (typeof oneOrMany === "string" && Array.isArray(node.anyOf) && node.anyOf.length === 2) {
22
+ const given = args[oneOrMany];
23
+ if (Array.isArray(given)) return resolve(node.anyOf[1]);
24
+ if (isSchema(given)) return resolve(node.anyOf[0]);
25
+ return resolve(withoutKeys(node, ["x-one-or-many-of"]));
26
+ }
27
+ const schemaOf = node["x-schema-of"];
28
+ if (typeof schemaOf === "string") {
29
+ const given = args[schemaOf];
30
+ return isSchema(given) ? given : withoutKeys(node, ["x-schema-of"]);
31
+ }
32
+ return Object.fromEntries(Object.entries(node).map(([key, value]) => [key, resolve(value)]));
33
+ };
34
+ return resolve(schema);
35
+ };
36
+ const withoutKeys = (node, keys) => Object.fromEntries(Object.entries(node).filter(([key]) => !keys.includes(key)));
37
+ /**
38
+ * The schema of the value at `path` below a value of `schema`: a property's, or a map's `additionalProperties`; for a
39
+ * schema of several kinds, any of the branches' schemas for it. Undefined when the schema, or one of its branches,
40
+ * does not say, so the value is of any type.
41
+ */
42
+ const schemaAt = (schema, path) => {
43
+ let current = schema;
44
+ for (const key of path) {
45
+ if (!current) return;
46
+ current = fieldSchema(current, key);
47
+ }
48
+ return current;
49
+ };
50
+ const fieldSchema = (schema, key) => {
51
+ const branches = [schema.anyOf, schema.oneOf].find(Array.isArray);
52
+ if (branches) {
53
+ if (branches.some((branch) => !isSchema(branch))) return;
54
+ const found = branches.filter(isSchema).map((branch) => fieldSchema(branch, key));
55
+ if (found.length === 0 || found.some((each) => each === void 0)) return;
56
+ return found.length === 1 ? found[0] : { anyOf: found };
57
+ }
58
+ if (isSchema(schema.properties) && isSchema(schema.properties[key])) return schema.properties[key];
59
+ if (isSchema(schema.additionalProperties)) return schema.additionalProperties;
60
+ };
61
+ //#endregion
62
+ export { resolveSchema, schemaAt };
@@ -25,6 +25,21 @@ const boundIn = (value, names = /* @__PURE__ */ new Set(), root = true) => {
25
25
  for (const [key, child] of Object.entries(value)) if (key !== "position" && key !== "value" && key !== "type") boundIn(child, names, false);
26
26
  return names;
27
27
  };
28
+ /**
29
+ * The names bound inside a node of an expression's syntax tree, given those bound around it: a block or a function adds
30
+ * what it binds, and a function its parameters; any other node binds nothing of its own.
31
+ */
32
+ const boundWithin = (value, outer) => {
33
+ if (!isNode(value) || !opensScope(value)) return outer;
34
+ const parameters = value.type === "lambda" && Array.isArray(value.arguments) ? value.arguments.map((argument) => String(argument.value)) : [];
35
+ return /* @__PURE__ */ new Set([
36
+ ...outer,
37
+ ...parameters,
38
+ ...boundIn(value)
39
+ ]);
40
+ };
41
+ /** The names the whole expression binds outside any block or function in it. */
42
+ const boundAtRoot = (ast) => boundIn(ast);
28
43
  /** The reads of a compiled expression, from its syntax tree, each name read where it is bound counted as no read. */
29
44
  const expressionReads = (expression) => {
30
45
  const variables = /* @__PURE__ */ new Set();
@@ -34,15 +49,7 @@ const expressionReads = (expression) => {
34
49
  const visit = (value, rooted, outer) => {
35
50
  if (Array.isArray(value)) return value.forEach((item) => visit(item, rooted, outer));
36
51
  if (!isNode(value)) return;
37
- let bound = outer;
38
- if (opensScope(value)) {
39
- const parameters = value.type === "lambda" && Array.isArray(value.arguments) ? value.arguments.map((argument) => String(argument.value)) : [];
40
- bound = /* @__PURE__ */ new Set([
41
- ...outer,
42
- ...parameters,
43
- ...boundIn(value)
44
- ]);
45
- }
52
+ const bound = boundWithin(value, outer);
46
53
  if (value.type === "variable") {
47
54
  const name = String(value.value);
48
55
  if (name === "" || name === "$") input ||= rooted;
@@ -78,4 +85,4 @@ const expressionReads = (expression) => {
78
85
  };
79
86
  };
80
87
  //#endregion
81
- export { builtInFunctions, expressionReads };
88
+ export { boundAtRoot, boundWithin, builtInFunctions, expressionReads };