Skip to content

Commit

Permalink
fix(eslint-plugin): [explicit-module-boundary-types] false positive f…
Browse files Browse the repository at this point in the history
…or returned fns (#1490)
  • Loading branch information
mijay authored and bradzacher committed Jan 22, 2020
1 parent 77a1caa commit 5562ad5
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/eslint-plugin/src/rules/explicit-module-boundary-types.ts
Expand Up @@ -211,6 +211,7 @@ export default util.createRule<Options, MessageIds>({
}

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

if (node.type === AST_NODE_TYPES.ReturnStatement) {
isReturnedValue = true;
}

if (
node.type === AST_NODE_TYPES.ArrowFunctionExpression ||
node.type === AST_NODE_TYPES.FunctionDeclaration ||
node.type === AST_NODE_TYPES.FunctionExpression
) {
isReturnedValue = false;
}

if (node.type === AST_NODE_TYPES.BlockStatement && !isReturnedValue) {
return true;
}

node = node.parent;
}

Expand Down
Expand Up @@ -72,6 +72,39 @@ export class Test {
{
filename: 'test.ts',
code: `
export function test(): void {
nested();
return;
function nested() {}
}
`,
},
{
filename: 'test.ts',
code: `
export function test(): string {
const nested = () => 'value';
return nested();
}
`,
},
{
filename: 'test.ts',
code: `
export function test(): string {
class Nested {
public method() {
return 'value';
}
}
return new Nested().method();
}
`,
},
{
filename: 'test.ts',
code: `
export var arrowFn: Foo = () => 'test';
`,
options: [
Expand Down Expand Up @@ -452,6 +485,26 @@ export class Foo {
},
],
},
{
filename: 'test.ts',
code: 'export default () => true ? (() => {}) : ((): void => {});',
errors: [
{
messageId: 'missingReturnType',
line: 1,
endLine: 1,
column: 16,
endColumn: 21,
},
{
messageId: 'missingReturnType',
line: 1,
endLine: 1,
column: 30,
endColumn: 35,
},
],
},
{
filename: 'test.ts',
code: "export var arrowFn = () => 'test';",
Expand Down

0 comments on commit 5562ad5

Please sign in to comment.