Skip to content

Commit 548b7df

Browse files
feat(eslint-plugin): [restrict-plus-operands] add allow* options from restrict-template-expressions
1 parent e458b5a commit 548b7df

File tree

4 files changed

+940
-340
lines changed

4 files changed

+940
-340
lines changed

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

Lines changed: 100 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,70 +18,145 @@ This rule reports when a `+` operation combines two values of different types, o
1818
### ❌ Incorrect
1919

2020
```ts
21-
var foo = '5.5' + 5;
22-
var foo = 1n + 1;
21+
let foo = '5.5' + 5;
22+
let foo = 1n + 1;
2323
```
2424

2525
### ✅ Correct
2626

2727
```ts
28-
var foo = parseInt('5.5', 10) + 10;
29-
var foo = 1n + 1n;
28+
let foo = parseInt('5.5', 10) + 10;
29+
let foo = 1n + 1n;
3030
```
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 }]*/
43+
let fn = (a: number, b: []) => a + b;
44+
let fn = (a: string, b: []) => a + b;
45+
```
4446

45-
let foo: string | undefined;
46-
foo += 'some data';
47+
#### ✅ Correct
4748

48-
let bar: string = '';
49-
bar += 0;
49+
```ts
50+
let fn = (a: number, b: any) => a + b;
51+
let fn = (a: string, b: any) => a + b;
52+
```
53+
54+
### `allowBoolean`
55+
56+
Examples of code for this rule with `{ allowBoolean: true }`:
57+
58+
<!--tabs-->
59+
60+
#### ❌ Incorrect
61+
62+
```ts
63+
let fn = (a: number, b: unknown) => a + b;
64+
let fn = (a: string, b: unknown) => a + b;
5065
```
5166

5267
#### ✅ Correct
5368

5469
```ts
55-
/*eslint @typescript-eslint/restrict-plus-operands: ["error", { "checkCompoundAssignments": true }]*/
70+
let fn = (a: number, b: boolean) => a + b;
71+
let fn = (a: string, b: boolean) => a + b;
72+
```
5673

57-
let foo: number = 0;
58-
foo += 1;
74+
### `allowNullish`
5975

60-
let bar = '';
61-
bar += 'test';
76+
Examples of code for this rule with `{ allowNullish: true }`:
77+
78+
<!--tabs-->
79+
80+
#### ❌ Incorrect
81+
82+
```ts
83+
let fn = (a: number, b: unknown) => a + b;
84+
let fn = (a: number, b: never) => a + b;
85+
let fn = (a: string, b: unknown) => a + b;
86+
let fn = (a: string, b: never) => a + b;
6287
```
6388

64-
### `allowAny`
89+
#### ✅ Correct
6590

66-
Examples of code for this rule with `{ allowAny: true }`:
91+
```ts
92+
let fn = (a: number, b: undefined) => a + b;
93+
let fn = (a: number, b: null) => a + b;
94+
let fn = (a: string, b: undefined) => a + b;
95+
let fn = (a: string, b: null) => a + b;
96+
```
97+
98+
### `allowNumberAndString`
99+
100+
Examples of code for this rule with `{ allowNumberAndString: true }`:
101+
102+
<!--tabs-->
103+
104+
#### ❌ Incorrect
105+
106+
```ts
107+
let fn = (a: number, b: unknown) => a + b;
108+
let fn = (a: number, b: never) => a + b;
109+
```
110+
111+
#### ✅ Correct
112+
113+
```ts
114+
let fn = (a: number, b: string) => a + b;
115+
let fn = (a: number, b: number | string) => a + b;
116+
```
117+
118+
### `allowRegExp`
119+
120+
Examples of code for this rule with `{ allowRegExp: true }`:
67121

68122
<!--tabs-->
69123

70124
#### ❌ Incorrect
71125

72126
```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;
127+
let fn = (a: number, b: RegExp) => a + b;
76128
```
77129

78130
#### ✅ Correct
79131

80132
```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;
133+
let fn = (a: string, b: RegExp) => a + b;
134+
```
135+
136+
### `checkCompoundAssignments`
137+
138+
Examples of code for this rule with `{ checkCompoundAssignments: true }`:
139+
140+
<!--tabs-->
141+
142+
#### ❌ Incorrect
143+
144+
```ts
145+
let foo: string | undefined;
146+
foo += 'some data';
147+
148+
let bar: string = '';
149+
bar += 0;
150+
```
151+
152+
#### ✅ Correct
153+
154+
```ts
155+
let foo: number = 0;
156+
foo += 1;
157+
158+
let bar = '';
159+
bar += 'test';
85160
```
86161

87162
## When Not To Use It

0 commit comments

Comments
 (0)