Skip to content

Latest commit

Β 

History

History
73 lines (46 loc) Β· 1.63 KB

File metadata and controls

73 lines (46 loc) Β· 1.63 KB

n/prefer-global/text-decoder

πŸ“ Enforce either TextDecoder or require("util").TextDecoder.

The TextDecoder class of util module is defined as a global variable.

console.log(TextDecoder === require("util").TextDecoder) //β†’ true

It will be readable if we use either TextDecoder consistently.

πŸ“– Rule Details

This rule enforces which TextDecoder we should use.

Options

This rule has a string option.

{
    "n/prefer-global/text-decoder": ["error", "always" | "never"]
}
  • "always" (default) ... enforces to use the global variable TextDecoder rather than require("util").TextDecoder.
  • "never" ... enforces to use require("util").TextDecoder rather than the global variable TextDecoder.

always

Examples of πŸ‘Ž incorrect code for this rule:

/*eslint n/prefer-global/text-decoder: [error]*/

const { TextDecoder } = require("util")
const u = new TextDecoder(s)

Examples of πŸ‘ correct code for this rule:

/*eslint n/prefer-global/text-decoder: [error]*/

const u = new TextDecoder(s)

never

Examples of πŸ‘Ž incorrect code for the "never" option:

/*eslint n/prefer-global/text-decoder: [error, never]*/

const u = new TextDecoder(s)

Examples of πŸ‘ correct code for the "never" option:

/*eslint n/prefer-global/text-decoder: [error, never]*/

const { TextDecoder } = require("util")
const u = new TextDecoder(s)

πŸ”Ž Implementation