diff --git a/packages/eslint-plugin/docs/rules/restrict-plus-operands.md b/packages/eslint-plugin/docs/rules/restrict-plus-operands.md index fc823f6da9f8..1689160f7f6e 100644 --- a/packages/eslint-plugin/docs/rules/restrict-plus-operands.md +++ b/packages/eslint-plugin/docs/rules/restrict-plus-operands.md @@ -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 }`: #### ❌ 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 }`: #### ❌ 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 diff --git a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts index 7232d610911e..4bda765c8565 100644 --- a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts +++ b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts @@ -5,8 +5,8 @@ import * as util from '../util'; type Options = [ { - checkCompoundAssignments?: boolean; allowAny?: boolean; + skipCompoundAssignments?: boolean; }, ]; type MessageIds = @@ -42,25 +42,26 @@ export default util.createRule({ 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(); @@ -191,7 +192,7 @@ export default util.createRule({ return { "BinaryExpression[operator='+']": checkPlusOperands, - ...(checkCompoundAssignments && { + ...(!skipCompoundAssignments && { "AssignmentExpression[operator='+=']"(node): void { checkPlusOperands(node); }, diff --git a/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts b/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts index 8ccb8bdb5fe8..d5ff0e57429a 100644 --- a/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts +++ b/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts @@ -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, }, ], }, @@ -801,14 +801,9 @@ function foo(a: T) { }, { code: ` -let foo: string | undefined; -foo += 'some data'; +let foo: string = ''; +foo += 1; `, - options: [ - { - checkCompoundAssignments: true, - }, - ], errors: [ { messageId: 'notStrings', @@ -819,14 +814,9 @@ foo += 'some data'; }, { code: ` -let foo = ''; -foo += 0; +let foo = 0; +foo += ''; `, - options: [ - { - checkCompoundAssignments: true, - }, - ], errors: [ { messageId: 'notStrings', diff --git a/packages/eslint-plugin/tests/schema-snapshots/restrict-plus-operands.shot b/packages/eslint-plugin/tests/schema-snapshots/restrict-plus-operands.shot index a1727d55fca7..726cfbf1aa9d 100644 --- a/packages/eslint-plugin/tests/schema-snapshots/restrict-plus-operands.shot +++ b/packages/eslint-plugin/tests/schema-snapshots/restrict-plus-operands.shot @@ -12,8 +12,8 @@ exports[`Rule schemas should be convertible to TS types for documentation purpos "description": "Whether to allow \`any\` typed values.", "type": "boolean" }, - "checkCompoundAssignments": { - "description": "Whether to check compound assignments such as \`+=\`.", + "skipCompoundAssignments": { + "description": "Whether to skip checking compound assignments such as \`+=\`.", "type": "boolean" } }, @@ -28,8 +28,8 @@ type Options = [ { /** Whether to allow \`any\` typed values. */ allowAny?: boolean; - /** Whether to check compound assignments such as \`+=\`. */ - checkCompoundAssignments?: boolean; + /** Whether to skip checking compound assignments such as \`+=\`. */ + skipCompoundAssignments?: boolean; }, ]; " pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy