Skip to content

Commit 95aea18

Browse files
authored
refactor(eslint-plugin): [restrict-plus-operands] add better error messages (typescript-eslint#4332)
1 parent ea85dda commit 95aea18

File tree

2 files changed

+30
-19
lines changed

2 files changed

+30
-19
lines changed

packages/eslint-plugin/src/rules/restrict-plus-operands.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ type Options = [
88
allowAny?: boolean;
99
},
1010
];
11-
type MessageIds = 'notNumbers' | 'notStrings' | 'notBigInts';
11+
type MessageIds =
12+
| 'notNumbers'
13+
| 'notStrings'
14+
| 'notBigInts'
15+
| 'notValidAnys'
16+
| 'notValidTypes';
1217

1318
export default util.createRule<Options, MessageIds>({
1419
name: 'restrict-plus-operands',
@@ -26,6 +31,10 @@ export default util.createRule<Options, MessageIds>({
2631
notStrings:
2732
"Operands of '+' operation must either be both strings or both numbers. Consider using a template literal.",
2833
notBigInts: "Operands of '+' operation must be both bigints.",
34+
notValidAnys:
35+
"Operands of '+' operation with any is possible only with string, number, bigint or any",
36+
notValidTypes:
37+
"Operands of '+' operation must either be one of string, number, bigint or any (if allowed by option)",
2938
},
3039
schema: [
3140
{
@@ -118,14 +127,14 @@ export default util.createRule<Options, MessageIds>({
118127
if (leftType === 'invalid') {
119128
context.report({
120129
node,
121-
messageId: 'notNumbers',
130+
messageId: 'notValidTypes',
122131
});
123132
}
124133

125134
if (!allowAny && leftType === 'any') {
126135
context.report({
127136
node,
128-
messageId: 'notNumbers',
137+
messageId: 'notValidAnys',
129138
});
130139
}
131140

@@ -136,7 +145,7 @@ export default util.createRule<Options, MessageIds>({
136145
if (!allowAny || leftType === 'invalid' || rightType === 'invalid') {
137146
context.report({
138147
node,
139-
messageId: 'notNumbers',
148+
messageId: 'notValidAnys',
140149
});
141150
}
142151

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

160-
context.report({
161-
node,
162-
messageId: 'notNumbers',
163-
});
169+
if (leftType === 'number' || rightType === 'number') {
170+
return context.report({
171+
node,
172+
messageId: 'notNumbers',
173+
});
174+
}
164175
}
165176

166177
return {

packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ export const f = (a: any, b: number) => a + b;
195195
code: 'var foo = [] + {};',
196196
errors: [
197197
{
198-
messageId: 'notNumbers',
198+
messageId: 'notValidTypes',
199199
line: 1,
200200
column: 11,
201201
},
@@ -225,7 +225,7 @@ export const f = (a: any, b: number) => a + b;
225225
code: 'var foo = [] + [];',
226226
errors: [
227227
{
228-
messageId: 'notNumbers',
228+
messageId: 'notValidTypes',
229229
line: 1,
230230
column: 11,
231231
},
@@ -368,7 +368,7 @@ var foo = pair + pair;
368368
`,
369369
errors: [
370370
{
371-
messageId: 'notNumbers',
371+
messageId: 'notValidTypes',
372372
line: 3,
373373
column: 11,
374374
},
@@ -555,7 +555,7 @@ function foo<T extends 1>(a: T) {
555555
`,
556556
errors: [
557557
{
558-
messageId: 'notNumbers',
558+
messageId: 'notValidAnys',
559559
line: 4,
560560
column: 19,
561561
},
@@ -622,7 +622,7 @@ const f = (a: any, b: boolean) => a + b;
622622
],
623623
errors: [
624624
{
625-
messageId: 'notNumbers',
625+
messageId: 'notValidAnys',
626626
line: 2,
627627
column: 35,
628628
},
@@ -639,7 +639,7 @@ const f = (a: any, b: []) => a + b;
639639
],
640640
errors: [
641641
{
642-
messageId: 'notNumbers',
642+
messageId: 'notValidAnys',
643643
line: 2,
644644
column: 30,
645645
},
@@ -657,7 +657,7 @@ const f = (a: any, b: any) => a + b;
657657
],
658658
errors: [
659659
{
660-
messageId: 'notNumbers',
660+
messageId: 'notValidAnys',
661661
line: 2,
662662
column: 31,
663663
},
@@ -674,7 +674,7 @@ const f = (a: any, b: string) => a + b;
674674
],
675675
errors: [
676676
{
677-
messageId: 'notNumbers',
677+
messageId: 'notValidAnys',
678678
line: 2,
679679
column: 34,
680680
},
@@ -691,7 +691,7 @@ const f = (a: any, b: bigint) => a + b;
691691
],
692692
errors: [
693693
{
694-
messageId: 'notNumbers',
694+
messageId: 'notValidAnys',
695695
line: 2,
696696
column: 34,
697697
},
@@ -708,7 +708,7 @@ const f = (a: any, b: number) => a + b;
708708
],
709709
errors: [
710710
{
711-
messageId: 'notNumbers',
711+
messageId: 'notValidAnys',
712712
line: 2,
713713
column: 34,
714714
},
@@ -725,7 +725,7 @@ const f = (a: any, b: boolean) => a + b;
725725
],
726726
errors: [
727727
{
728-
messageId: 'notNumbers',
728+
messageId: 'notValidAnys',
729729
line: 2,
730730
column: 35,
731731
},

0 commit comments

Comments
 (0)