Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0ade469

Browse files
authored
fix(eslint-plugin): [explicit-module-boundary-types] check allowNames on function declarations and property methods (typescript-eslint#3051)
1 parent 792623f commit 0ade469

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,18 @@ export default util.createRule<Options, MessageIds>({
224224
return false;
225225
}
226226

227-
if (node.type === AST_NODE_TYPES.VariableDeclarator) {
227+
if (
228+
node.type === AST_NODE_TYPES.VariableDeclarator ||
229+
node.type === AST_NODE_TYPES.FunctionDeclaration
230+
) {
228231
return (
229-
node.id.type === AST_NODE_TYPES.Identifier &&
232+
node.id?.type === AST_NODE_TYPES.Identifier &&
230233
options.allowedNames.includes(node.id.name)
231234
);
232235
} else if (
233236
node.type === AST_NODE_TYPES.MethodDefinition ||
234-
node.type === AST_NODE_TYPES.TSAbstractMethodDefinition
237+
node.type === AST_NODE_TYPES.TSAbstractMethodDefinition ||
238+
(node.type === AST_NODE_TYPES.Property && node.method)
235239
) {
236240
if (
237241
node.key.type === AST_NODE_TYPES.Literal &&
@@ -481,7 +485,7 @@ export default util.createRule<Options, MessageIds>({
481485
}
482486
checkedFunctions.add(node);
483487

484-
if (isAllowedName(node.parent) || ancestorHasReturnType(node)) {
488+
if (isAllowedName(node) || ancestorHasReturnType(node)) {
485489
return;
486490
}
487491

packages/eslint-plugin/tests/rules/explicit-module-boundary-types.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,23 @@ export const func2 = (value: number) => ({ type: 'X', value });
321321
},
322322
{
323323
code: `
324+
export function func1() {
325+
return 0;
326+
}
327+
export const foo = {
328+
func2() {
329+
return 0;
330+
},
331+
};
332+
`,
333+
options: [
334+
{
335+
allowedNames: ['func1', 'func2'],
336+
},
337+
],
338+
},
339+
{
340+
code: `
324341
export class Test {
325342
get prop() {
326343
return 1;
@@ -1798,5 +1815,38 @@ export function foo(...[a]: any): void {}
17981815
},
17991816
],
18001817
},
1818+
{
1819+
code: `
1820+
export function func1() {
1821+
return 0;
1822+
}
1823+
export const foo = {
1824+
func2() {
1825+
return 0;
1826+
},
1827+
};
1828+
`,
1829+
options: [
1830+
{
1831+
allowedNames: [],
1832+
},
1833+
],
1834+
errors: [
1835+
{
1836+
messageId: 'missingReturnType',
1837+
line: 2,
1838+
endLine: 2,
1839+
column: 8,
1840+
endColumn: 24,
1841+
},
1842+
{
1843+
messageId: 'missingReturnType',
1844+
line: 6,
1845+
endLine: 6,
1846+
column: 3,
1847+
endColumn: 10,
1848+
},
1849+
],
1850+
},
18011851
],
18021852
});

0 commit comments

Comments
 (0)