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 allowedNames #4440

Merged
merged 15 commits into from Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -75,6 +75,10 @@ type Options = {
allowDirectConstAssertionInArrowFunctions?: boolean;
// if true, concise arrow functions that start with the void keyword will not be checked
allowConciseArrowFunctionExpressionsStartingWithVoid?: boolean;
/**
* An array of function/method names that will not have their arguments or their return values checked.
*/
allowedNames?: string[];
};

const defaults = {
Expand All @@ -83,6 +87,7 @@ const defaults = {
allowHigherOrderFunctions: true,
allowDirectConstAssertionInArrowFunctions: true,
allowConciseArrowFunctionExpressionsStartingWithVoid: false,
allowedNames: [],
};
```

Expand Down Expand Up @@ -260,6 +265,21 @@ const log = (message: string) => {
var log = (message: string) => void console.log(message);
```

### `allowedNames`

You may pass function/method names you would like this rule to ignore, like so:

```json
{
"@typescript-eslint/explicit-function-return-type": [
"error",
{
"allowedNames": ["ignoredFunctionName", "ignoredMethodName"]
}
]
}
```

## 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
Expand Up @@ -12,6 +12,7 @@ type Options = [
allowHigherOrderFunctions?: boolean;
allowDirectConstAssertionInArrowFunctions?: boolean;
allowConciseArrowFunctionExpressionsStartingWithVoid?: boolean;
allowedNames?: string[];
},
];
type MessageIds = 'missingReturnType';
Expand Down Expand Up @@ -47,6 +48,12 @@ export default util.createRule<Options, MessageIds>({
allowConciseArrowFunctionExpressionsStartingWithVoid: {
type: 'boolean',
},
allowedNames: {
type: 'array',
items: {
type: 'string',
},
},
},
additionalProperties: false,
},
Expand All @@ -59,11 +66,36 @@ export default util.createRule<Options, MessageIds>({
allowHigherOrderFunctions: true,
allowDirectConstAssertionInArrowFunctions: true,
allowConciseArrowFunctionExpressionsStartingWithVoid: false,
allowedNames: [],
},
],
create(context, [options]) {
const sourceCode = context.getSourceCode();

function isAllowedName(
node:
| TSESTree.ArrowFunctionExpression
| TSESTree.FunctionExpression
| TSESTree.FunctionDeclaration,
): boolean {
if (
node.type === AST_NODE_TYPES.ArrowFunctionExpression ||
node.type === AST_NODE_TYPES.FunctionExpression
) {
const parent = node.parent;
return (
parent?.type === AST_NODE_TYPES.VariableDeclarator &&
parent?.id.type === AST_NODE_TYPES.Identifier &&
islandryu marked this conversation as resolved.
Show resolved Hide resolved
!!options.allowedNames?.includes(parent.id.name)
);
}
if (node.type === AST_NODE_TYPES.FunctionDeclaration) {
return (
node.id?.type === AST_NODE_TYPES.Identifier &&
!!options.allowedNames?.includes(node.id.name)
);
}
return false;
}
return {
'ArrowFunctionExpression, FunctionExpression'(
node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression,
Expand All @@ -78,6 +110,10 @@ export default util.createRule<Options, MessageIds>({
return;
}

if (isAllowedName(node)) {
return;
}

checkFunctionExpressionReturnType(node, options, sourceCode, loc =>
context.report({
node,
Expand All @@ -87,6 +123,9 @@ export default util.createRule<Options, MessageIds>({
);
},
FunctionDeclaration(node): void {
if (isAllowedName(node)) {
return;
}
checkFunctionReturnType(node, options, sourceCode, loc =>
context.report({
node,
Expand Down
Expand Up @@ -400,6 +400,45 @@ new Foo(1, () => {});
code: 'const log = (message: string) => void console.log(message);',
options: [{ allowConciseArrowFunctionExpressionsStartingWithVoid: true }],
},
{
filename: 'test.ts',
options: [
{
allowedNames: ['test'],
},
],
code: `
function test() {
return;
}
`,
},
{
filename: 'test.ts',
options: [
{
allowedNames: ['test'],
},
],
code: `
const test = function () {
return;
};
`,
},
{
filename: 'test.ts',
options: [
{
allowedNames: ['test'],
},
],
code: `
const test = () => {
return;
};
`,
},
],
invalid: [
{
Expand Down Expand Up @@ -1106,5 +1145,27 @@ const func = (value: number) => ({ type: 'X', value } as const);
},
],
},
{
filename: 'test.ts',
options: [
{
allowedNames: ['test'],
},
],
code: `
function hoge() {
return;
}
`,
errors: [
{
messageId: 'missingReturnType',
line: 2,
endLine: 2,
column: 1,
endColumn: 16,
},
],
},
],
});