@skillstech/thunderlang 0.5.10 → 0.5.11

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/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ # 0.5.11
2
+
3
+ **Language: choices among cases, and inner functions, in the flow.**
4
+ - A `switch`, `when`, `match`, or `select` is one question with one side per case ("Chooses by score": "When it is
5
+ from 90 to 100", …, "Otherwise"), not a yes/no question. Flow blocks carry `cases: [{ label, blocks }]`, the
6
+ decision node `cases: true`, and its edges `label: 'case'` with the case's `text` (the edge into an inner function's steps is `label: 'nested'`). Without an Otherwise, "None of
7
+ these" carries straight on.
8
+ - An inner function (a React hook, a callback, `go`/`defer`, a closure) is its own sub-flow (`nested: true` on its
9
+ block): a return or stop inside it ends only that function, and the outer flow carries on from the step. Details no
10
+ longer lists its returns and stops as the outer action's endings.
11
+ - Destructured parameters (React props `{ userId, name }`) are what goes in: "user id, name".
12
+ - SQL is in the reader on thunderlang.dev/read, with a sample; pasted SQL is recognized.
13
+
1
14
  # 0.5.10
2
15
 
3
16
  **Language: SQL.** Thunder reads `.sql` files: tables become Data or Things (columns in words, defaults, always
package/dist/core.cjs CHANGED
@@ -4471,7 +4471,7 @@ function notesSummary(ast) {
4471
4471
  byLens
4472
4472
  };
4473
4473
  }
4474
- var COMPILER_VERSION = "0.5.10";
4474
+ var COMPILER_VERSION = "0.5.11";
4475
4475
  var PROOF_SCHEMA_VERSION = "0.1.0";
4476
4476
  var SOURCE_PRODUCT = "skillstech-compiler";
4477
4477
  function buildContractGraph(ast, generatedAt) {
@@ -5748,6 +5748,7 @@ function liftDiagnostics(lift, facts) {
5748
5748
  return d;
5749
5749
  }
5750
5750
  function languageForFile(file) {
5751
+ if (/\.sql$/i.test(file)) return "sql";
5751
5752
  if (/\.rs$/i.test(file)) return "rust";
5752
5753
  if (/\.(pl|pm|t)$/i.test(file)) return "perl";
5753
5754
  if (/\.pyi?$/i.test(file)) return "python";
@@ -5893,6 +5894,7 @@ var KEYWORDS_NOT_FN = /* @__PURE__ */ new Set(["if", "for", "while", "switch", "
5893
5894
  function detectLanguage(source, file = "") {
5894
5895
  if (file) return languageForFile(file);
5895
5896
  const s = String(source);
5897
+ if (/^\s*(?:--[^\n]*\n\s*)*(?:CREATE|ALTER|INSERT\s+INTO|SELECT|WITH)\b/i.test(s) && /\b(?:CREATE\s+(?:OR\s+REPLACE\s+)?(?:TABLE|VIEW|FUNCTION|PROCEDURE|TRIGGER|TYPE|INDEX)|SELECT\b[\s\S]*\bFROM)\b/i.test(s)) return "sql";
5896
5898
  if (/^\s*<\?php\b/.test(s) || /\bfunction\s+\w+\s*\([^)]*\$\w+/.test(s)) return "php";
5897
5899
  if (/\bfun\s+(<[^>]*>\s*)?[\w.]*\w+\s*\(/.test(s)) return "kotlin";
5898
5900
  if (/^\s*def\s+[\w.?!]+(\s*\(.*\))?\s*$/m.test(s) && /^\s*end\s*$/m.test(s)) return "ruby";
@@ -9840,7 +9842,7 @@ function flowOf(steps, { start = "Start", memberIndex = /* @__PURE__ */ new Map(
9840
9842
  return id2;
9841
9843
  };
9842
9844
  const connect = (open2, to) => {
9843
- for (const o of open2) edges.push({ from: o.id, to, ...o.label ? { label: o.label } : {} });
9845
+ for (const o of open2) edges.push({ from: o.id, to, ...o.label ? { label: o.label } : {}, ...o.text ? { text: o.text } : {} });
9844
9846
  };
9845
9847
  const usesOf = (st) => {
9846
9848
  for (const c of String(st.code || "").matchAll(/(?:\b(?:self|this)\.)?\b([A-Za-z_]\w*)\s*\(/g)) {
@@ -9854,6 +9856,23 @@ function flowOf(steps, { start = "Start", memberIndex = /* @__PURE__ */ new Map(
9854
9856
  for (const st of list) {
9855
9857
  const does = stepDoes(st);
9856
9858
  const base = { line: st.line, lane, does, ...usesOf(st) ? { uses: usesOf(st) } : {} };
9859
+ const arms = st.kind === "branch" && st.children?.length && st.children.every((c) => c.kind === "check") ? st.children : null;
9860
+ if (arms) {
9861
+ const d = node2("decision", st.says.replace(/\?$/, ""), { ...base, cases: true });
9862
+ connect(open2, d);
9863
+ const cases = [];
9864
+ let after = [];
9865
+ for (const arm of arms) {
9866
+ const label = arm.says;
9867
+ const w = walk(arm.children || [], [{ id: d, label: "case", text: label }], lane + 1);
9868
+ cases.push({ label, line: arm.line, blocks: w.blocks });
9869
+ after = [...after, ...w.open];
9870
+ }
9871
+ if (!arms.some((a) => /^(Otherwise|If none is ready yet|Default)\b/i.test(a.says))) after.push({ id: d, label: "case", text: "None of these" });
9872
+ blocks3.push({ id: d, cases });
9873
+ open2 = after;
9874
+ continue;
9875
+ }
9857
9876
  if (st.kind === "branch") {
9858
9877
  const { text, flip } = askAsQuestion(st, base.uses && memberLabel.get(base.uses));
9859
9878
  const [into, past] = flip ? ["no", "yes"] : ["yes", "no"];
@@ -9907,7 +9926,10 @@ function flowOf(steps, { start = "Start", memberIndex = /* @__PURE__ */ new Map(
9907
9926
  const k = node2(does === "code" ? "code" : "step", does === "code" ? st.code ?? st.says : st.says, base);
9908
9927
  connect(open2, k);
9909
9928
  open2 = [{ id: k }];
9910
- if (st.children?.length) {
9929
+ if (st.children?.length && st.nested) {
9930
+ const inner = walk(st.children, [{ id: k, label: "nested" }], lane + 1);
9931
+ blocks3.push({ id: k, body: inner.blocks, nested: true });
9932
+ } else if (st.children?.length) {
9911
9933
  const inner = walk(st.children, open2, lane + 1);
9912
9934
  blocks3.push({ id: k, body: inner.blocks });
9913
9935
  open2 = inner.open;
@@ -9926,7 +9948,7 @@ function flowOf(steps, { start = "Start", memberIndex = /* @__PURE__ */ new Map(
9926
9948
  }
9927
9949
  var idOf = (...parts) => parts.join("#").replace(/\s+/g, "");
9928
9950
  function youGiveOf(item2) {
9929
- const given = (item2.inputs || []).filter((p) => !/^(self|this|cls)$/.test(p.name) && !p.plumbing).map((p) => words6(p.name));
9951
+ const given = (item2.inputs || []).filter((p) => !/^(self|this|cls)$/.test(p.name) && !p.plumbing).flatMap((p) => /^\{.*\}$/.test(String(p.name).trim()) ? String(p.name).trim().slice(1, -1).split(",").map((x) => x.split(/[:=]/)[0].trim().replace(/^\.\.\./, "")).filter((x) => /^[A-Za-z_$][\w$]*$/.test(x)) : [p.name]).map((n) => words6(n));
9930
9952
  const codes = [];
9931
9953
  const walk = (steps) => {
9932
9954
  for (const s of steps || []) {
@@ -10040,7 +10062,7 @@ function flowLines(flow, depth = 1) {
10040
10062
  const out = [];
10041
10063
  const pad = (d) => " ".repeat(d);
10042
10064
  const at = (n) => n.line ? ` (line ${n.line})` : "";
10043
- const say = (n) => n.kind === "code" ? `\`${n.text}\` (code, not explained)` : n.kind === "decision" ? `Ask: ${n.text}` : n.kind === "end" && n.answer ? `Answer: ${n.text}` : n.text;
10065
+ const say = (n) => n.kind === "code" ? `\`${n.text}\` (code, not explained)` : n.kind === "decision" && n.cases ? n.text : n.kind === "decision" ? `Ask: ${n.text}` : n.kind === "end" && n.answer ? `Answer: ${n.text}` : n.text;
10044
10066
  const go = (blocks2, d) => {
10045
10067
  for (const b of blocks2 || []) {
10046
10068
  const n = byId.get(b.id);
@@ -10054,6 +10076,12 @@ function flowLines(flow, depth = 1) {
10054
10076
  } else out.push(`${pad(d + 1)}${side === "yes" ? "Yes" : "No"}: continue`);
10055
10077
  }
10056
10078
  }
10079
+ for (const c of b.cases || []) {
10080
+ if (c.blocks.length) {
10081
+ out.push(`${pad(d + 1)}${c.label}:`);
10082
+ go(c.blocks, d + 2);
10083
+ } else out.push(`${pad(d + 1)}${c.label}: nothing`);
10084
+ }
10057
10085
  if (b.each) {
10058
10086
  out.push(`${pad(d + 1)}For each:`);
10059
10087
  go(b.each, d + 2);
@@ -10116,6 +10144,7 @@ function detailSteps(flow) {
10116
10144
  walk(b.yes, depth + 1, "Yes");
10117
10145
  walk(b.no, depth + 1, "No");
10118
10146
  walk(b.each, depth + 1, "For each");
10147
+ for (const c of b.cases || []) walk(c.blocks, depth + 1, c.label);
10119
10148
  walk(b.body, depth + 1, null);
10120
10149
  for (const h of b.handlers || []) {
10121
10150
  const hn = byId.get(h.id);
@@ -10148,7 +10177,8 @@ function pathsOf(flow) {
10148
10177
  walk(b.yes, [...when, { question: q, answer: "Yes" }]);
10149
10178
  walk(b.no, [...when, { question: q, answer: "No" }]);
10150
10179
  walk(b.each, [...when, { question: q, answer: "For each" }]);
10151
- walk(b.body, when);
10180
+ for (const c of b.cases || []) walk(c.blocks, [...when, { question: q, answer: c.label }]);
10181
+ if (!b.nested) walk(b.body, when);
10152
10182
  for (const h of b.handlers || []) {
10153
10183
  const hn = byId.get(h.id);
10154
10184
  walk(h.blocks, [...when, { question: (hn?.text ?? "").replace(/\?$/, ""), answer: "Yes" }]);
package/dist/index.cjs CHANGED
@@ -2423,7 +2423,7 @@ function notesSummary(ast) {
2423
2423
  byLens
2424
2424
  };
2425
2425
  }
2426
- var COMPILER_VERSION = "0.5.10";
2426
+ var COMPILER_VERSION = "0.5.11";
2427
2427
  var PROOF_SCHEMA_VERSION = "0.1.0";
2428
2428
  var SOURCE_PRODUCT = "skillstech-compiler";
2429
2429
  function buildContractGraph(ast, generatedAt) {
@@ -4164,6 +4164,7 @@ function liftDiagnostics(lift, facts) {
4164
4164
  return d;
4165
4165
  }
4166
4166
  function languageForFile(file) {
4167
+ if (/\.sql$/i.test(file)) return "sql";
4167
4168
  if (/\.rs$/i.test(file)) return "rust";
4168
4169
  if (/\.(pl|pm|t)$/i.test(file)) return "perl";
4169
4170
  if (/\.pyi?$/i.test(file)) return "python";
@@ -10751,6 +10752,7 @@ var KEYWORDS_NOT_FN = /* @__PURE__ */ new Set(["if", "for", "while", "switch", "
10751
10752
  function detectLanguage(source, file = "") {
10752
10753
  if (file) return languageForFile(file);
10753
10754
  const s = String(source);
10755
+ if (/^\s*(?:--[^\n]*\n\s*)*(?:CREATE|ALTER|INSERT\s+INTO|SELECT|WITH)\b/i.test(s) && /\b(?:CREATE\s+(?:OR\s+REPLACE\s+)?(?:TABLE|VIEW|FUNCTION|PROCEDURE|TRIGGER|TYPE|INDEX)|SELECT\b[\s\S]*\bFROM)\b/i.test(s)) return "sql";
10754
10756
  if (/^\s*<\?php\b/.test(s) || /\bfunction\s+\w+\s*\([^)]*\$\w+/.test(s)) return "php";
10755
10757
  if (/\bfun\s+(<[^>]*>\s*)?[\w.]*\w+\s*\(/.test(s)) return "kotlin";
10756
10758
  if (/^\s*def\s+[\w.?!]+(\s*\(.*\))?\s*$/m.test(s) && /^\s*end\s*$/m.test(s)) return "ruby";
package/dist/learn.cjs CHANGED
@@ -2920,7 +2920,7 @@ var SEMANTIC_TYPES = Object.keys(SEMANTIC);
2920
2920
  var PRIMITIVE_TYPES = Object.keys(PRIMITIVE);
2921
2921
 
2922
2922
  // src/emit.mjs
2923
- var COMPILER_VERSION = "0.5.10";
2923
+ var COMPILER_VERSION = "0.5.11";
2924
2924
 
2925
2925
  // src/lift.mjs
2926
2926
  var IR_SCHEMA_VERSION = "0.1.0";
@@ -3471,6 +3471,7 @@ function extractFacts(source, language, file) {
3471
3471
  return adapter ? adapter(String(source), file || `input.${LANG_EXT[key] || "txt"}`) : null;
3472
3472
  }
3473
3473
  function languageForFile(file) {
3474
+ if (/\.sql$/i.test(file)) return "sql";
3474
3475
  if (/\.rs$/i.test(file)) return "rust";
3475
3476
  if (/\.(pl|pm|t)$/i.test(file)) return "perl";
3476
3477
  if (/\.pyi?$/i.test(file)) return "python";
@@ -3510,6 +3511,7 @@ var KEYWORDS_NOT_FN = /* @__PURE__ */ new Set(["if", "for", "while", "switch", "
3510
3511
  function detectLanguage(source, file = "") {
3511
3512
  if (file) return languageForFile(file);
3512
3513
  const s = String(source);
3514
+ if (/^\s*(?:--[^\n]*\n\s*)*(?:CREATE|ALTER|INSERT\s+INTO|SELECT|WITH)\b/i.test(s) && /\b(?:CREATE\s+(?:OR\s+REPLACE\s+)?(?:TABLE|VIEW|FUNCTION|PROCEDURE|TRIGGER|TYPE|INDEX)|SELECT\b[\s\S]*\bFROM)\b/i.test(s)) return "sql";
3513
3515
  if (/^\s*<\?php\b/.test(s) || /\bfunction\s+\w+\s*\([^)]*\$\w+/.test(s)) return "php";
3514
3516
  if (/\bfun\s+(<[^>]*>\s*)?[\w.]*\w+\s*\(/.test(s)) return "kotlin";
3515
3517
  if (/^\s*def\s+[\w.?!]+(\s*\(.*\))?\s*$/m.test(s) && /^\s*end\s*$/m.test(s)) return "ruby";
@@ -6526,7 +6528,7 @@ function flowOf(steps, { start = "Start", memberIndex = /* @__PURE__ */ new Map(
6526
6528
  return id;
6527
6529
  };
6528
6530
  const connect = (open2, to) => {
6529
- for (const o of open2) edges.push({ from: o.id, to, ...o.label ? { label: o.label } : {} });
6531
+ for (const o of open2) edges.push({ from: o.id, to, ...o.label ? { label: o.label } : {}, ...o.text ? { text: o.text } : {} });
6530
6532
  };
6531
6533
  const usesOf = (st) => {
6532
6534
  for (const c of String(st.code || "").matchAll(/(?:\b(?:self|this)\.)?\b([A-Za-z_]\w*)\s*\(/g)) {
@@ -6540,6 +6542,23 @@ function flowOf(steps, { start = "Start", memberIndex = /* @__PURE__ */ new Map(
6540
6542
  for (const st of list) {
6541
6543
  const does = stepDoes(st);
6542
6544
  const base = { line: st.line, lane, does, ...usesOf(st) ? { uses: usesOf(st) } : {} };
6545
+ const arms = st.kind === "branch" && st.children?.length && st.children.every((c) => c.kind === "check") ? st.children : null;
6546
+ if (arms) {
6547
+ const d = node("decision", st.says.replace(/\?$/, ""), { ...base, cases: true });
6548
+ connect(open2, d);
6549
+ const cases = [];
6550
+ let after = [];
6551
+ for (const arm of arms) {
6552
+ const label = arm.says;
6553
+ const w = walk(arm.children || [], [{ id: d, label: "case", text: label }], lane + 1);
6554
+ cases.push({ label, line: arm.line, blocks: w.blocks });
6555
+ after = [...after, ...w.open];
6556
+ }
6557
+ if (!arms.some((a) => /^(Otherwise|If none is ready yet|Default)\b/i.test(a.says))) after.push({ id: d, label: "case", text: "None of these" });
6558
+ blocks2.push({ id: d, cases });
6559
+ open2 = after;
6560
+ continue;
6561
+ }
6543
6562
  if (st.kind === "branch") {
6544
6563
  const { text, flip } = askAsQuestion(st, base.uses && memberLabel.get(base.uses));
6545
6564
  const [into, past] = flip ? ["no", "yes"] : ["yes", "no"];
@@ -6593,7 +6612,10 @@ function flowOf(steps, { start = "Start", memberIndex = /* @__PURE__ */ new Map(
6593
6612
  const k = node(does === "code" ? "code" : "step", does === "code" ? st.code ?? st.says : st.says, base);
6594
6613
  connect(open2, k);
6595
6614
  open2 = [{ id: k }];
6596
- if (st.children?.length) {
6615
+ if (st.children?.length && st.nested) {
6616
+ const inner = walk(st.children, [{ id: k, label: "nested" }], lane + 1);
6617
+ blocks2.push({ id: k, body: inner.blocks, nested: true });
6618
+ } else if (st.children?.length) {
6597
6619
  const inner = walk(st.children, open2, lane + 1);
6598
6620
  blocks2.push({ id: k, body: inner.blocks });
6599
6621
  open2 = inner.open;
@@ -6612,7 +6634,7 @@ function flowOf(steps, { start = "Start", memberIndex = /* @__PURE__ */ new Map(
6612
6634
  }
6613
6635
  var idOf = (...parts) => parts.join("#").replace(/\s+/g, "");
6614
6636
  function youGiveOf(item) {
6615
- const given = (item.inputs || []).filter((p) => !/^(self|this|cls)$/.test(p.name) && !p.plumbing).map((p) => words4(p.name));
6637
+ const given = (item.inputs || []).filter((p) => !/^(self|this|cls)$/.test(p.name) && !p.plumbing).flatMap((p) => /^\{.*\}$/.test(String(p.name).trim()) ? String(p.name).trim().slice(1, -1).split(",").map((x) => x.split(/[:=]/)[0].trim().replace(/^\.\.\./, "")).filter((x) => /^[A-Za-z_$][\w$]*$/.test(x)) : [p.name]).map((n) => words4(n));
6616
6638
  const codes = [];
6617
6639
  const walk = (steps) => {
6618
6640
  for (const s of steps || []) {
@@ -6726,7 +6748,7 @@ function flowLines(flow, depth = 1) {
6726
6748
  const out = [];
6727
6749
  const pad = (d) => " ".repeat(d);
6728
6750
  const at = (n) => n.line ? ` (line ${n.line})` : "";
6729
- const say = (n) => n.kind === "code" ? `\`${n.text}\` (code, not explained)` : n.kind === "decision" ? `Ask: ${n.text}` : n.kind === "end" && n.answer ? `Answer: ${n.text}` : n.text;
6751
+ const say = (n) => n.kind === "code" ? `\`${n.text}\` (code, not explained)` : n.kind === "decision" && n.cases ? n.text : n.kind === "decision" ? `Ask: ${n.text}` : n.kind === "end" && n.answer ? `Answer: ${n.text}` : n.text;
6730
6752
  const go = (blocks, d) => {
6731
6753
  for (const b of blocks || []) {
6732
6754
  const n = byId.get(b.id);
@@ -6740,6 +6762,12 @@ function flowLines(flow, depth = 1) {
6740
6762
  } else out.push(`${pad(d + 1)}${side === "yes" ? "Yes" : "No"}: continue`);
6741
6763
  }
6742
6764
  }
6765
+ for (const c of b.cases || []) {
6766
+ if (c.blocks.length) {
6767
+ out.push(`${pad(d + 1)}${c.label}:`);
6768
+ go(c.blocks, d + 2);
6769
+ } else out.push(`${pad(d + 1)}${c.label}: nothing`);
6770
+ }
6743
6771
  if (b.each) {
6744
6772
  out.push(`${pad(d + 1)}For each:`);
6745
6773
  go(b.each, d + 2);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skillstech/thunderlang",
3
- "version": "0.5.10",
3
+ "version": "0.5.11",
4
4
  "description": "Deterministic ThunderLang compiler, CLI, and Language Server. No AI required. Turns .thunder files into the canonical Intent Graph, docs, and a proof, and EXECUTES the intent: run decisions, simulate lifecycles, and check outcome contracts. Interops with DMN, BPMN, JSON Schema, and OpenAPI.",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
package/src/emit.mjs CHANGED
@@ -30,7 +30,7 @@ export function notesSummary(ast) {
30
30
  };
31
31
  }
32
32
 
33
- export const COMPILER_VERSION = '0.5.10';
33
+ export const COMPILER_VERSION = '0.5.11';
34
34
  export const PROOF_SCHEMA_VERSION = '0.1.0';
35
35
  // Identifies which ecosystem product emitted this proof. Consumed by SkillsTech
36
36
  // Certified to key cert proofs to the compiler. Stable slug per the coordination bus.
package/src/lift.mjs CHANGED
@@ -628,6 +628,7 @@ function liftDiagnostics(lift, facts) {
628
628
  * repo-level summary matching the `thunder lift --from repo --json` contract.
629
629
  */
630
630
  export function languageForFile(file) {
631
+ if (/\.sql$/i.test(file)) return 'sql';
631
632
  if (/\.rs$/i.test(file)) return 'rust';
632
633
  if (/\.(pl|pm|t)$/i.test(file)) return 'perl';
633
634
  if (/\.pyi?$/i.test(file)) return 'python';
package/src/model.d.mts CHANGED
@@ -2,9 +2,13 @@
2
2
  import type { Does } from './visual.mjs';
3
3
  export const MODEL_SCHEMA: 'thunder-model-v1';
4
4
  export const THING_LANGUAGES: string[];
5
- export interface FlowNode { id: string; kind: 'start' | 'decision' | 'step' | 'loop' | 'stop' | 'end' | 'code'; text: string; line?: number; lane: number; row: number; does: Does; uses?: string; answer?: boolean; otherwise?: boolean; value?: string; code?: boolean }
6
- export interface FlowEdge { from: string; to: string; label?: 'yes' | 'no' | 'each' | 'next' | 'done' | 'error' }
7
- export interface FlowBlock { id: string; yes?: FlowBlock[]; no?: FlowBlock[]; each?: FlowBlock[]; body?: FlowBlock[]; handlers?: { id: string; blocks: FlowBlock[] }[]; always?: { id: string; blocks: FlowBlock[] } }
5
+ export interface FlowNode { id: string; kind: 'start' | 'decision' | 'step' | 'loop' | 'stop' | 'end' | 'code'; text: string; line?: number; lane: number; row: number; does: Does; uses?: string; answer?: boolean; otherwise?: boolean; value?: string; code?: boolean; /** A decision with several cases (see FlowBlock.cases). */ cases?: true }
6
+ export interface FlowEdge { from: string; to: string; label?: 'yes' | 'no' | 'each' | 'next' | 'done' | 'error' | 'case' | 'nested'; /** On a 'case' edge: the case it takes ("When it is from 90 to 100", "Otherwise", "None of these"). */ text?: string }
7
+ export interface FlowBlock { id: string; yes?: FlowBlock[]; no?: FlowBlock[]; each?: FlowBlock[]; body?: FlowBlock[];
8
+ /** A choice among several cases (switch, when, match, select): one side per case, in order. The decision node has `cases: true`. */
9
+ cases?: { label: string; line?: number; blocks: FlowBlock[] }[];
10
+ /** The body is an inner function (a hook, a callback, go/defer): its own sub-flow; the outer flow continues from this step. */
11
+ nested?: true; handlers?: { id: string; blocks: FlowBlock[] }[]; always?: { id: string; blocks: FlowBlock[] } }
8
12
  /** nodes/edges: the flow as a graph. blocks: the same nodes as a tree, which views draw from. */
9
13
  export interface Flow { nodes: FlowNode[]; edges: FlowEdge[]; blocks: FlowBlock[] }
10
14
  export interface Thing { id: string; kind: 'thing' | 'record' | 'choices'; name: string; label: string; doc?: string; file: string; lines: [number, number]; knows: { name: string; label: string; kind: string | null; start: string | null; line: number | null }[]; choices: string[]; can: string[] }
package/src/model.mjs CHANGED
@@ -253,7 +253,7 @@ export function flowOf(steps, { start = 'Start', memberIndex = new Map(), member
253
253
  const nodes = [], edges = [];
254
254
  let n = 0;
255
255
  const node = (kind, text, extra = {}) => { const id = `n${n++}`; nodes.push({ id, kind, text, ...extra }); return id; };
256
- const connect = (open, to) => { for (const o of open) edges.push({ from: o.id, to, ...(o.label ? { label: o.label } : {}) }); };
256
+ const connect = (open, to) => { for (const o of open) edges.push({ from: o.id, to, ...(o.label ? { label: o.label } : {}), ...(o.text ? { text: o.text } : {}) }); };
257
257
  // The first call in the step to another member of this file: self.can_borrow(...), delivery_fee(...), this.lateFee(...).
258
258
  const usesOf = (st) => {
259
259
  for (const c of String(st.code || '').matchAll(/(?:\b(?:self|this)\.)?\b([A-Za-z_]\w*)\s*\(/g)) {
@@ -270,6 +270,25 @@ export function flowOf(steps, { start = 'Start', memberIndex = new Map(), member
270
270
  for (const st of list) {
271
271
  const does = stepDoes(st);
272
272
  const base = { line: st.line, lane, does, ...(usesOf(st) ? { uses: usesOf(st) } : {}) };
273
+ // A choice among several cases (switch, when, match, select): one question with one side per case.
274
+ const arms = st.kind === 'branch' && st.children?.length && st.children.every((c) => c.kind === 'check') ? st.children : null;
275
+ if (arms) {
276
+ const d = node('decision', st.says.replace(/\?$/, ''), { ...base, cases: true });
277
+ connect(open, d);
278
+ const cases = [];
279
+ let after = [];
280
+ for (const arm of arms) {
281
+ const label = arm.says;
282
+ const w = walk(arm.children || [], [{ id: d, label: 'case', text: label }], lane + 1);
283
+ cases.push({ label, line: arm.line, blocks: w.blocks });
284
+ after = [...after, ...w.open];
285
+ }
286
+ // With no "Otherwise", a value that matches no case carries straight on.
287
+ if (!arms.some((a) => /^(Otherwise|If none is ready yet|Default)\b/i.test(a.says))) after.push({ id: d, label: 'case', text: 'None of these' });
288
+ blocks.push({ id: d, cases });
289
+ open = after;
290
+ continue;
291
+ }
273
292
  if (st.kind === 'branch') {
274
293
  const { text, flip } = askAsQuestion(st, base.uses && memberLabel.get(base.uses));
275
294
  const [into, past] = flip ? ['no', 'yes'] : ['yes', 'no'];
@@ -325,7 +344,12 @@ export function flowOf(steps, { start = 'Start', memberIndex = new Map(), member
325
344
  const k = node(does === 'code' ? 'code' : 'step', does === 'code' ? st.code ?? st.says : st.says, base);
326
345
  connect(open, k);
327
346
  open = [{ id: k }];
328
- if (st.children?.length) {
347
+ if (st.children?.length && st.nested) {
348
+ // An inner function (a hook, a callback, go/defer, a closure) runs on its own: its steps are its own
349
+ // sub-flow, and a return or stop inside it ends only that function. The outer flow carries on from here.
350
+ const inner = walk(st.children, [{ id: k, label: 'nested' }], lane + 1);
351
+ blocks.push({ id: k, body: inner.blocks, nested: true });
352
+ } else if (st.children?.length) {
329
353
  const inner = walk(st.children, open, lane + 1);
330
354
  blocks.push({ id: k, body: inner.blocks });
331
355
  open = inner.open;
@@ -356,7 +380,10 @@ const idOf = (...parts) => parts.join('#').replace(/\s+/g, '');
356
380
  * `const { id } = req.params`, `{ email } = await request.json()`.
357
381
  */
358
382
  function youGiveOf(item) {
359
- const given = (item.inputs || []).filter((p) => !/^(self|this|cls)$/.test(p.name) && !p.plumbing).map((p) => words(p.name));
383
+ const given = (item.inputs || []).filter((p) => !/^(self|this|cls)$/.test(p.name) && !p.plumbing)
384
+ // Destructured parameters (React props `{ userId, name }`) give each name.
385
+ .flatMap((p) => (/^\{.*\}$/.test(String(p.name).trim()) ? String(p.name).trim().slice(1, -1).split(',').map((x) => x.split(/[:=]/)[0].trim().replace(/^\.\.\./, '')).filter((x) => /^[A-Za-z_$][\w$]*$/.test(x)) : [p.name]))
386
+ .map((n) => words(n));
360
387
  const codes = [];
361
388
  const walk = (steps) => { for (const s of steps || []) { if (s.code) codes.push(s.code); walk(s.children); walk(s.otherwise); } };
362
389
  walk(item.steps);
@@ -445,7 +472,7 @@ function flowLines(flow, depth = 1) {
445
472
  const out = [];
446
473
  const pad = (d) => ' '.repeat(d);
447
474
  const at = (n) => (n.line ? ` (line ${n.line})` : '');
448
- const say = (n) => (n.kind === 'code' ? `\`${n.text}\` (code, not explained)` : n.kind === 'decision' ? `Ask: ${n.text}` : n.kind === 'end' && n.answer ? `Answer: ${n.text}` : n.text);
475
+ const say = (n) => (n.kind === 'code' ? `\`${n.text}\` (code, not explained)` : n.kind === 'decision' && n.cases ? n.text : n.kind === 'decision' ? `Ask: ${n.text}` : n.kind === 'end' && n.answer ? `Answer: ${n.text}` : n.text);
449
476
  const go = (blocks, d) => {
450
477
  for (const b of blocks || []) {
451
478
  const n = byId.get(b.id);
@@ -457,6 +484,10 @@ function flowLines(flow, depth = 1) {
457
484
  else out.push(`${pad(d + 1)}${side === 'yes' ? 'Yes' : 'No'}: continue`);
458
485
  }
459
486
  }
487
+ for (const c of b.cases || []) {
488
+ if (c.blocks.length) { out.push(`${pad(d + 1)}${c.label}:`); go(c.blocks, d + 2); }
489
+ else out.push(`${pad(d + 1)}${c.label}: nothing`);
490
+ }
460
491
  if (b.each) { out.push(`${pad(d + 1)}For each:`); go(b.each, d + 2); }
461
492
  if (b.body) go(b.body, d + 1);
462
493
  for (const h of b.handlers || []) { out.push(`${pad(d + 1)}${byId.get(h.id).text.replace(/\?$/, '')}:`); go(h.blocks, d + 2); }
@@ -510,6 +541,7 @@ function detailSteps(flow) {
510
541
  walk(b.yes, depth + 1, 'Yes');
511
542
  walk(b.no, depth + 1, 'No');
512
543
  walk(b.each, depth + 1, 'For each');
544
+ for (const c of b.cases || []) walk(c.blocks, depth + 1, c.label);
513
545
  walk(b.body, depth + 1, null);
514
546
  for (const h of b.handlers || []) { const hn = byId.get(h.id); if (hn) push(hn, depth, null, hn.text.replace(/\?$/, '')); walk(h.blocks, depth + 1, null); }
515
547
  if (b.always) { const an = byId.get(b.always.id); if (an) push(an, depth, null); walk(b.always.blocks, depth + 1, null); }
@@ -537,7 +569,9 @@ function pathsOf(flow) {
537
569
  walk(b.yes, [...when, { question: q, answer: 'Yes' }]);
538
570
  walk(b.no, [...when, { question: q, answer: 'No' }]);
539
571
  walk(b.each, [...when, { question: q, answer: 'For each' }]);
540
- walk(b.body, when);
572
+ for (const c of b.cases || []) walk(c.blocks, [...when, { question: q, answer: c.label }]);
573
+ // An inner function's steps end only that function: not one of this action's endings.
574
+ if (!b.nested) walk(b.body, when);
541
575
  for (const h of b.handlers || []) { const hn = byId.get(h.id); walk(h.blocks, [...when, { question: (hn?.text ?? '').replace(/\?$/, ''), answer: 'Yes' }]); }
542
576
  if (b.always) walk(b.always.blocks, when);
543
577
  // A question whose one side ends the path (a Stop or an answer) guards everything after it: what follows only
package/src/read.mjs CHANGED
@@ -23,6 +23,8 @@ const KEYWORDS_NOT_FN = new Set(['if', 'for', 'while', 'switch', 'catch', 'retur
23
23
  export function detectLanguage(source, file = '') {
24
24
  if (file) return languageForFile(file);
25
25
  const s = String(source);
26
+ // SQL: a file that starts with (or is mostly) schema statements.
27
+ if (/^\s*(?:--[^\n]*\n\s*)*(?:CREATE|ALTER|INSERT\s+INTO|SELECT|WITH)\b/i.test(s) && /\b(?:CREATE\s+(?:OR\s+REPLACE\s+)?(?:TABLE|VIEW|FUNCTION|PROCEDURE|TRIGGER|TYPE|INDEX)|SELECT\b[\s\S]*\bFROM)\b/i.test(s)) return 'sql';
26
28
  if (/^\s*<\?php\b/.test(s) || /\bfunction\s+\w+\s*\([^)]*\$\w+/.test(s)) return 'php';
27
29
  if (/\bfun\s+(<[^>]*>\s*)?[\w.]*\w+\s*\(/.test(s)) return 'kotlin';
28
30
  if (/^\s*def\s+[\w.?!]+(\s*\(.*\))?\s*$/m.test(s) && /^\s*end\s*$/m.test(s)) return 'ruby';