{"record":{"id":"f6c980e2e6610825","repo":"oxc-project/oxc","slug":"prefer-blob-good-method-over-filereader-ba","errorCode":null,"errorMessage":"Prefer `Blob#{good_method}()` over `FileReader#{bad_method}(blob)`.","messagePattern":"Prefer `Blob#(.+?)\\(\\)` over `FileReader#(.+?)\\(blob\\)`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_blob_reading_methods.rs","lineNumber":13,"sourceCode":"use oxc_ast::AstKind;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn prefer_blob_reading_methods_diagnostic(\n    span: Span,\n    good_method: &str,\n    bad_method: &str,\n) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\n        \"Prefer `Blob#{good_method}()` over `FileReader#{bad_method}(blob)`.\"\n    ))\n    .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferBlobReadingMethods;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Recommends using `Blob#text()` and `Blob#arrayBuffer()` over `FileReader#readAsText()` and `FileReader#readAsArrayBuffer()`.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// `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.\n    ///\n    /// ### Examples","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_blob_reading_methods.rs#L1-L31","documentation":"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`.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Replace with `const text = await blob.text();` or `const buf = await blob.arrayBuffer();`.","Run `oxlint --fix` if available for the exact pattern, or refactor manually since FileReader setup spans several statements.","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`.","Disable the rule for codebases supporting browsers without `Blob#text()` (very old Safari/IE)."],"exampleFix":"// before\nconst reader = new FileReader();\nreader.onload = () => use(reader.result);\nreader.readAsText(blob);\n\n// after\nconst text = await blob.text();\nuse(text);","handlingStrategy":"validation","validationCode":"// oxlint --filter unicorn/prefer-blob-reading-methods src/\n// CI gate: oxlint --deny-warnings src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default to `await blob.text()` / `await blob.arrayBuffer()`; reach for FileReader only for non-UTF-8 encodings or progress events.","When suppression is justified (encoding argument), record why in the disable comment."],"tags":["oxlint","unicorn","dom","blob","async","file-api"],"backgroundTag":"lint-rule-violation","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}