Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [explicit-function-return-type] allowHigherOrderFunctions (#193) #538

Merged
merged 17 commits into from Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/eslint-plugin/docs/rules/explicit-function-return-type.md
Expand Up @@ -67,11 +67,14 @@ type Options = {
allowExpressions?: boolean;
// if true, type annotations are also allowed on the variable of a function expression rather than on the function directly.
allowTypedFunctionExpressions?: boolean;
// if true, only arrow functions which does not return an arrow function will be checked
allowCurrying?: boolean;
};

const defaults = {
allowExpressions: false,
allowTypedFunctionExpressions: false,
allowCurrying: false,
};
```

Expand Down Expand Up @@ -137,6 +140,20 @@ let objectPropCast = <ObjectType>{
};
```

### allowCurrying

Examples of **incorrect** code for this rule with `{ allowCurrying: true }`:

```ts
var curriedAddFn = (x: number) => (y: number) => x + y;
```

Examples of **correct** code for this rule with `{ allowCurrying: true }`:

```ts
var curriedAddFn = (x: number) => (y: number): number => x + y;
```

## When Not To Use It

If you don't wish to prevent calling code from using function return values in unexpected ways, then
Expand Down
13 changes: 13 additions & 0 deletions packages/eslint-plugin/src/rules/explicit-function-return-type.ts
Expand Up @@ -8,6 +8,7 @@ type Options = [
{
allowExpressions?: boolean;
allowTypedFunctionExpressions?: boolean;
allowCurrying?: boolean;
}
];
type MessageIds = 'missingReturnType';
Expand Down Expand Up @@ -35,6 +36,9 @@ export default util.createRule<Options, MessageIds>({
allowTypedFunctionExpressions: {
type: 'boolean',
},
allowCurrying: {
type: 'boolean',
},
},
additionalProperties: false,
},
Expand All @@ -44,6 +48,7 @@ export default util.createRule<Options, MessageIds>({
{
allowExpressions: false,
allowTypedFunctionExpressions: false,
allowCurrying: false,
},
],
create(context, [options]) {
Expand Down Expand Up @@ -188,6 +193,14 @@ export default util.createRule<Options, MessageIds>({
) {
return;
}

if (
options.allowCurrying &&
node.type === AST_NODE_TYPES.ArrowFunctionExpression &&
node.body.type === AST_NODE_TYPES.ArrowFunctionExpression
) {
return;
}
}

checkFunctionReturnType(node);
Expand Down
Expand Up @@ -180,6 +180,13 @@ const myObj = {
};
`,
},
{
filename: 'test.ts',
code: `
var curriedAddFn = (x: int) => (y: int): int => x + y;
`,
options: [{ allowCurrying: true }],
},
],
invalid: [
{
Expand Down Expand Up @@ -364,5 +371,38 @@ const x: Foo = {
},
],
},
{
filename: 'test.ts',
code: `
var curriedAddFn = (x: number) => (y: number) => x + y;
`,
options: [{ allowCurrying: false }],
errors: [
{
messageId: 'missingReturnType',
line: 2,
column: 20,
},
{
messageId: 'missingReturnType',
line: 2,
column: 35,
},
],
},
{
filename: 'test.ts',
code: `
var curriedAddFn = (x: number) => (y: number) => x + y;
`,
options: [{ allowCurrying: true }],
errors: [
{
messageId: 'missingReturnType',
line: 2,
column: 35,
},
],
},
],
});