Skip to content

Commit

Permalink
refactor(eslint-plugin): [restrict-plus-operands] add better error me…
Browse files Browse the repository at this point in the history
…ssages (#4332)
  • Loading branch information
lonyele committed Jan 17, 2022
1 parent ea85dda commit 95aea18
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
27 changes: 19 additions & 8 deletions packages/eslint-plugin/src/rules/restrict-plus-operands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ type Options = [
allowAny?: boolean;
},
];
type MessageIds = 'notNumbers' | 'notStrings' | 'notBigInts';
type MessageIds =
| 'notNumbers'
| 'notStrings'
| 'notBigInts'
| 'notValidAnys'
| 'notValidTypes';

export default util.createRule<Options, MessageIds>({
name: 'restrict-plus-operands',
Expand All @@ -26,6 +31,10 @@ export default util.createRule<Options, MessageIds>({
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: [
{
Expand Down Expand Up @@ -118,14 +127,14 @@ export default util.createRule<Options, MessageIds>({
if (leftType === 'invalid') {
context.report({
node,
messageId: 'notNumbers',
messageId: 'notValidTypes',
});
}

if (!allowAny && leftType === 'any') {
context.report({
node,
messageId: 'notNumbers',
messageId: 'notValidAnys',
});
}

Expand All @@ -136,7 +145,7 @@ export default util.createRule<Options, MessageIds>({
if (!allowAny || leftType === 'invalid' || rightType === 'invalid') {
context.report({
node,
messageId: 'notNumbers',
messageId: 'notValidAnys',
});
}

Expand All @@ -157,10 +166,12 @@ export default util.createRule<Options, MessageIds>({
});
}

context.report({
node,
messageId: 'notNumbers',
});
if (leftType === 'number' || rightType === 'number') {
return context.report({
node,
messageId: 'notNumbers',
});
}
}

return {
Expand Down
22 changes: 11 additions & 11 deletions packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -368,7 +368,7 @@ var foo = pair + pair;
`,
errors: [
{
messageId: 'notNumbers',
messageId: 'notValidTypes',
line: 3,
column: 11,
},
Expand Down Expand Up @@ -555,7 +555,7 @@ function foo<T extends 1>(a: T) {
`,
errors: [
{
messageId: 'notNumbers',
messageId: 'notValidAnys',
line: 4,
column: 19,
},
Expand Down Expand Up @@ -622,7 +622,7 @@ const f = (a: any, b: boolean) => a + b;
],
errors: [
{
messageId: 'notNumbers',
messageId: 'notValidAnys',
line: 2,
column: 35,
},
Expand All @@ -639,7 +639,7 @@ const f = (a: any, b: []) => a + b;
],
errors: [
{
messageId: 'notNumbers',
messageId: 'notValidAnys',
line: 2,
column: 30,
},
Expand All @@ -657,7 +657,7 @@ const f = (a: any, b: any) => a + b;
],
errors: [
{
messageId: 'notNumbers',
messageId: 'notValidAnys',
line: 2,
column: 31,
},
Expand All @@ -674,7 +674,7 @@ const f = (a: any, b: string) => a + b;
],
errors: [
{
messageId: 'notNumbers',
messageId: 'notValidAnys',
line: 2,
column: 34,
},
Expand All @@ -691,7 +691,7 @@ const f = (a: any, b: bigint) => a + b;
],
errors: [
{
messageId: 'notNumbers',
messageId: 'notValidAnys',
line: 2,
column: 34,
},
Expand All @@ -708,7 +708,7 @@ const f = (a: any, b: number) => a + b;
],
errors: [
{
messageId: 'notNumbers',
messageId: 'notValidAnys',
line: 2,
column: 34,
},
Expand All @@ -725,7 +725,7 @@ const f = (a: any, b: boolean) => a + b;
],
errors: [
{
messageId: 'notNumbers',
messageId: 'notValidAnys',
line: 2,
column: 35,
},
Expand Down

0 comments on commit 95aea18

Please sign in to comment.