From 0ade469dc1cf17d79c36a9c985630d60491ed847 Mon Sep 17 00:00:00 2001 From: Oleksandr T Date: Sat, 13 Feb 2021 23:08:32 +0200 Subject: [PATCH] fix(eslint-plugin): [explicit-module-boundary-types] check allowNames on function declarations and property methods (#3051) --- .../rules/explicit-module-boundary-types.ts | 12 +++-- .../explicit-module-boundary-types.test.ts | 50 +++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts index 2214f8fa343..bdcd9750c88 100644 --- a/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts +++ b/packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts @@ -224,14 +224,18 @@ export default util.createRule({ return false; } - if (node.type === AST_NODE_TYPES.VariableDeclarator) { + if ( + node.type === AST_NODE_TYPES.VariableDeclarator || + node.type === AST_NODE_TYPES.FunctionDeclaration + ) { return ( - node.id.type === AST_NODE_TYPES.Identifier && + node.id?.type === AST_NODE_TYPES.Identifier && options.allowedNames.includes(node.id.name) ); } else if ( node.type === AST_NODE_TYPES.MethodDefinition || - node.type === AST_NODE_TYPES.TSAbstractMethodDefinition + node.type === AST_NODE_TYPES.TSAbstractMethodDefinition || + (node.type === AST_NODE_TYPES.Property && node.method) ) { if ( node.key.type === AST_NODE_TYPES.Literal && @@ -481,7 +485,7 @@ export default util.createRule({ } checkedFunctions.add(node); - if (isAllowedName(node.parent) || ancestorHasReturnType(node)) { + if (isAllowedName(node) || ancestorHasReturnType(node)) { return; } diff --git a/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts b/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts index b2906d17a98..3652ebc876a 100644 --- a/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts +++ b/packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts @@ -321,6 +321,23 @@ export const func2 = (value: number) => ({ type: 'X', value }); }, { code: ` +export function func1() { + return 0; +} +export const foo = { + func2() { + return 0; + }, +}; + `, + options: [ + { + allowedNames: ['func1', 'func2'], + }, + ], + }, + { + code: ` export class Test { get prop() { return 1; @@ -1798,5 +1815,38 @@ export function foo(...[a]: any): void {} }, ], }, + { + code: ` +export function func1() { + return 0; +} +export const foo = { + func2() { + return 0; + }, +}; + `, + options: [ + { + allowedNames: [], + }, + ], + errors: [ + { + messageId: 'missingReturnType', + line: 2, + endLine: 2, + column: 8, + endColumn: 24, + }, + { + messageId: 'missingReturnType', + line: 6, + endLine: 6, + column: 3, + endColumn: 10, + }, + ], + }, ], });