From 27e6f76620dab76b2a045ba6b6f44142ed533331 Mon Sep 17 00:00:00 2001 From: lonyele Date: Mon, 20 Dec 2021 21:02:33 +0900 Subject: [PATCH 1/2] feat: add better error messages --- .../src/rules/restrict-plus-operands.ts | 27 +++++++++++++------ .../rules/restrict-plus-operands.test.ts | 22 +++++++-------- 2 files changed, 30 insertions(+), 19 deletions(-) diff --git a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts index afcbf9abf2f3..ac5ad921e678 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, }, From 28db82712cb399f6833a2a2c0a5ee21568385b95 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Sun, 16 Jan 2022 22:30:10 -0800 Subject: [PATCH 2/2] nit typo --- packages/eslint-plugin/src/rules/restrict-plus-operands.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts index ac5ad921e678..5c8c5a64f2cf 100644 --- a/packages/eslint-plugin/src/rules/restrict-plus-operands.ts +++ b/packages/eslint-plugin/src/rules/restrict-plus-operands.ts @@ -34,7 +34,7 @@ export default util.createRule({ 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)", + "Operands of '+' operation must either be one of string, number, bigint or any (if allowed by option)", }, schema: [ {