|
25 | 25 | * |
26 | 26 | * ## Why these are structural rather than registered |
27 | 27 | * |
28 | | - * A registered function component sees `content()` — rendered text. Matchers |
29 | | - * are structure: `value` is an expression prop, `template` is a prop-or-children |
30 | | - * pair, and each needs its own position for diagnostics. Only the expansion hook |
31 | | - * hands over the element itself, which is why `<Test>` and `<WhenPrompt>` are |
32 | | - * claimed the same way. Being claimed means being reserved: a repository file |
33 | | - * named `Answers.md` never stands in, and it could not implement matcher |
34 | | - * semantics if it did. |
| 28 | + * A function component sees `content()` — rendered text. Matchers are |
| 29 | + * structure: `value` is an expression prop, `template` is a prop-or-children |
| 30 | + * pair, and each needs its own position for diagnostics, and only the element |
| 31 | + * itself carries those. That is why these are structural rather than |
| 32 | + * components: being reserved means a repository file named `Answers.md` never |
| 33 | + * stands in, and it could not implement matcher semantics if it did. |
35 | 34 | * |
36 | | - * Partitioning before expanding is what the hook buys. Matchers are collected |
37 | | - * from the whole child list first, so where an `<Answer>` is written does not |
38 | | - * decide what it can answer; and "an `<Answers>` with no body" is a structural |
39 | | - * fact rather than a guess from empty rendered text. |
| 35 | + * Partitioning before expanding is what the structural dispatch buys. Matchers |
| 36 | + * are collected from the whole child list first, so where an `<Answer>` is |
| 37 | + * written does not decide what it can answer; and "an `<Answers>` with no body" |
| 38 | + * is a structural fact rather than a guess from empty rendered text. |
40 | 39 | * |
41 | 40 | * ## Selection |
42 | 41 | * |
|
65 | 64 | import { scoped } from "effection"; |
66 | 65 | import type { Operation } from "effection"; |
67 | 66 |
|
68 | | -import { Component, env, expandSegments, raise } from "./component-api.ts"; |
| 67 | +import { env, expandSegments, raise } from "./component-api.ts"; |
69 | 68 | import { renderSegments } from "./render.ts"; |
70 | 69 | import { evaluateExpression } from "./expand.ts"; |
71 | 70 | import { Elicitation } from "./elicitation-api.ts"; |
72 | 71 | import type { ElicitationRequest } from "./elicitation-api.ts"; |
73 | 72 | import { JsonParseError, parseJson } from "./json.ts"; |
74 | 73 | import { matchPrompt, parseTemplate } from "./template.ts"; |
75 | 74 | import type { ParsedTemplate } from "./template.ts"; |
76 | | -import type { ComponentElement, ComponentHandling, ErrorSegment, Json, Segment } from "./types.ts"; |
| 75 | +import type { ComponentElement, ErrorSegment, Json, Segment } from "./types.ts"; |
77 | 76 |
|
78 | 77 | const ANSWERS = "Answers"; |
79 | 78 | const ANSWER = "Answer"; |
@@ -106,44 +105,17 @@ function isBlankText(segment: Segment): boolean { |
106 | 105 | return segment.type === "text" && segment.content.trim() === ""; |
107 | 106 | } |
108 | 107 |
|
109 | | -/** |
110 | | - * Install the handler for both constructs. |
111 | | - * |
112 | | - * Offered every element, it claims only these two names and delegates the rest, |
113 | | - * so it composes with any other expansion support a host has installed. |
114 | | - */ |
115 | | -export function installAnswers(): Operation<void> { |
116 | | - return Component.around( |
117 | | - { |
118 | | - *expand([element], next) { |
119 | | - if (element.name === ANSWERS) { |
120 | | - return { segments: yield* expandAnswers(element) }; |
121 | | - } |
122 | | - if (element.name === ANSWER) { |
123 | | - // Reaching here means no enclosing `<Answers>` consumed it: a |
124 | | - // well-placed matcher is partitioned out and never expanded on its |
125 | | - // own. Same shape as `<Else>` outside `<If>`. |
126 | | - return { |
127 | | - segments: [ |
128 | | - yield* raise( |
129 | | - configError( |
130 | | - ANSWER, |
131 | | - "must be a direct child of <Answers>. It is reserved: it never resolves a " + |
132 | | - "component, and only the <Answers> it belongs to can read it.", |
133 | | - element, |
134 | | - ), |
135 | | - ), |
136 | | - ], |
137 | | - }; |
138 | | - } |
139 | | - return yield* next(element); |
140 | | - }, |
141 | | - }, |
142 | | - { at: "min" }, |
| 108 | +/** A `<Answer>` written outside the `<Answers>` that would have read it. */ |
| 109 | +export function strayAnswerError(element: ComponentElement): ErrorSegment { |
| 110 | + return configError( |
| 111 | + ANSWER, |
| 112 | + "must be a direct child of <Answers>. It is reserved: it never resolves a " + |
| 113 | + "component, and only the <Answers> it belongs to can read it.", |
| 114 | + element, |
143 | 115 | ); |
144 | 116 | } |
145 | 117 |
|
146 | | -function* expandAnswers(element: ComponentElement): Operation<Segment[]> { |
| 118 | +export function* expandAnswers(element: ComponentElement): Operation<Segment[]> { |
147 | 119 | for (const name of Object.keys({ ...element.props, ...element.expressions })) { |
148 | 120 | if (name !== "delegate") { |
149 | 121 | return [ |
|
0 commit comments