Skip to content

Commit dc801d8

Browse files
feat(eslint-plugin): [restrict-plus-operands] change checkCompoundAssignments to skipCompoundAssignments (#7027)
* feat(eslint-plugin): [restrict-plus-operands] change checkCompoundAssignments to skipCompoundAssignments * Update schema-snapshots
1 parent e6235bf commit dc801d8

File tree

4 files changed

+50
-57
lines changed

4 files changed

+50
-57
lines changed

‎packages/eslint-plugin/docs/rules/restrict-plus-operands.md‎

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,57 +31,59 @@ var foo = 1n + 1n;
3131

3232
## Options
3333

34-
### `checkCompoundAssignments`
34+
### `allowAny`
3535

36-
Examples of code for this rule with `{ checkCompoundAssignments: true }`:
36+
Examples of code for this rule with `{ allowAny: true }`:
3737

3838
<!--tabs-->
3939

4040
#### ❌ Incorrect
4141

4242
```ts
43-
/*eslint @typescript-eslint/restrict-plus-operands: ["error", { "checkCompoundAssignments": true }]*/
44-
45-
let foo: string | undefined;
46-
foo += 'some data';
47-
48-
let bar: string = '';
49-
bar += 0;
43+
var fn = (a: any, b: boolean) => a + b;
44+
var fn = (a: any, b: []) => a + b;
45+
var fn = (a: any, b: {}) => a + b;
5046
```
5147

5248
#### ✅ Correct
5349

5450
```ts
55-
/*eslint @typescript-eslint/restrict-plus-operands: ["error", { "checkCompoundAssignments": true }]*/
56-
57-
let foo: number = 0;
58-
foo += 1;
59-
60-
let bar = '';
61-
bar += 'test';
51+
var fn = (a: any, b: any) => a + b;
52+
var fn = (a: any, b: string) => a + b;
53+
var fn = (a: any, b: bigint) => a + b;
54+
var fn = (a: any, b: number) => a + b;
6255
```
6356

64-
### `allowAny`
57+
### `skipCompoundAssignments`
6558

66-
Examples of code for this rule with `{ allowAny: true }`:
59+
Whether to skip checking `+=` assignments.
60+
61+
Examples of code for this rule with `{ skipCompoundAssignments: true }`:
6762

6863
<!--tabs-->
6964

7065
#### ❌ Incorrect
7166

7267
```ts
73-
var fn = (a: any, b: boolean) => a + b;
74-
var fn = (a: any, b: []) => a + b;
75-
var fn = (a: any, b: {}) => a + b;
68+
/*eslint @typescript-eslint/restrict-plus-operands: ["error", { "skipCompoundAssignments": true }]*/
69+
70+
let numeric = 0;
71+
numeric = numeric + 'some data';
72+
73+
let stringy = 'some data';
74+
stringy = stringy + 0;
7675
```
7776

7877
#### ✅ Correct
7978

8079
```ts
81-
var fn = (a: any, b: any) => a + b;
82-
var fn = (a: any, b: string) => a + b;
83-
var fn = (a: any, b: bigint) => a + b;
84-
var fn = (a: any, b: number) => a + b;
80+
/*eslint @typescript-eslint/restrict-plus-operands: ["error", { "skipCompoundAssignments": true }]*/
81+
82+
let numeric = 0;
83+
numeric += 'some data';
84+
85+
let stringy = 'some data';
86+
stringy += 0;
8587
```
8688

8789
## When Not To Use It

‎packages/eslint-plugin/src/rules/restrict-plus-operands.ts‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import * as util from '../util';
55

66
type Options = [
77
{
8-
checkCompoundAssignments?: boolean;
98
allowAny?: boolean;
9+
skipCompoundAssignments?: boolean;
1010
},
1111
];
1212
type MessageIds =
@@ -42,25 +42,26 @@ export default util.createRule<Options, MessageIds>({
4242
type: 'object',
4343
additionalProperties: false,
4444
properties: {
45-
checkCompoundAssignments: {
46-
description: 'Whether to check compound assignments such as `+=`.',
47-
type: 'boolean',
48-
},
4945
allowAny: {
5046
description: 'Whether to allow `any` typed values.',
5147
type: 'boolean',
5248
},
49+
skipCompoundAssignments: {
50+
description:
51+
'Whether to skip checking compound assignments such as `+=`.',
52+
type: 'boolean',
53+
},
5354
},
5455
},
5556
],
5657
},
5758
defaultOptions: [
5859
{
59-
checkCompoundAssignments: false,
6060
allowAny: false,
61+
skipCompoundAssignments: false,
6162
},
6263
],
63-
create(context, [{ checkCompoundAssignments, allowAny }]) {
64+
create(context, [{ allowAny, skipCompoundAssignments }]) {
6465
const services = util.getParserServices(context);
6566
const checker = services.program.getTypeChecker();
6667

@@ -191,7 +192,7 @@ export default util.createRule<Options, MessageIds>({
191192

192193
return {
193194
"BinaryExpression[operator='+']": checkPlusOperands,
194-
...(checkCompoundAssignments && {
195+
...(!skipCompoundAssignments && {
195196
"AssignmentExpression[operator='+=']"(node): void {
196197
checkPlusOperands(node);
197198
},

‎packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts‎

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,23 @@ const b = A('') + '!';
161161
`,
162162
{
163163
code: `
164-
let foo: number = 0;
165-
foo += 1;
164+
let foo = '';
165+
foo += 0;
166166
`,
167167
options: [
168168
{
169-
checkCompoundAssignments: false,
169+
skipCompoundAssignments: true,
170170
},
171171
],
172172
},
173173
{
174174
code: `
175-
let foo: number = 0;
176-
foo += 'string';
175+
let foo = 0;
176+
foo += '';
177177
`,
178178
options: [
179179
{
180-
checkCompoundAssignments: false,
180+
skipCompoundAssignments: true,
181181
},
182182
],
183183
},
@@ -801,14 +801,9 @@ function foo<T extends 1>(a: T) {
801801
},
802802
{
803803
code: `
804-
let foo: string | undefined;
805-
foo += 'some data';
804+
let foo: string = '';
805+
foo += 1;
806806
`,
807-
options: [
808-
{
809-
checkCompoundAssignments: true,
810-
},
811-
],
812807
errors: [
813808
{
814809
messageId: 'notStrings',
@@ -819,14 +814,9 @@ foo += 'some data';
819814
},
820815
{
821816
code: `
822-
let foo = '';
823-
foo += 0;
817+
let foo = 0;
818+
foo += '';
824819
`,
825-
options: [
826-
{
827-
checkCompoundAssignments: true,
828-
},
829-
],
830820
errors: [
831821
{
832822
messageId: 'notStrings',

‎packages/eslint-plugin/tests/schema-snapshots/restrict-plus-operands.shot‎

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)