Navigation Menu

Skip to content

Commit

Permalink
feat(eslint-plugin): [restrict-plus-operand] add allowAny option (#4260)
Browse files Browse the repository at this point in the history
  • Loading branch information
lonyele committed Dec 20, 2021
1 parent ff0adf9 commit 2788545
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 18 deletions.
42 changes: 38 additions & 4 deletions packages/eslint-plugin/docs/rules/restrict-plus-operands.md
Expand Up @@ -22,14 +22,25 @@ var foo = 1n + 1n;

## Options

This rule has an object option:
The rule accepts an options object with the following properties:

- `"checkCompoundAssignments": false`: (default) does not check compound assignments (`+=`)
- `"checkCompoundAssignments": true`
```ts
type Options = {
// if true, check compound assignments (`+=`)
checkCompoundAssignments?: boolean;
// if true, 'any' itself and `string`,`bigint`, `number` is allowed.
allowAny?: boolean;
};

const defaults = {
checkCompoundAssignments: false,
allowAny: false,
};
```

### `checkCompoundAssignments`

Examples of code for the `{ "checkCompoundAssignments": true }` option:
Examples of code for this rule with `{ checkCompoundAssignments: true }`:

<!--tabs-->

Expand Down Expand Up @@ -57,6 +68,29 @@ let bar = '';
bar += 'test';
```

### `allowAny`

Examples of code for this rule with `{ allowAny: true }`:

<!--tabs-->

#### ❌ Incorrect

```ts
var fn = (a: any, b: boolean) => a + b;
var fn = (a: any, b: []) => a + b;
var fn = (a: any, b: {}) => a + b;
```

#### ✅ Correct

```ts
var fn = (a: any, b: any) => a + b;
var fn = (a: any, b: string) => a + b;
var fn = (a: any, b: bigint) => a + b;
var fn = (a: any, b: number) => a + b;
```

## How to Use

```json
Expand Down
57 changes: 44 additions & 13 deletions packages/eslint-plugin/src/rules/restrict-plus-operands.ts
Expand Up @@ -5,6 +5,7 @@ import * as util from '../util';
type Options = [
{
checkCompoundAssignments?: boolean;
allowAny?: boolean;
},
];
type MessageIds = 'notNumbers' | 'notStrings' | 'notBigInts';
Expand Down Expand Up @@ -34,20 +35,24 @@ export default util.createRule<Options, MessageIds>({
checkCompoundAssignments: {
type: 'boolean',
},
allowAny: {
type: 'boolean',
},
},
},
],
},
defaultOptions: [
{
checkCompoundAssignments: false,
allowAny: false,
},
],
create(context, [{ checkCompoundAssignments }]) {
create(context, [{ checkCompoundAssignments, allowAny }]) {
const service = util.getParserServices(context);
const typeChecker = service.program.getTypeChecker();

type BaseLiteral = 'string' | 'number' | 'bigint' | 'invalid';
type BaseLiteral = 'string' | 'number' | 'bigint' | 'invalid' | 'any';

/**
* Helper function to get base type of node
Expand Down Expand Up @@ -82,7 +87,8 @@ export default util.createRule<Options, MessageIds>({
if (
stringType === 'number' ||
stringType === 'string' ||
stringType === 'bigint'
stringType === 'bigint' ||
stringType === 'any'
) {
return stringType;
}
Expand All @@ -108,28 +114,53 @@ export default util.createRule<Options, MessageIds>({
const leftType = getNodeType(node.left);
const rightType = getNodeType(node.right);

if (
leftType === 'invalid' ||
rightType === 'invalid' ||
leftType !== rightType
) {
if (leftType === 'string' || rightType === 'string') {
if (leftType === rightType) {
if (leftType === 'invalid') {
context.report({
node,
messageId: 'notStrings',
messageId: 'notNumbers',
});
} else if (leftType === 'bigint' || rightType === 'bigint') {
}

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

return;
}

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

return;
}

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

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

context.report({
node,
messageId: 'notNumbers',
});
}

return {
Expand Down
162 changes: 161 additions & 1 deletion packages/eslint-plugin/tests/rules/restrict-plus-operands.test.ts
Expand Up @@ -139,6 +139,46 @@ foo += 'string';
},
],
},
{
code: `
export const f = (a: any, b: any) => a + b;
`,
options: [
{
allowAny: true,
},
],
},
{
code: `
export const f = (a: any, b: string) => a + b;
`,
options: [
{
allowAny: true,
},
],
},
{
code: `
export const f = (a: any, b: bigint) => a + b;
`,
options: [
{
allowAny: true,
},
],
},
{
code: `
export const f = (a: any, b: number) => a + b;
`,
options: [
{
allowAny: true,
},
],
},
],
invalid: [
{
Expand Down Expand Up @@ -515,7 +555,7 @@ function foo<T extends 1>(a: T) {
`,
errors: [
{
messageId: 'notStrings',
messageId: 'notNumbers',
line: 4,
column: 19,
},
Expand Down Expand Up @@ -571,5 +611,125 @@ foo += 0;
},
],
},
{
code: `
const f = (a: any, b: boolean) => a + b;
`,
options: [
{
allowAny: true,
},
],
errors: [
{
messageId: 'notNumbers',
line: 2,
column: 35,
},
],
},
{
code: `
const f = (a: any, b: []) => a + b;
`,
options: [
{
allowAny: true,
},
],
errors: [
{
messageId: 'notNumbers',
line: 2,
column: 30,
},
],
},

{
code: `
const f = (a: any, b: any) => a + b;
`,
options: [
{
allowAny: false,
},
],
errors: [
{
messageId: 'notNumbers',
line: 2,
column: 31,
},
],
},
{
code: `
const f = (a: any, b: string) => a + b;
`,
options: [
{
allowAny: false,
},
],
errors: [
{
messageId: 'notNumbers',
line: 2,
column: 34,
},
],
},
{
code: `
const f = (a: any, b: bigint) => a + b;
`,
options: [
{
allowAny: false,
},
],
errors: [
{
messageId: 'notNumbers',
line: 2,
column: 34,
},
],
},
{
code: `
const f = (a: any, b: number) => a + b;
`,
options: [
{
allowAny: false,
},
],
errors: [
{
messageId: 'notNumbers',
line: 2,
column: 34,
},
],
},
{
code: `
const f = (a: any, b: boolean) => a + b;
`,
options: [
{
allowAny: false,
},
],
errors: [
{
messageId: 'notNumbers',
line: 2,
column: 35,
},
],
},
],
});

0 comments on commit 2788545

Please sign in to comment.