diff --git a/packages/eslint-plugin/docs/rules/explicit-function-return-type.md b/packages/eslint-plugin/docs/rules/explicit-function-return-type.md index 4c199878bb1f..4c68bfb0591a 100644 --- a/packages/eslint-plugin/docs/rules/explicit-function-return-type.md +++ b/packages/eslint-plugin/docs/rules/explicit-function-return-type.md @@ -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 = { @@ -83,6 +87,7 @@ const defaults = { allowHigherOrderFunctions: true, allowDirectConstAssertionInArrowFunctions: true, allowConciseArrowFunctionExpressionsStartingWithVoid: false, + allowedNames: [], }; ``` @@ -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 diff --git a/packages/eslint-plugin/src/rules/explicit-function-return-type.ts b/packages/eslint-plugin/src/rules/explicit-function-return-type.ts index 233b07bbc6a5..2e3fa120fca7 100644 --- a/packages/eslint-plugin/src/rules/explicit-function-return-type.ts +++ b/packages/eslint-plugin/src/rules/explicit-function-return-type.ts @@ -12,6 +12,7 @@ type Options = [ allowHigherOrderFunctions?: boolean; allowDirectConstAssertionInArrowFunctions?: boolean; allowConciseArrowFunctionExpressionsStartingWithVoid?: boolean; + allowedNames?: string[]; }, ]; type MessageIds = 'missingReturnType'; @@ -47,6 +48,12 @@ export default util.createRule({ allowConciseArrowFunctionExpressionsStartingWithVoid: { type: 'boolean', }, + allowedNames: { + type: 'array', + items: { + type: 'string', + }, + }, }, additionalProperties: false, }, @@ -59,11 +66,36 @@ export default util.createRule({ 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, @@ -78,6 +110,10 @@ export default util.createRule({ return; } + if (isAllowedName(node)) { + return; + } + checkFunctionExpressionReturnType(node, options, sourceCode, loc => context.report({ node, @@ -87,6 +123,9 @@ export default util.createRule({ ); }, FunctionDeclaration(node): void { + if (isAllowedName(node)) { + return; + } checkFunctionReturnType(node, options, sourceCode, loc => context.report({ node, diff --git a/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts b/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts index ab98b4be27c5..8e22f5f55a31 100644 --- a/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-function-return-type.test.ts @@ -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: [ { @@ -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, + }, + ], + }, ], });