diff --git a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts index 5ccae1aca105..d126dc42bfb0 100644 --- a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts +++ b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts @@ -8,7 +8,12 @@ type Options = [ allowAny?: boolean; }, ]; -type MessageIds = 'notNumbers' | 'notStrings' | 'notBigInts'; +type MessageIds = + | 'notNumbers' + | 'notStrings' + | 'notBigInts' + | 'notValidAnys' + | 'notValidTypes'; export default util.createRule({ name: 'restrict-plus-operands', @@ -26,6 +31,10 @@ export default util.createRule({ notStrings: "Operands of '+' operation must either be both strings or both numbers. Consider using a template literal.", notBigInts: "Operands of '+' operation must be both bigints.", + notValidAnys: + "Operands of '+' operation with any is possible only with string, number, bigint or any", + notValidTypes: + "Operands of '+' operation must either be one of string, number, bigint or any (if allowed by option)", }, schema: [ { @@ -118,14 +127,14 @@ export default util.createRule({ if (leftType === 'invalid') { context.report({ node, - messageId: 'notNumbers', + messageId: 'notValidTypes', }); } if (!allowAny && leftType === 'any') { context.report({ node, - messageId: 'notNumbers', + messageId: 'notValidAnys', }); } @@ -136,7 +145,7 @@ export default util.createRule({ if (!allowAny || leftType === 'invalid' || rightType === 'invalid') { context.report({ node, - messageId: 'notNumbers', + messageId: 'notValidAnys', }); } @@ -157,10 +166,12 @@ export default util.createRule({ }); } - context.report({ - node, - messageId: 'notNumbers', - }); + if (leftType === 'number' || rightType === 'number') { + return context.report({ + node, + messageId: 'notNumbers', + }); + } } return { 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 12b7e44657ea..ec14f26db7b3 100644 --- a/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts +++ b/packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts @@ -195,7 +195,7 @@ export const f = (a: any, b: number) => a + b; code: 'var foo = [] + {};', errors: [ { - messageId: 'notNumbers', + messageId: 'notValidTypes', line: 1, column: 11, }, @@ -225,7 +225,7 @@ export const f = (a: any, b: number) => a + b; code: 'var foo = [] + [];', errors: [ { - messageId: 'notNumbers', + messageId: 'notValidTypes', line: 1, column: 11, }, @@ -368,7 +368,7 @@ var foo = pair + pair; `, errors: [ { - messageId: 'notNumbers', + messageId: 'notValidTypes', line: 3, column: 11, }, @@ -555,7 +555,7 @@ function foo(a: T) { `, errors: [ { - messageId: 'notNumbers', + messageId: 'notValidAnys', line: 4, column: 19, }, @@ -622,7 +622,7 @@ const f = (a: any, b: boolean) => a + b; ], errors: [ { - messageId: 'notNumbers', + messageId: 'notValidAnys', line: 2, column: 35, }, @@ -639,7 +639,7 @@ const f = (a: any, b: []) => a + b; ], errors: [ { - messageId: 'notNumbers', + messageId: 'notValidAnys', line: 2, column: 30, }, @@ -657,7 +657,7 @@ const f = (a: any, b: any) => a + b; ], errors: [ { - messageId: 'notNumbers', + messageId: 'notValidAnys', line: 2, column: 31, }, @@ -674,7 +674,7 @@ const f = (a: any, b: string) => a + b; ], errors: [ { - messageId: 'notNumbers', + messageId: 'notValidAnys', line: 2, column: 34, }, @@ -691,7 +691,7 @@ const f = (a: any, b: bigint) => a + b; ], errors: [ { - messageId: 'notNumbers', + messageId: 'notValidAnys', line: 2, column: 34, }, @@ -708,7 +708,7 @@ const f = (a: any, b: number) => a + b; ], errors: [ { - messageId: 'notNumbers', + messageId: 'notValidAnys', line: 2, column: 34, }, @@ -725,7 +725,7 @@ const f = (a: any, b: boolean) => a + b; ], errors: [ { - messageId: 'notNumbers', + messageId: 'notValidAnys', line: 2, column: 35, },