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(eslint-plugin): [restrict-plus-operands] add allow* options #6161

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
125 changes: 100 additions & 25 deletions packages/eslint-plugin/docs/rules/restrict-plus-operands.md
Expand Up @@ -18,70 +18,145 @@ This rule reports when a `+` operation combines two values of different types, o
### ❌ Incorrect

```ts
var foo = '5.5' + 5;
var foo = 1n + 1;
let foo = '5.5' + 5;
let foo = 1n + 1;
```

### ✅ Correct

```ts
var foo = parseInt('5.5', 10) + 10;
var foo = 1n + 1n;
let foo = parseInt('5.5', 10) + 10;
let foo = 1n + 1n;
```

## Options

### `checkCompoundAssignments`
### `allowAny`
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

Examples of code for this rule with `{ checkCompoundAssignments: true }`:
Examples of code for this rule with `{ allowAny: true }`:

<!--tabs-->

#### ❌ Incorrect

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

let foo: string | undefined;
foo += 'some data';
#### ✅ Correct

let bar: string = '';
bar += 0;
```ts
let fn = (a: number, b: any) => a + b;
let fn = (a: string, b: any) => a + b;
```

### `allowBoolean`

Examples of code for this rule with `{ allowBoolean: true }`:

<!--tabs-->

#### ❌ Incorrect

```ts
let fn = (a: number, b: unknown) => a + b;
let fn = (a: string, b: unknown) => a + b;
```

#### ✅ Correct

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

let foo: number = 0;
foo += 1;
### `allowNullish`

let bar = '';
bar += 'test';
Examples of code for this rule with `{ allowNullish: true }`:

<!--tabs-->

#### ❌ Incorrect

```ts
let fn = (a: number, b: unknown) => a + b;
let fn = (a: number, b: never) => a + b;
let fn = (a: string, b: unknown) => a + b;
let fn = (a: string, b: never) => a + b;
```

### `allowAny`
#### ✅ Correct

Examples of code for this rule with `{ allowAny: true }`:
```ts
let fn = (a: number, b: undefined) => a + b;
let fn = (a: number, b: null) => a + b;
let fn = (a: string, b: undefined) => a + b;
let fn = (a: string, b: null) => a + b;
```

### `allowNumberAndString`

Examples of code for this rule with `{ allowNumberAndString: true }`:

<!--tabs-->

#### ❌ Incorrect

```ts
let fn = (a: number, b: unknown) => a + b;
let fn = (a: number, b: never) => a + b;
```

#### ✅ Correct

```ts
let fn = (a: number, b: string) => a + b;
let fn = (a: number, b: number | string) => a + b;
```

### `allowRegExp`

Examples of code for this rule with `{ allowRegExp: true }`:

<!--tabs-->

#### ❌ Incorrect

```ts
var fn = (a: any, b: boolean) => a + b;
var fn = (a: any, b: []) => a + b;
var fn = (a: any, b: {}) => a + b;
let fn = (a: number, b: RegExp) => a + b;
```

#### ✅ Correct

```ts
var fn = (a: any, b: any) => a + b;
var fn = (a: any, b: string) => a + b;
var fn = (a: any, b: bigint) => a + b;
var fn = (a: any, b: number) => a + b;
let fn = (a: string, b: RegExp) => a + b;
```

### `checkCompoundAssignments`

Examples of code for this rule with `{ checkCompoundAssignments: true }`:

<!--tabs-->

#### ❌ Incorrect

```ts
let foo: string | undefined;
foo += 'some data';

let bar: string = '';
bar += 0;
```

#### ✅ Correct

```ts
let foo: number = 0;
foo += 1;

let bar = '';
bar += 'test';
```

## When Not To Use It
Expand Down