Skip to content

Latest commit

 

History

History
124 lines (80 loc) · 2.2 KB

no-let.md

File metadata and controls

124 lines (80 loc) · 2.2 KB

Disallow mutable variables (functional/no-let)

💼 This rule is enabled in the following configs: ☑️ lite, no-mutations, ✅ recommended, 🔒 strict.

This rule should be combined with ESLint's built-in no-var rule to enforce that all variables are declared as const.

Rule Details

In functional programming variables should not be mutable; use const instead.

❌ Incorrect

/* eslint functional/no-let: "error" */

let x = 5;
/* eslint functional/no-let: "error" */

for (let i = 0; i < array.length; i++) {}

✅ Correct

/* eslint functional/no-let: "error" */

const x = 5;
/* eslint functional/no-let: "error" */

for (const element of array) {
}
/* eslint functional/no-let: "error" */

for (const [index, element] of array.entries()) {
}

Options

This rule accepts an options object of the following type:

type Options = {
  allowInFunctions: boolean;
  ignoreIdentifierPattern?: string[] | string;
};

Default Options

const defaults = {
  allowInForLoopInit: false,
  allowInFunctions: false,
};

Preset Overrides

recommended and lite

const recommendedAndLiteOptions = {
  allowInForLoopInit: true,
};

allowInForLoopInit

If set, lets inside of for a loop initializer are allowed. This does not include for...of or for...in loops as they should use const instead.

✅ Correct

/* eslint functional/no-let: ["error", { "allowInForLoopInit": true } ] */

for (let i = 0; i < array.length; i++) {}

❌ Incorrect

/* eslint functional/no-let: "error" */

for (let element of array) {
}
/* eslint functional/no-let: "error" */

for (let [index, element] of array.entries()) {
}

allowInFunctions

If true, the rule will not flag any statements that are inside of function bodies.

ignoreIdentifierPattern

This option takes a RegExp string or an array of RegExp strings. It allows for the ability to ignore violations based on a variable's name.