oxc-project/oxc · warning · OxcDiagnostic

Prefer `Blob#{good_method}()` over `FileReader#{bad_method}(

Error message

Prefer `Blob#{good_method}()` over `FileReader#{bad_method}(blob)`.

What it means

Lint diagnostic from oxlint's `unicorn/prefer-blob-reading-methods` rule. `FileReader.readAsText(blob)` / `readAsArrayBuffer(blob)` use callback-based eventing (`onload`, `onerror`) to deliver the content. The promise-based `Blob#text()` and `Blob#arrayBuffer()` do the same in one awaitable call. The rule flags the FileReader forms on a Blob argument; `{good_method}`/`{bad_method}` interpolate as `text`/`readAsText` or `arrayBuffer`/`readAsArrayBuffer`.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/prefer_blob_reading_methods.rs:13

use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

use crate::{AstNode, context::LintContext, rule::Rule};

fn prefer_blob_reading_methods_diagnostic(
    span: Span,
    good_method: &str,
    bad_method: &str,
) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!(
        "Prefer `Blob#{good_method}()` over `FileReader#{bad_method}(blob)`."
    ))
    .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct PreferBlobReadingMethods;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Recommends using `Blob#text()` and `Blob#arrayBuffer()` over `FileReader#readAsText()` and `FileReader#readAsArrayBuffer()`.
    ///
    /// ### Why is this bad?
    ///
    /// `FileReader` predates promises, and the newer [`Blob#arrayBuffer()`](https://developer.mozilla.org/en-US/docs/Web/API/Blob/arrayBuffer) and [`Blob#text()`](https://developer.mozilla.org/en-US/docs/Web/API/Blob/text) methods are much cleaner and easier to use.
    ///
    /// ### Examples

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Replace with `const text = await blob.text();` or `const buf = await blob.arrayBuffer();`.
  2. Run `oxlint --fix` if available for the exact pattern, or refactor manually since FileReader setup spans several statements.
  3. If you need a specific encoding (`readAsText(blob, 'windows-1252')`) or progress events, keep FileReader and suppress inline with `// oxlint-disable-next-line unicorn/prefer-blob-reading-methods`.
  4. Disable the rule for codebases supporting browsers without `Blob#text()` (very old Safari/IE).

Example fix

// before
const reader = new FileReader();
reader.onload = () => use(reader.result);
reader.readAsText(blob);

// after
const text = await blob.text();
use(text);
Defensive patterns

Strategy: validation

Validate before calling

// oxlint --filter unicorn/prefer-blob-reading-methods src/
// CI gate: oxlint --deny-warnings src/

Prevention

When it happens

Trigger: `const reader = new FileReader(); reader.readAsText(blob);` then `reader.onload = ...`, or `readAsArrayBuffer(blob)` where the argument is identified as a Blob. Detected during oxlint runs when the rule matches `FileReader` method calls with a blob-ish argument.

Common situations: Copy-paste from older file-upload tutorials (FileReader predates the promise API). Refactors to async/await usually trigger the cleanup. Note real behavioral differences: `Blob#text()` always decodes as UTF-8 while `readAsText(blob, encoding)` honors the encoding argument — if you rely on a non-UTF-8 encoding, suppressing the warning is legitimate.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/f6c980e2e6610825. Report an issue: GitHub.