unthrown icon indicating copy to clipboard operation
unthrown copied to clipboard

A lint rule for `Ok(v).toAsync()` — the receiver makes it mechanical, unlike prefer-ensure

Open btravers opened this issue 3 months ago • 0 comments

Filed from btravstack/btravstack, the way #92 was: the convention was written down there, held by review, and drifted anyway.

What the rule would flag

Ok(value).toAsync()   // → OkAsync(value)
Err(error).toAsync()  // → ErrAsync(error)
Ok().toAsync()        // → OkAsync()
Ok(undefined).toAsync() // → OkAsync()

Nothing else. The test is the receiver: a call expression whose callee is the imported Ok or Err, immediately followed by .toAsync(). A .toAsync() on anything else — a variable, a function's return, a ternary, a fromNullable(...) — is the combinator doing its actual job and must not be touched.

Why this one is safe where prefer-ensure was not

prefer-ensure was removed in 5.4.0 for flagging correct code and carrying a known false positive, and I am not asking for it back. The difference is where the rule has to look:

  • prefer-ensure had to reason about a callback's body — is this Ok(x) the same x the callback was handed? That is an identity judgement across a scope, which is where the false positives came from.
  • This one is a syntactic receiver check. Ok(...) immediately followed by .toAsync() has exactly one meaning: build a Result and immediately throw it away for its async twin. There is no context in which that is the intended spelling, so there is no case to exempt.

It is autofixable for the same reason: the replacement is the callee's name plus Async, with the arguments untouched (and the empty-argument and literal undefined forms collapsing to OkAsync()).

The evidence

btravstack/btravstack documents this convention in its root CLAUDE.md — "Pre-lifted constructors, not .toAsync() on a fresh literal" — and asserted that exactly one .toAsync() in the repository was legitimate.

A sweep just now found 33 sites violating it, in three packages, against 8 that lift an existing Result correctly. The claim in the docs was wrong in both directions, and nothing caught it: tsc is happy, oxlint is happy, every test passes. Ok(v).toAsync() and OkAsync(v) are the same value; the only difference is an allocation nobody profiles and a reader's second glance.

That is the profile of a rule rather than a convention. A style point that type-checks, tests green, and is invisible in review is one that only a linter holds.

Scope note

Its natural home is the opt-in set, beside no-throw and no-get-or-throw — this is a spelling preference, not a thesis about correctness, and a consumer who does not share it should not be forced into it by recommended.

btravers avatar Aug 26 '26 21:08 btravers