Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rule logical-assignment-operators #16102

Merged
merged 37 commits into from Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e7cb5b1
feat: add rule logical-assignment-operators
DMartens Jul 5, 2022
e320bfd
docs: update rule documentation
DMartens Jul 11, 2022
e3e4672
chore: unify ast-utils import
DMartens Jul 11, 2022
f667a30
fix: only check for void 0 in undefined checks
DMartens Jul 11, 2022
b0b978a
fix: always fix for the logical pattern
DMartens Jul 12, 2022
21cc419
feat: support yoda conditions in if conditions
DMartens Jul 12, 2022
8a1da4b
fix: remove parenthesis around assignment target if necessary
DMartens Jul 12, 2022
9bd6e23
fix: parenthesize logical pattern if needed
DMartens Jul 12, 2022
a55ae4c
fix: add semicolon for if patterns with a body if needed
DMartens Jul 12, 2022
9a4b7f8
fix: remove file extension from import
DMartens Jul 12, 2022
a548dd4
fix: check strictness of global scope instead of current scope to avo…
DMartens Jul 13, 2022
613c2d3
fix: check for mixed ?? and ||/&& operators in the fix for never
DMartens Jul 13, 2022
ec1e1a5
fix: check previous token for continuation problems in the if fix
DMartens Jul 13, 2022
f964605
feat: support else if (and fix suggest cases for if)
DMartens Jul 30, 2022
ef42c44
fix: do not remove else keyword for else if
DMartens Aug 8, 2022
9d8dfc7
fix: if cases also suggest based on potential getter
DMartens Aug 8, 2022
9191a66
fix: also fix if only a single property is accessed for the if pattern
DMartens Aug 11, 2022
0214ef8
fix: do not fix logical patterns with a deeper access
DMartens Aug 31, 2022
adc1689
fix: check whether Boolean references a global
DMartens Sep 6, 2022
76ac24c
fix: allow test conditions to access the same static property (a.b <=…
DMartens Sep 6, 2022
784fefe
fix: use the whole assignment operator with equals in the message and…
DMartens Sep 6, 2022
fa5c0cc
docs: remove edit_link
DMartens Sep 6, 2022
e159e7a
docs: remove description from docs as it is autogenerated from rule.m…
DMartens Sep 6, 2022
ef1a3f8
docs: move introductory text before rule details
DMartens Sep 6, 2022
c211235
docs: add missing 'logical' to rule description
DMartens Sep 6, 2022
aad522c
docs: fix formatting for options
DMartens Sep 6, 2022
863df8e
docs: include all logical operators for option 'never'
DMartens Sep 6, 2022
88c5360
docs: add examples for option 'always'
DMartens Sep 6, 2022
a99eb62
docs: add examples of for the 'enforceForIfStatements' option and swa…
DMartens Sep 6, 2022
aa69bbf
fix: disallow optional chaining for the logical pattern
DMartens Sep 6, 2022
7f2c77f
fix: fixer does not delete parenthesis around the right operand in th…
DMartens Sep 6, 2022
6e00a16
fix: remove multiple parenthesis around the right operand in the logi…
DMartens Sep 6, 2022
bf51265
test: add data property for suggestions
DMartens Sep 6, 2022
5eb5c6b
docs: add missing comma in description
DMartens Sep 6, 2022
839145f
test: clean up unnecessary data, add missing data and pass missing op…
DMartens Sep 7, 2022
fde5e0f
fix: do not allow property accesses in a computed property when check…
DMartens Sep 7, 2022
180155d
test: add test cases for private identifiers
DMartens Sep 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
131 changes: 131 additions & 0 deletions docs/src/rules/logical-assignment-operators.md
@@ -0,0 +1,131 @@
---
title: logical-assignment-operators
layout: doc
rule_type: suggestion
---

ES2021 introduces the assignment operator shorthand for the logical operators `||`, `&&` and `??`.
Before, this was only allowed for mathematical operations such as `+` or `*` (see the rule [operator-assignment](./operator-assignment)).
The shorthand can be used if the assignment target and the left expression of a logical expression are the same.
For example `a = a || b` can be shortened to `a ||= b`.

## Rule Details

This rule requires or disallows logical assignment operator shorthand.

### Options

This rule has a string and an object option.
String option:

* `"always"` (default)
* `"never"`

Object option (only available if string option is set to `"always"`):

* `"enforceForIfStatements": false`(default) Do *not* check for equivalent `if` statements
* `"enforceForIfStatements": true` Check for equivalent `if` statements

#### always

Examples of **incorrect** code for this rule with the default `"always"` option:

::: incorrect

```js
/*eslint logical-assignment-operators: ["error", "always"]*/

a = a || b
a = a && b
a = a ?? b
a || (a = b)
a && (a = b)
a ?? (a = b)
```

:::

Examples of **correct** code for this rule with the default `"always"` option:

::: correct

```js
/*eslint logical-assignment-operators: ["error", "always"]*/

a = b
a += b
a ||= b
a = b || c
a || (b = c)

if (a) a = b
```

:::

#### never

Examples of **incorrect** code for this rule with the `"never"` option:

::: incorrect

```js
/*eslint logical-assignment-operators: ["error", "never"]*/

a ||= b
a &&= b
DMartens marked this conversation as resolved.
Show resolved Hide resolved
a ??= b
```

:::

Examples of **correct** code for this rule with the `"never"` option:

::: correct

```js
/*eslint logical-assignment-operators: ["error", "never"]*/

a = a || b
a = a && b
a = a ?? b
```

:::

#### enforceForIfStatements

This option checks for additional patterns with if statements which could be expressed with the logical assignment operator.

::: incorrect

Examples of **incorrect** code for this rule with the `["always", { enforceIfStatements: true }]` option:

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

if (a) a = b // <=> a &&= b
if (!a) a = b // <=> a ||= b

if (a == null) a = b // <=> a ??= b
if (a === null || a === undefined) a = b // <=> a ??= b
```

:::

Examples of **correct** code for this rule with the `["always", { enforceIfStatements: true }]` option:

::: correct

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

if (a) b = c
if (a === 0) a = b
```

:::

## When Not To Use It

Use of logical operator assignment shorthand is a stylistic choice. Leaving this rule turned off would allow developers to choose which style is more readable on a case-by-case basis.
1 change: 1 addition & 0 deletions lib/rules/index.js
Expand Up @@ -72,6 +72,7 @@ module.exports = new LazyLoadingRuleMap(Object.entries({
"lines-around-comment": () => require("./lines-around-comment"),
"lines-around-directive": () => require("./lines-around-directive"),
"lines-between-class-members": () => require("./lines-between-class-members"),
"logical-assignment-operators": () => require("./logical-assignment-operators"),
"max-classes-per-file": () => require("./max-classes-per-file"),
"max-depth": () => require("./max-depth"),
"max-len": () => require("./max-len"),
Expand Down