Skip to content

Latest commit

 

History

History
71 lines (45 loc) · 1.58 KB

File metadata and controls

71 lines (45 loc) · 1.58 KB

Enforce either crypto or require("crypto").webcrypto (n/prefer-global/crypto)

An implementation of the Web Crypto API is defined as a global variable.

console.log(crypto === require("node:crypto").webcrypto) //→ true

It will be readable if we use crypto consistently.

📖 Rule Details

This rule enforces whether to use the global crypto variable or the webcrypto property from the crypto module.

Options

This rule has a string option.

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

always

Examples of 👎 incorrect code for this rule:

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

const { webcrypto } = require("crypto")
webcrypto.randomUUID()

Examples of 👍 correct code for this rule:

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

crypto.randomUUID()

never

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

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

crypto.randomUUID()

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

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

const { webcrypto } = require("crypto")
webcrypto.randomUUID()

🔎 Implementation