Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(eslint-plugin): [explicit-function-return-type] add handling for…
… usage as arguments (#680)
  • Loading branch information
bradzacher authored and JamesHenry committed Jul 8, 2019
1 parent 6de19d3 commit e0aeb18
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
Expand Up @@ -138,6 +138,9 @@ let objectPropAs = {
let objectPropCast = <ObjectType>{
foo: () => 1,
};

declare functionWithArg(arg: () => number);
functionWithArg(() => 1);
```

### allowHigherOrderFunctions
Expand Down
Expand Up @@ -187,6 +187,21 @@ export default util.createRule<Options, MessageIds>({
);
}

/**
* Checks if a node belongs to:
* `foo(() => 1)`
*/
function isFunctionArgument(
parent: TSESTree.Node,
child: TSESTree.Node,
): boolean {
return (
parent.type === AST_NODE_TYPES.CallExpression &&
// make sure this isn't an IIFE
parent.callee !== child
);
}

/**
* Checks if a function declaration/expression has a return type.
*/
Expand Down Expand Up @@ -232,7 +247,8 @@ export default util.createRule<Options, MessageIds>({
isTypeCast(node.parent) ||
isVariableDeclaratorWithTypeAnnotation(node.parent) ||
isClassPropertyWithTypeAnnotation(node.parent) ||
isPropertyOfObjectWithType(node.parent)
isPropertyOfObjectWithType(node.parent) ||
isFunctionArgument(node.parent, node)
) {
return;
}
Expand Down
Expand Up @@ -245,6 +245,42 @@ function FunctionDeclaration() {
`,
options: [{ allowHigherOrderFunctions: true }],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/679
{
filename: 'test.ts',
code: `
declare function foo(arg: () => void): void
foo(() => 1)
foo(() => {})
foo(() => null)
foo(() => true)
foo(() => '')
`,
options: [
{
allowTypedFunctionExpressions: true,
},
],
},
{
filename: 'test.ts',
code: `
class Accumulator {
private count: number = 0;
public accumulate(fn: () => number): void {
this.count += fn();
}
}
new Accumulator().accumulate(() => 1);
`,
options: [
{
allowTypedFunctionExpressions: true,
},
],
},
],
invalid: [
{
Expand Down Expand Up @@ -550,5 +586,88 @@ function FunctionDeclaration() {
},
],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/679
{
filename: 'test.ts',
code: `
declare function foo(arg: () => void): void
foo(() => 1)
foo(() => {})
foo(() => null)
foo(() => true)
foo(() => '')
`,
options: [
{
allowTypedFunctionExpressions: false,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 3,
},
{
messageId: 'missingReturnType',
line: 4,
},
{
messageId: 'missingReturnType',
line: 5,
},
{
messageId: 'missingReturnType',
line: 6,
},
{
messageId: 'missingReturnType',
line: 7,
},
],
},
{
filename: 'test.ts',
code: `
class Accumulator {
private count: number = 0;
public accumulate(fn: () => number): void {
this.count += fn();
}
}
new Accumulator().accumulate(() => 1);
`,
options: [
{
allowTypedFunctionExpressions: false,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 10,
column: 30,
},
],
},
{
filename: 'test.ts',
code: `
(() => true)()
`,
options: [
{
allowTypedFunctionExpressions: false,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 2,
column: 2,
},
],
},
],
});

0 comments on commit e0aeb18

Please sign in to comment.