Add an @unthrown/oxlint rule: prefer-ensure (flatMap gates that should be ensure)
Summary
@unthrown/oxlint ships five rules (no-ambiguous-error-type, prefer-async-result, no-unhandled-result, no-catch-all-pattern, no-throw). I'd like to propose a sixth: unthrown/prefer-ensure, flagging flatMap calls that are really validation gates and should use the ensure combinator v5 introduced.
The motivation is that ensure is new in v5, so every existing codebase is full of the hand-rolled shape — and unlike most style rules, this one is mechanically detectable with no false-positive-prone heuristics.
The shape
A flatMap whose success branch returns the callback's own parameter, untouched is not a bind — it is a predicate:
// flagged
.flatMap((information) =>
information.status !== "DISCHARGED"
? Ok(information)
: Err(new BadStatusError({ status: information.status })),
)
// suggested
.ensure(
(information) => information.status !== "DISCHARGED",
(information) => new BadStatusError({ status: information.status }),
)
A second, slightly weaker variant — an absence guard followed by a projection — maps onto the type-guard overload plus map, which separates "does it exist" from "what shape do I return":
// flagged
.flatMap((doc) => {
if (doc === null) {
return Err(new NotFoundError({ id }));
}
return Ok({ id: doc.id as EntityId, lines: doc.lines.map(...) });
})
// suggested
.ensure(
(doc): doc is NonNullable<typeof doc> => doc !== null,
() => new NotFoundError({ id }),
).map((doc) => ({ id: doc.id as EntityId, lines: doc.lines.map(...) }))
Suggested detection rule
For each .flatMap((x) => <body>) / .flatMapOk((x) => <body>):
- Gate (strong signal, safe to autofix-suggest): every
Ok(...)/OkAsync(...)in the body is exactlyOk(x)— the bound parameter, no property access, no re-wrap — and the body contains at least oneErr(...)/ErrAsync(...). - Guard + projection (weaker, report-only): exactly one
Err(...), guarded by an absence check onx(!x,x === null,x === undefined) as the body's first statement, and the remaining return is a singleOk(<expr derived from x>).
Skipping bodies that contain a nested flatMap keeps the rule conservative — I did that in my own scan and it costs a handful of true positives, which seems the right trade for a lint rule.
Purely syntactic; no type information required, so it should fit the JS-plugin model the other rules use.
Prevalence
I scanned a ~264-project TypeScript monorepo mid-way through its v5 upgrade (18 packages on unthrown, ~209 combinator call sites). Excluding nested-flatMap bodies, so this is a floor:
| Shape | Sites |
|---|---|
| Pure gate | 17 |
| Absence guard + projection | 12 |
| Total | 29 |
They cluster hard, which is what you'd expect if the idiom is load-bearing rather than incidental — one domain service accounts for 10, one Prisma repository for 8 (eight near-identical if (row === null) return Err(...) guards).
The strongest single case is a validator built entirely out of flatMap-returning-Ok(same) — four chained gates in a row, each one a predicate wearing a monad-bind costume. With ensure it reads as the validation pipeline it actually is.
Why a lint rule rather than docs
The migration path to v5 is already well-signposted for the combinators that broke — mapErr → mapErrCases is a compile error, so nobody misses it. ensure is purely additive: the old shape keeps compiling forever, so it only gets adopted where someone happens to remember it exists. That's precisely the gap a lint rule closes, and it's the same argument that landed no-catch-all-pattern in recommended at beta.8.
Happy to contribute the implementation if you'd take a PR — I have the detector logic and a corpus of 29 real call sites to test it against.