Skip to content

Commit

Permalink
feat(eslint-plugin): [explicit-function-return-type] add allowedNames…
Browse files Browse the repository at this point in the history
… option
  • Loading branch information
islandryu committed Jan 14, 2022
1 parent 9eb0a5b commit def7c60
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
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-module-boundary-types": [
"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 &&
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,
},
],
},
],
});

0 comments on commit def7c60

Please sign in to comment.