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

Skip to content

Commit 5562ad5

Browse files
mijaybradzacher
authored andcommitted
fix(eslint-plugin): [explicit-module-boundary-types] false positive for returned fns (typescript-eslint#1490)
1 parent 77a1caa commit 5562ad5

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ export default util.createRule<Options, MessageIds>({
211211
}
212212

213213
function isUnexported(node: TSESTree.Node | undefined): boolean {
214+
let isReturnedValue = false;
214215
while (node) {
215216
if (
216217
node.type === AST_NODE_TYPES.ExportDefaultDeclaration ||
@@ -220,6 +221,22 @@ export default util.createRule<Options, MessageIds>({
220221
return false;
221222
}
222223

224+
if (node.type === AST_NODE_TYPES.ReturnStatement) {
225+
isReturnedValue = true;
226+
}
227+
228+
if (
229+
node.type === AST_NODE_TYPES.ArrowFunctionExpression ||
230+
node.type === AST_NODE_TYPES.FunctionDeclaration ||
231+
node.type === AST_NODE_TYPES.FunctionExpression
232+
) {
233+
isReturnedValue = false;
234+
}
235+
236+
if (node.type === AST_NODE_TYPES.BlockStatement && !isReturnedValue) {
237+
return true;
238+
}
239+
223240
node = node.parent;
224241
}
225242

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,39 @@ export class Test {
7272
{
7373
filename: 'test.ts',
7474
code: `
75+
export function test(): void {
76+
nested();
77+
return;
78+
79+
function nested() {}
80+
}
81+
`,
82+
},
83+
{
84+
filename: 'test.ts',
85+
code: `
86+
export function test(): string {
87+
const nested = () => 'value';
88+
return nested();
89+
}
90+
`,
91+
},
92+
{
93+
filename: 'test.ts',
94+
code: `
95+
export function test(): string {
96+
class Nested {
97+
public method() {
98+
return 'value';
99+
}
100+
}
101+
return new Nested().method();
102+
}
103+
`,
104+
},
105+
{
106+
filename: 'test.ts',
107+
code: `
75108
export var arrowFn: Foo = () => 'test';
76109
`,
77110
options: [
@@ -452,6 +485,26 @@ export class Foo {
452485
},
453486
],
454487
},
488+
{
489+
filename: 'test.ts',
490+
code: 'export default () => true ? (() => {}) : ((): void => {});',
491+
errors: [
492+
{
493+
messageId: 'missingReturnType',
494+
line: 1,
495+
endLine: 1,
496+
column: 16,
497+
endColumn: 21,
498+
},
499+
{
500+
messageId: 'missingReturnType',
501+
line: 1,
502+
endLine: 1,
503+
column: 30,
504+
endColumn: 35,
505+
},
506+
],
507+
},
455508
{
456509
filename: 'test.ts',
457510
code: "export var arrowFn = () => 'test';",

0 commit comments

Comments
 (0)