Skip to content

Commit 2e9c202

Browse files
authored
feat(eslint-plugin): [explicit-function-return-type] add option to allow concise arrows that start with void (typescript-eslint#1732)
1 parent 3eee804 commit 2e9c202

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

packages/eslint-plugin/docs/rules/explicit-function-return-type.md

+20
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ type Options = {
6969
allowTypedFunctionExpressions?: boolean;
7070
// if true, functions immediately returning another function expression will not be checked
7171
allowHigherOrderFunctions?: boolean;
72+
// if true, concise arrow functions that start with the void keyword will not be checked
73+
allowConciseArrowFunctionExpressionStartingWithVoid?: boolean;
7274
};
7375

7476
const defaults = {
@@ -198,6 +200,24 @@ function fn() {
198200
}
199201
```
200202

203+
### `allowConciseArrowFunctionExpressionsStartingWithVoid`
204+
205+
Examples of **incorrect** code for this rule with `{ allowConciseArrowFunctionExpressionsStartingWithVoid: true }`:
206+
207+
```ts
208+
var join = (a: string, b: string) => `${a}${b}`;
209+
210+
const log = (message: string) => {
211+
console.log(message);
212+
};
213+
```
214+
215+
Examples of **correct** code for this rule with `{ allowConciseArrowFunctionExpressionsStartingWithVoid: true }`:
216+
217+
```ts
218+
var log = (message: string) => void console.log(message);
219+
```
220+
201221
## When Not To Use It
202222

203223
If you don't wish to prevent calling code from using function return values in unexpected ways, then

packages/eslint-plugin/src/rules/explicit-function-return-type.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { TSESTree } from '@typescript-eslint/experimental-utils';
1+
import {
2+
AST_NODE_TYPES,
3+
TSESTree,
4+
} from '@typescript-eslint/experimental-utils';
25
import * as util from '../util';
36
import {
47
checkFunctionReturnType,
@@ -11,6 +14,7 @@ type Options = [
1114
allowTypedFunctionExpressions?: boolean;
1215
allowHigherOrderFunctions?: boolean;
1316
allowDirectConstAssertionInArrowFunctions?: boolean;
17+
allowConciseArrowFunctionExpressionsStartingWithVoid?: boolean;
1418
},
1519
];
1620
type MessageIds = 'missingReturnType';
@@ -44,6 +48,9 @@ export default util.createRule<Options, MessageIds>({
4448
allowDirectConstAssertionInArrowFunctions: {
4549
type: 'boolean',
4650
},
51+
allowConciseArrowFunctionExpressionsStartingWithVoid: {
52+
type: 'boolean',
53+
},
4754
},
4855
additionalProperties: false,
4956
},
@@ -55,6 +62,7 @@ export default util.createRule<Options, MessageIds>({
5562
allowTypedFunctionExpressions: true,
5663
allowHigherOrderFunctions: true,
5764
allowDirectConstAssertionInArrowFunctions: true,
65+
allowConciseArrowFunctionExpressionsStartingWithVoid: false,
5866
},
5967
],
6068
create(context, [options]) {
@@ -64,6 +72,16 @@ export default util.createRule<Options, MessageIds>({
6472
'ArrowFunctionExpression, FunctionExpression'(
6573
node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression,
6674
): void {
75+
if (
76+
options.allowConciseArrowFunctionExpressionsStartingWithVoid &&
77+
node.type === AST_NODE_TYPES.ArrowFunctionExpression &&
78+
node.expression &&
79+
node.body.type === AST_NODE_TYPES.UnaryExpression &&
80+
node.body.operator === 'void'
81+
) {
82+
return;
83+
}
84+
6785
checkFunctionExpressionReturnType(node, options, sourceCode, loc =>
6886
context.report({
6987
node,

packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts

+39
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ new Foo(1, () => {});
366366
},
367367
],
368368
},
369+
{
370+
filename: 'test.ts',
371+
code: 'const log = (message: string) => void console.log(message);',
372+
options: [{ allowConciseArrowFunctionExpressionsStartingWithVoid: true }],
373+
},
369374
],
370375
invalid: [
371376
{
@@ -1037,5 +1042,39 @@ const func = (value: number) => ({ type: 'X', value } as const);
10371042
},
10381043
],
10391044
},
1045+
{
1046+
filename: 'test.ts',
1047+
code: 'const log = (message: string) => void console.log(message);',
1048+
options: [
1049+
{ allowConciseArrowFunctionExpressionsStartingWithVoid: false },
1050+
],
1051+
errors: [
1052+
{
1053+
messageId: 'missingReturnType',
1054+
line: 1,
1055+
endLine: 1,
1056+
column: 13,
1057+
endColumn: 33,
1058+
},
1059+
],
1060+
},
1061+
{
1062+
filename: 'test.ts',
1063+
code: `
1064+
const log = (message: string) => {
1065+
void console.log(message);
1066+
};
1067+
`,
1068+
options: [{ allowConciseArrowFunctionExpressionsStartingWithVoid: true }],
1069+
errors: [
1070+
{
1071+
messageId: 'missingReturnType',
1072+
line: 2,
1073+
endLine: 2,
1074+
column: 21,
1075+
endColumn: 41,
1076+
},
1077+
],
1078+
},
10401079
],
10411080
});

0 commit comments

Comments
 (0)