The TextEncoder
class of util
module is defined as a global variable.
console.log(TextEncoder === require("util").TextEncoder) //→ true
It will be readable if we use either TextEncoder
consistently.
This rule enforces which TextEncoder
we should use.
This rule has a string option.
{
"n/prefer-global/text-encoder": ["error", "always" | "never"]
}
"always"
(default) ... enforces to use the global variableTextEncoder
rather thanrequire("util").TextEncoder
."never"
... enforces to userequire("util").TextEncoder
rather than the global variableTextEncoder
.
Examples of 👎 incorrect code for this rule:
/*eslint n/prefer-global/text-encoder: [error]*/
const { TextEncoder } = require("util")
const u = new TextEncoder(s)
Examples of 👍 correct code for this rule:
/*eslint n/prefer-global/text-encoder: [error]*/
const u = new TextEncoder(s)
Examples of 👎 incorrect code for the "never"
option:
/*eslint n/prefer-global/text-encoder: [error, never]*/
const u = new TextEncoder(s)
Examples of 👍 correct code for the "never"
option:
/*eslint n/prefer-global/text-encoder: [error, never]*/
const { TextEncoder } = require("util")
const u = new TextEncoder(s)