Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(eslint-plugin): [restrict-plus-operands] add better error messages #4332

Merged
merged 4 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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