-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
prefer-blob-reading-methods
rule (#2065)
Co-authored-by: fisker <[email protected]>
- Loading branch information
Showing
7 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Prefer `Blob#arrayBuffer()` over `FileReader#readAsArrayBuffer(…)` and `Blob#text()` over `FileReader#readAsText(…)` | ||
|
||
💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs). | ||
|
||
<!-- end auto-generated rule header --> | ||
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` --> | ||
|
||
`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. | ||
|
||
## Fail | ||
|
||
```js | ||
const arrayBuffer = await new Promise((resolve, reject) => { | ||
const fileReader = new FileReader(); | ||
fileReader.addEventListener('load', () => { | ||
resolve(fileReader.result); | ||
}); | ||
fileReader.addEventListener('error', () => { | ||
reject(fileReader.error); | ||
}); | ||
fileReader.readAsArrayBuffer(blob); | ||
}); | ||
``` | ||
|
||
```js | ||
fileReader.readAsText(blob); | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const arrayBuffer = await blob.arrayBuffer(); | ||
``` | ||
|
||
```js | ||
const text = await blob.text(); | ||
``` | ||
|
||
```js | ||
fileReader.readAsText(blob, 'ascii'); | ||
``` | ||
|
||
```js | ||
fileReader.readAsDataURL(blob); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
const {methodCallSelector} = require('./selectors/index.js'); | ||
|
||
const messages = { | ||
'error/readAsArrayBuffer': 'Prefer `Blob#arrayBuffer()` over `FileReader#readAsArrayBuffer(blob)`.', | ||
'error/readAsText': 'Prefer `Blob#text()` over `FileReader#readAsText(blob)`.', | ||
}; | ||
|
||
const selector = methodCallSelector({ | ||
methods: ['readAsText', 'readAsArrayBuffer'], | ||
argumentsLength: 1, | ||
}); | ||
|
||
/** @param {import('eslint').Rule.RuleContext} context */ | ||
const create = () => ({ | ||
[selector](node) { | ||
const method = node.callee.property; | ||
const methodName = method.name; | ||
|
||
return { | ||
node: method, | ||
messageId: `error/${methodName}`, | ||
}; | ||
}, | ||
}); | ||
|
||
/** @type {import('eslint').Rule.RuleModule} */ | ||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Prefer `Blob#arrayBuffer()` over `FileReader#readAsArrayBuffer(…)` and `Blob#text()` over `FileReader#readAsText(…)`.', | ||
}, | ||
messages, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {getTester} from './utils/test.mjs'; | ||
|
||
const {test} = getTester(import.meta); | ||
|
||
test.snapshot({ | ||
valid: [ | ||
'blob.arrayBuffer()', | ||
'blob.text()', | ||
'new Response(blob).arrayBuffer()', | ||
'new Response(blob).text()', | ||
'fileReader.readAsDataURL(blob)', | ||
'fileReader.readAsBinaryString(blob)', | ||
'fileReader.readAsText(blob, "ascii")', | ||
], | ||
invalid: [ | ||
'fileReader.readAsArrayBuffer(blob)', | ||
'fileReader.readAsText(blob)', | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Snapshot report for `test/prefer-blob-reading-methods.mjs` | ||
|
||
The actual snapshot is saved in `prefer-blob-reading-methods.mjs.snap`. | ||
|
||
Generated by [AVA](https://avajs.dev). | ||
|
||
## Invalid #1 | ||
1 | fileReader.readAsArrayBuffer(blob) | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | fileReader.readAsArrayBuffer(blob)␊ | ||
| ^^^^^^^^^^^^^^^^^ Prefer \`Blob#arrayBuffer()\` over \`FileReader#readAsArrayBuffer(blob)\`.␊ | ||
` | ||
|
||
## Invalid #2 | ||
1 | fileReader.readAsText(blob) | ||
|
||
> Error 1/1 | ||
`␊ | ||
> 1 | fileReader.readAsText(blob)␊ | ||
| ^^^^^^^^^^ Prefer \`Blob#text()\` over \`FileReader#readAsText(blob)\`.␊ | ||
` |
Binary file not shown.