Skip to content

Commit 7a1bc24

Browse files
committed
🐛 fix: stop <If> from reporting selected-branch errors twice
An ErrorSegment passes through Component.raise exactly once, where it is created (spec §6.9). `<If>` broke that: the nested expandSegments() settled each error from the selected branch, and the dispatch branch then raised the returned segment again. A counting raise middleware saw `<Broken />` once inline and twice inside `<If condition={true}>`. Move the ownership split into expandIf: it reports the errors it creates itself — invalid condition, unknown prop, malformed `<Else>` — exactly once, and the dispatcher appends the selected branch's segments untouched, as the `<Content />` projection branch already does. A throwing policy still aborts at the first error, and an unselected branch stays entirely unobserved. IF50 is the regression: it fails against the previous commit and passes now. The same duplicate observation exists in the `<Each>`, `<Capture>`, and ordinary component paths. That is pre-existing and tracked by #220 rather than repaired here.
1 parent 20d2b29 commit 7a1bc24

3 files changed

Lines changed: 128 additions & 16 deletions

File tree

‎packages/core/src/expand.ts‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -518,14 +518,11 @@ export function* expandSegments(
518518
}
519519

520520
if (segment.name === "If") {
521-
const ifResult = yield* expandIf(segment, parentMeta, parentProps, hideSet, counter);
522-
for (const ifSegment of ifResult) {
523-
if (ifSegment.type === "error") {
524-
result.push(yield* raise(ifSegment));
525-
} else {
526-
result.push(ifSegment);
527-
}
528-
}
521+
// `<If>` is not an observation boundary. It reports the errors it
522+
// creates itself, and the selected branch's segments already passed
523+
// through the ambient policy where they were produced, so they are
524+
// appended as they are (spec §6.9: one observation per segment).
525+
result.push(...(yield* expandIf(segment, parentMeta, parentProps, hideSet, counter)));
529526
break;
530527
}
531528

@@ -1007,6 +1004,12 @@ const IF_PROPS = new Set(["condition"]);
10071004
* `<If>` opens no binding scope: the selected branch expands in the enclosing
10081005
* environment, so a `<Capture>` it creates behaves like inline content and
10091006
* stays available after `</If>`.
1007+
*
1008+
* It is not an observation boundary either. Errors it creates itself — an
1009+
* invalid condition, an unknown prop, a malformed `<Else>` — are reported here,
1010+
* exactly once. Everything the selected branch returns was already reported
1011+
* where it was produced and is handed back untouched, so a `<Broken />` inside
1012+
* a selected branch settles once, exactly as it would inline.
10101013
*/
10111014
function* expandIf(
10121015
segment: ComponentElement,
@@ -1019,12 +1022,20 @@ function* expandIf(
10191022
(name) => !IF_PROPS.has(name),
10201023
);
10211024
if (unknownProp !== undefined) {
1022-
return [ifError(segment, `<If> only accepts a "condition" prop. Got: "${unknownProp}".`)];
1025+
return [
1026+
yield* raise(
1027+
ifError(segment, `<If> only accepts a "condition" prop. Got: "${unknownProp}".`),
1028+
),
1029+
];
10231030
}
10241031

10251032
const structure = ifStructure(segment);
10261033
if (structure.violations.length > 0) {
1027-
return structure.violations;
1034+
const reported: Segment[] = [];
1035+
for (const violation of structure.violations) {
1036+
reported.push(yield* raise(violation));
1037+
}
1038+
return reported;
10281039
}
10291040

10301041
let condition: Json;
@@ -1040,18 +1051,22 @@ function* expandIf(
10401051
);
10411052
condition = resolved.condition;
10421053
} catch (error) {
1043-
return [ifError(segment, error instanceof Error ? error.message : String(error))];
1054+
return [
1055+
yield* raise(ifError(segment, error instanceof Error ? error.message : String(error))),
1056+
];
10441057
}
10451058
} else {
1046-
return [ifError(segment, '<If> requires a "condition" prop (a boolean).')];
1059+
return [yield* raise(ifError(segment, '<If> requires a "condition" prop (a boolean).'))];
10471060
}
10481061

10491062
if (typeof condition !== "boolean") {
10501063
return [
1051-
ifError(
1052-
segment,
1053-
`Prop "condition" on <If /> must be a boolean, not ${jsonKind(condition)}. ` +
1054-
"<If> does not coerce truthy or falsy values.",
1064+
yield* raise(
1065+
ifError(
1066+
segment,
1067+
`Prop "condition" on <If /> must be a boolean, not ${jsonKind(condition)}. ` +
1068+
"<If> does not coerce truthy or falsy values.",
1069+
),
10551070
),
10561071
];
10571072
}

‎packages/core/tests/if.test.ts‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { scoped } from "effection";
55
import type { Operation } from "effection";
66
import { expandSegments } from "../src/expand.ts";
77
import { Component } from "../src/component-api.ts";
8+
import { AmbientErrorPolicy, DocumentationError } from "../src/errors.ts";
89
import { scanSegments } from "../src/scanner.ts";
910
import type { SourceOrigin } from "../src/scanner.ts";
1011
import { renderSegments } from "../src/render.ts";
@@ -694,3 +695,86 @@ describe("Tier IF — the unselected branch reaches no external mechanism", () =
694695
expect(selected.output).toContain("[then]");
695696
});
696697
});
698+
699+
/**
700+
* `<If>` must not add an observation boundary (spec §6.9). Every ErrorSegment
701+
* passes through `Component.raise` exactly once, where it is created — so an
702+
* error inside a selected branch settles once, exactly as the same error would
703+
* inline, and an error `<If>` creates itself settles once too.
704+
*/
705+
describe("Tier IF — error observation", () => {
706+
interface RaiseProbe {
707+
observed: string[];
708+
output: string;
709+
}
710+
711+
function runRaiseProbe(source: string): Operation<RaiseProbe> {
712+
return scoped(function* () {
713+
const observed: string[] = [];
714+
yield* Component.around({
715+
*raise([error], next) {
716+
observed.push(error.message);
717+
return yield* next(error);
718+
},
719+
});
720+
yield* Component.around({
721+
// deno-lint-ignore require-yield
722+
*expand([element], next) {
723+
if (element.name === "Broken") {
724+
return {
725+
segments: [{ type: "error", message: "broken thing", source: "Broken" }],
726+
};
727+
}
728+
return yield* next(element);
729+
},
730+
});
731+
yield* Component.around({ env: () => ({ values: {} }) }, { at: "min" });
732+
const segments = yield* expandSegments(scanSegments(source), {}, {}, new Set());
733+
return { observed, output: renderSegments(segments) };
734+
});
735+
}
736+
737+
it("IF49: an inline error is observed once", function* () {
738+
const probe = yield* runRaiseProbe("<Broken />");
739+
expect(probe.observed).toEqual(["broken thing"]);
740+
});
741+
742+
it("IF50: the same error inside a selected branch is observed once", function* () {
743+
const probe = yield* runRaiseProbe("<If condition={true}><Broken /></If>");
744+
expect(probe.observed).toEqual(["broken thing"]);
745+
expect(probe.output).toContain("broken thing");
746+
});
747+
748+
it("IF51: an error in an unselected branch is observed zero times", function* () {
749+
const probe = yield* runRaiseProbe("<If condition={false}><Broken /><Else>alt</Else></If>");
750+
expect(probe.observed).toEqual([]);
751+
expect(probe.output).toBe("alt");
752+
});
753+
754+
it("IF52: an <If>-owned validation error is observed once", function* () {
755+
const missing = yield* runRaiseProbe("<If>body</If>");
756+
expect(missing.observed).toHaveLength(1);
757+
expect(missing.observed[0]).toContain('requires a "condition" prop');
758+
759+
const nonBoolean = yield* runRaiseProbe("<If condition={1}>body</If>");
760+
expect(nonBoolean.observed).toHaveLength(1);
761+
expect(nonBoolean.observed[0]).toContain("must be a boolean");
762+
763+
const structure = yield* runRaiseProbe('<If condition={true}>a<Else when="x">b</Else></If>');
764+
expect(structure.observed).toHaveLength(1);
765+
expect(structure.observed[0]).toContain("accepts no props");
766+
});
767+
768+
it("IF53: a throwing policy still aborts on a selected-branch error", function* () {
769+
let thrown: unknown;
770+
yield* scoped(function* () {
771+
yield* AmbientErrorPolicy.set("throw");
772+
try {
773+
yield* runRaiseProbe("<If condition={true}><Broken /></If>");
774+
} catch (error) {
775+
thrown = error;
776+
}
777+
});
778+
expect(thrown).toBeInstanceOf(DocumentationError);
779+
});
780+
});

‎specs/executable-mdx-spec.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3026,6 +3026,14 @@ segments that are spliced into the surrounding output, so `ErrorSegment` and
30263026
`execOutput` segments survive and the ambient raise policy applies to them
30273027
exactly as elsewhere.
30283028

3029+
`<If>` is **not an observation boundary**. Under the one-observation rule
3030+
(§6.9), it reports only the errors it creates itself — an invalid condition, an
3031+
unknown prop, a malformed `<Else>` — and hands back the selected branch's
3032+
segments untouched, because they were already reported where they were
3033+
produced. A failing element inside a selected branch therefore settles exactly
3034+
once, as it would inline, and an ambient `throw` policy still aborts at the
3035+
first error.
3036+
30293037
Diagnostics from `<If>` and `<Else>` carry the source location of the element
30303038
that caused them, as `path:line:column` when the element came from a file and
30313039
`line:column` for text scanned without an origin.
@@ -5032,6 +5040,11 @@ Identifiers match `packages/core/tests/if.test.ts` one to one.
50325040
| IF46 | Process runtime | `exec` is never invoked for an unselected block |
50335041
| IF47 | Durable events | The unselected branch writes no exec or eval event |
50345042
| IF48 | Bindings (execution) | Later content sees no binding from the unselected branch |
5043+
| IF49 | Inline observation baseline | An `ErrorSegment` outside any `<If>` passes through `Component.raise` once |
5044+
| IF50 | Selected branch observed once | The same error inside a selected branch is observed once, not twice |
5045+
| IF51 | Unselected branch unobserved | An error in the unselected branch is observed zero times |
5046+
| IF52 | `<If>`-owned errors observed once | Missing/non-boolean `condition` and a malformed `<Else>` each report once |
5047+
| IF53 | Throwing policy | An ambient `throw` policy still aborts on a selected-branch error |
50355048

50365049
### Tier SC — Sample component (integration)
50375050

0 commit comments

Comments
 (0)