Skip to content

Latest commit

 

History

History
130 lines (85 loc) · 2.38 KB

no-let.md

File metadata and controls

130 lines (85 loc) · 2.38 KB

Disallow mutable variables (functional/no-let)

💼 This rule is enabled in the following configs: ☑️ lite, noMutations, ✅ 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.

❌ Incorrect

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

for (let element of array) {
}
/* eslint functional/no-let: ["error", { "allowInForLoopInit": true } ] */

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

✅ Correct

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

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

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.