unthrown icon indicating copy to clipboard operation
unthrown copied to clipboard

oxlint/prefer-async-result: add the AsyncResult import as part of the autofix when it isn't in scope

Open btravers opened this issue 4 months ago • 0 comments

prefer-async-result withholds its autofix when AsyncResult is not already imported from unthrown (packages/oxlint/src/rules/prefer-async-result.ts:92-101):

// Withhold the fix when this annotation is an `async` function's … or when
// `AsyncResult` isn't importable (the rewrite would reference an undefined name)

The reasoning is right — rewriting to a name that isn't in scope produces code that doesn't compile. But it makes the autofix unavailable in the most common real case: a file that already imports Result (and hence trips the rule) but has never needed AsyncResult. The diagnostic fires, the fix does not, and the user adds the import by hand before --fix will do anything.

Proposal

When AsyncResult is absent from scope and an import … from "unthrown" statement exists in the file, extend the fix to add the specifier as well as rewrite the annotation.

This is mechanically supported: FixFn returns Fix | Array<Fix | null | undefined> | IterableIterator<…> (@oxlint/plugins index.d.ts:3303), so one report can emit both edits — an insertTextAfter on the last specifier plus the existing replaceText on the annotation.

Guards to keep

  • async function return annotations (existing) — the implementation must return a native Promise.
  • Function-type return positions (existing) — same reason.
  • Locally-bound AsyncResult shadowing the import: adding a specifier would not help, so keep withholding.

Deliberately out of scope

A file with no unthrown import at all. Inserting a whole new import statement is a more invasive edit with its own placement and import type questions; leave the fix withheld there and revisit separately if it turns out to matter.

Notes

  • If the existing import is import type { Result } from "unthrown", adding AsyncResult to it is still correct — the rewrite only uses it in type position.
  • RuleTester cases should cover: type-only import, value import, renamed import (Result as R), namespace import, and the shadowed-binding no-fix case.

btravers avatar Aug 03 '26 22:08 btravers