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] change checkCompoundAssignments to skipCompoundAssignments #7027

Merged
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 27 additions & 25 deletions packages/eslint-plugin/docs/rules/restrict-plus-operands.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,57 +31,59 @@ var foo = 1n + 1n;

## Options

### `checkCompoundAssignments`
### `allowAny`

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 foo: string | undefined;
foo += 'some data';

let bar: string = '';
bar += 0;
var fn = (a: any, b: boolean) => a + b;
var fn = (a: any, b: []) => a + b;
var fn = (a: any, b: {}) => a + b;
```

#### ✅ Correct

```ts
/*eslint @typescript-eslint/restrict-plus-operands: ["error", { "checkCompoundAssignments": true }]*/

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

let bar = '';
bar += 'test';
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;
```

### `allowAny`
### `skipCompoundAssignments`

Examples of code for this rule with `{ allowAny: true }`:
Whether to skip checking `+=` assignments.

Examples of code for this rule with `{ skipCompoundAssignments: 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;
/*eslint @typescript-eslint/restrict-plus-operands: ["error", { "skipCompoundAssignments": true }]*/

let numeric = 0;
numeric = numeric + 'some data';

let stringy = 'some data';
stringy = stringy + 0;
```

#### ✅ 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;
/*eslint @typescript-eslint/restrict-plus-operands: ["error", { "skipCompoundAssignments": true }]*/

let numeric = 0;
numeric += 'some data';

let stringy = 'some data';
stringy += 0;
```

## When Not To Use It
Expand Down
17 changes: 9 additions & 8 deletions packages/eslint-plugin/src/rules/restrict-plus-operands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import * as util from '../util';

type Options = [
{
checkCompoundAssignments?: boolean;
allowAny?: boolean;
skipCompoundAssignments?: boolean;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if renaming it is overall super high value given the disruptive nature of the change. We do have precedence for "truthy-by-default" options like the options in strict-boolean-expressions.

I'll leave it up to you.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm yeah I see what you mean... I think the fact that we're turning on the behavior by default in the new config makes me feel better about it. Also not that many folks enable the option: https://sourcegraph.com/search?q=context:global+checkCompoundAssignments+lang:js+-file:node_modules&patternType=standard&sm=0&groupBy=path (current result count: 58).

},
];
type MessageIds =
Expand Down Expand Up @@ -42,25 +42,26 @@ export default util.createRule<Options, MessageIds>({
type: 'object',
additionalProperties: false,
properties: {
checkCompoundAssignments: {
description: 'Whether to check compound assignments such as `+=`.',
type: 'boolean',
},
allowAny: {
description: 'Whether to allow `any` typed values.',
type: 'boolean',
},
skipCompoundAssignments: {
description:
'Whether to skip checking compound assignments such as `+=`.',
type: 'boolean',
},
},
},
],
},
defaultOptions: [
{
checkCompoundAssignments: false,
allowAny: false,
skipCompoundAssignments: false,
},
],
create(context, [{ checkCompoundAssignments, allowAny }]) {
create(context, [{ allowAny, skipCompoundAssignments }]) {
const services = util.getParserServices(context);
const checker = services.program.getTypeChecker();

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

return {
"BinaryExpression[operator='+']": checkPlusOperands,
...(checkCompoundAssignments && {
...(!skipCompoundAssignments && {
"AssignmentExpression[operator='+=']"(node): void {
checkPlusOperands(node);
},
Expand Down
30 changes: 10 additions & 20 deletions packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,23 @@ const b = A('') + '!';
`,
{
code: `
let foo: number = 0;
foo += 1;
let foo = '';
foo += 0;
`,
options: [
{
checkCompoundAssignments: false,
skipCompoundAssignments: true,
},
],
},
{
code: `
let foo: number = 0;
foo += 'string';
let foo = 0;
foo += '';
`,
options: [
{
checkCompoundAssignments: false,
skipCompoundAssignments: true,
},
],
},
Expand Down Expand Up @@ -801,14 +801,9 @@ function foo<T extends 1>(a: T) {
},
{
code: `
let foo: string | undefined;
foo += 'some data';
let foo: string = '';
foo += 1;
`,
options: [
{
checkCompoundAssignments: true,
},
],
errors: [
{
messageId: 'notStrings',
Expand All @@ -819,14 +814,9 @@ foo += 'some data';
},
{
code: `
let foo = '';
foo += 0;
let foo = 0;
foo += '';
`,
options: [
{
checkCompoundAssignments: true,
},
],
errors: [
{
messageId: 'notStrings',
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.