Skip to content

Latest commit

Β 

History

History
39 lines (24 loc) Β· 1.68 KB

File metadata and controls

39 lines (24 loc) Β· 1.68 KB

logical-assignment-operators

πŸ“ Require or disallow logical assignment operator shorthand.

πŸ’ΌπŸš« This rule is enabled in the βœ… recommended config. This rule is disabled in the β˜‘οΈ unopinionated config.

πŸ”§πŸ’‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

This rule is the same as the built-in ESLint logical-assignment-operators rule, but when enforceForIfStatements is enabled, falsy if assignments are reported with suggestions for both ||= and ??= instead of being autofixed.

Consistently choosing logical-assignment shorthand or an explicit assignment or conditional makes updates easier to scan and avoids mixing the same pattern in different forms.

Replacement for ESLint logical-assignment-operators

This rule replaces ESLint's built-in logical-assignment-operators rule, which Unicorn presets disable when this rule is enabled.

Examples

/* eslint unicorn/logical-assignment-operators: ["error", "always", {"enforceForIfStatements": true}] */

// ❌
if (!foo) {
	foo = bar;
}

// βœ…
foo ||= bar;

// βœ…
foo ??= bar;

Options

This rule supports the same options as ESLint logical-assignment-operators.