Skip to content

Latest commit

 

History

History
71 lines (45 loc) · 1.52 KB

File metadata and controls

71 lines (45 loc) · 1.52 KB

Enforce either Buffer or require("buffer").Buffer (n/prefer-global/buffer)

The Buffer class of buffer module is defined as a global variable.

console.log(Buffer === require("buffer").Buffer) //→ true

It will be readable if we use either Buffer consistently.

📖 Rule Details

This rule enforces which Buffer we should use.

Options

This rule has a string option.

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

always

Examples of 👎 incorrect code for this rule:

/*eslint n/prefer-global/buffer: [error]*/

const { Buffer } = require("buffer")
const b = Buffer.alloc(16)

Examples of 👍 correct code for this rule:

/*eslint n/prefer-global/buffer: [error]*/

const b = Buffer.alloc(16)

never

Examples of 👎 incorrect code for the "never" option:

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

const b = Buffer.alloc(16)

Examples of 👍 correct code for the "never" option:

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

const { Buffer } = require("buffer")
const b = Buffer.alloc(16)

🔎 Implementation