|
| 1 | +# enforce either `TextDecoder` or `require("util").TextDecoder` (prefer-global/text-decoder) |
| 2 | + |
| 3 | +The `TextDecoder` class of `util` module is defined as a global variable. |
| 4 | + |
| 5 | +```js |
| 6 | +console.log(TextDecoder === require("util").TextDecoder) //→ true |
| 7 | +``` |
| 8 | + |
| 9 | +It will be readable if we use either `TextDecoder` consistently. |
| 10 | + |
| 11 | +## Rule Details |
| 12 | + |
| 13 | +This rule enforces which `TextDecoder` we should use. |
| 14 | + |
| 15 | +### Options |
| 16 | + |
| 17 | +This rule has a string option. |
| 18 | + |
| 19 | +```json |
| 20 | +{ |
| 21 | + "node/prefer-global/text-decoder": ["error", "always" | "never"] |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +- `"always"` (default) ... enforces to use the global variable `TextDecoder` rather than `require("util").TextDecoder`. |
| 26 | +- `"never"` ... enforces to use `require("util").TextDecoder` rather than the global variable `TextDecoder`. |
| 27 | + |
| 28 | +#### always |
| 29 | + |
| 30 | +Examples of :-1: **incorrect** code for this rule: |
| 31 | + |
| 32 | +```js |
| 33 | +/*eslint node/prefer-global/text-decoder: [error]*/ |
| 34 | + |
| 35 | +const { TextDecoder } = require("util") |
| 36 | +const u = new TextDecoder(s) |
| 37 | +``` |
| 38 | + |
| 39 | +Examples of :+1: **correct** code for this rule: |
| 40 | + |
| 41 | +```js |
| 42 | +/*eslint node/prefer-global/text-decoder: [error]*/ |
| 43 | + |
| 44 | +const u = new TextDecoder(s) |
| 45 | +``` |
| 46 | + |
| 47 | +#### never |
| 48 | + |
| 49 | +Examples of :-1: **incorrect** code for the `"never"` option: |
| 50 | + |
| 51 | +```js |
| 52 | +/*eslint node/prefer-global/text-decoder: [error, never]*/ |
| 53 | + |
| 54 | +const u = new TextDecoder(s) |
| 55 | +``` |
| 56 | + |
| 57 | +Examples of :+1: **correct** code for the `"never"` option: |
| 58 | + |
| 59 | +```js |
| 60 | +/*eslint node/prefer-global/text-decoder: [error, never]*/ |
| 61 | + |
| 62 | +const { TextDecoder } = require("util") |
| 63 | +const u = new TextDecoder(s) |
| 64 | +``` |
0 commit comments