Skip to content

Commit

Permalink
fix(eslint-plugin): [explicit-module-boundary-types] fix false positives
Browse files Browse the repository at this point in the history
Functions or classes nested, but not returned from inside an exported function should not be required to have an explicit type signature.

Fixes #1486
  • Loading branch information
Mitya Kononchuk committed Jan 21, 2020
1 parent 77a1caa commit cbe08b3
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 cbe08b3

Please sign in to comment.