Skip to content

Commit

Permalink
feat(eslint-plugin): [explicit-function-return-type] add option to al…
Browse files Browse the repository at this point in the history
…low concise arrows that start with void (#1732)
  • Loading branch information
G-Rath committed Apr 13, 2020
1 parent 3eee804 commit 2e9c202
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/eslint-plugin/docs/rules/explicit-function-return-type.md
Expand Up @@ -69,6 +69,8 @@ type Options = {
allowTypedFunctionExpressions?: boolean;
// if true, functions immediately returning another function expression will not be checked
allowHigherOrderFunctions?: boolean;
// if true, concise arrow functions that start with the void keyword will not be checked
allowConciseArrowFunctionExpressionStartingWithVoid?: boolean;
};

const defaults = {
Expand Down Expand Up @@ -198,6 +200,24 @@ function fn() {
}
```

### `allowConciseArrowFunctionExpressionsStartingWithVoid`

Examples of **incorrect** code for this rule with `{ allowConciseArrowFunctionExpressionsStartingWithVoid: true }`:

```ts
var join = (a: string, b: string) => `${a}${b}`;

const log = (message: string) => {
console.log(message);
};
```

Examples of **correct** code for this rule with `{ allowConciseArrowFunctionExpressionsStartingWithVoid: true }`:

```ts
var log = (message: string) => void console.log(message);
```

## When Not To Use It

If you don't wish to prevent calling code from using function return values in unexpected ways, then
Expand Down
@@ -1,4 +1,7 @@
import { TSESTree } from '@typescript-eslint/experimental-utils';
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';
import {
checkFunctionReturnType,
Expand All @@ -11,6 +14,7 @@ type Options = [
allowTypedFunctionExpressions?: boolean;
allowHigherOrderFunctions?: boolean;
allowDirectConstAssertionInArrowFunctions?: boolean;
allowConciseArrowFunctionExpressionsStartingWithVoid?: boolean;
},
];
type MessageIds = 'missingReturnType';
Expand Down Expand Up @@ -44,6 +48,9 @@ export default util.createRule<Options, MessageIds>({
allowDirectConstAssertionInArrowFunctions: {
type: 'boolean',
},
allowConciseArrowFunctionExpressionsStartingWithVoid: {
type: 'boolean',
},
},
additionalProperties: false,
},
Expand All @@ -55,6 +62,7 @@ export default util.createRule<Options, MessageIds>({
allowTypedFunctionExpressions: true,
allowHigherOrderFunctions: true,
allowDirectConstAssertionInArrowFunctions: true,
allowConciseArrowFunctionExpressionsStartingWithVoid: false,
},
],
create(context, [options]) {
Expand All @@ -64,6 +72,16 @@ export default util.createRule<Options, MessageIds>({
'ArrowFunctionExpression, FunctionExpression'(
node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression,
): void {
if (
options.allowConciseArrowFunctionExpressionsStartingWithVoid &&
node.type === AST_NODE_TYPES.ArrowFunctionExpression &&
node.expression &&
node.body.type === AST_NODE_TYPES.UnaryExpression &&
node.body.operator === 'void'
) {
return;
}

checkFunctionExpressionReturnType(node, options, sourceCode, loc =>
context.report({
node,
Expand Down
Expand Up @@ -366,6 +366,11 @@ new Foo(1, () => {});
},
],
},
{
filename: 'test.ts',
code: 'const log = (message: string) => void console.log(message);',
options: [{ allowConciseArrowFunctionExpressionsStartingWithVoid: true }],
},
],
invalid: [
{
Expand Down Expand Up @@ -1037,5 +1042,39 @@ const func = (value: number) => ({ type: 'X', value } as const);
},
],
},
{
filename: 'test.ts',
code: 'const log = (message: string) => void console.log(message);',
options: [
{ allowConciseArrowFunctionExpressionsStartingWithVoid: false },
],
errors: [
{
messageId: 'missingReturnType',
line: 1,
endLine: 1,
column: 13,
endColumn: 33,
},
],
},
{
filename: 'test.ts',
code: `
const log = (message: string) => {
void console.log(message);
};
`,
options: [{ allowConciseArrowFunctionExpressionsStartingWithVoid: true }],
errors: [
{
messageId: 'missingReturnType',
line: 2,
endLine: 2,
column: 21,
endColumn: 41,
},
],
},
],
});

0 comments on commit 2e9c202

Please sign in to comment.