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

feat(eslint-plugin): [explicit-function-return-type] add option to allow concise arrows that start with void #1732

Merged
merged 2 commits into from Apr 13, 2020
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
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,
},
],
},
],
});