Skip to content

Commit

Permalink
Renamed option
Browse files Browse the repository at this point in the history
  • Loading branch information
Svish committed May 23, 2019
1 parent 1651e47 commit 3182953
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
20 changes: 10 additions & 10 deletions packages/eslint-plugin/docs/rules/explicit-function-return-type.md
Expand Up @@ -67,14 +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, currying functions (those immediately returning another function expression) will not be checked
allowCurrying?: boolean;
// if true, functions immediately returning another function expression will not be checked
allowHigherOrderFunctions?: boolean;
};

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

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

### allowCurrying
### allowHigherOrderFunctions

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

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

function curriedFunction(x: number) {
function fn(x: number) {
return function(y: number) {
return x + y;
};
}
```

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

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

function curriedFunction(x: number) {
function fn(x: number) {
return function(y: number): number {
return x + y;
};
Expand Down
21 changes: 12 additions & 9 deletions packages/eslint-plugin/src/rules/explicit-function-return-type.ts
Expand Up @@ -8,7 +8,7 @@ type Options = [
{
allowExpressions?: boolean;
allowTypedFunctionExpressions?: boolean;
allowCurrying?: boolean;
allowHigherOrderFunctions?: boolean;
}
];
type MessageIds = 'missingReturnType';
Expand Down Expand Up @@ -36,7 +36,7 @@ export default util.createRule<Options, MessageIds>({
allowTypedFunctionExpressions: {
type: 'boolean',
},
allowCurrying: {
allowHigherOrderFunctions: {
type: 'boolean',
},
},
Expand All @@ -48,7 +48,7 @@ export default util.createRule<Options, MessageIds>({
{
allowExpressions: false,
allowTypedFunctionExpressions: false,
allowCurrying: false,
allowHigherOrderFunctions: false,
},
],
create(context, [options]) {
Expand Down Expand Up @@ -152,7 +152,7 @@ export default util.createRule<Options, MessageIds>({
* `function fn() { return () => ... }`
* `function fn() { return function() { ... } }`
*/
function isCurrying({
function doesImmediatelyReturnFunctionExpression({
body,
}:
| TSESTree.ArrowFunctionExpression
Expand All @@ -163,24 +163,24 @@ export default util.createRule<Options, MessageIds>({
return false;
}

// Body can be a single statement block
// Check if body is a block with a single statement
if (
body.type === AST_NODE_TYPES.BlockStatement &&
body.body.length === 1
) {
const [statement] = body.body;

// Which is a return statement with an argument
// Check if that statement is a return statement with an argument
if (
statement.type === AST_NODE_TYPES.ReturnStatement &&
!!statement.argument
) {
// In which case we check that instead of original body
// If so, check that returned argument as body
body = statement.argument;
}
}

// Then, check if body is a function expression
// Check if the body being returned is a function expression
return (
body.type === AST_NODE_TYPES.ArrowFunctionExpression ||
body.type === AST_NODE_TYPES.FunctionExpression
Expand All @@ -196,7 +196,10 @@ export default util.createRule<Options, MessageIds>({
| TSESTree.FunctionDeclaration
| TSESTree.FunctionExpression,
): void {
if (options.allowCurrying && isCurrying(node)) {
if (
options.allowHigherOrderFunctions &&
doesImmediatelyReturnFunctionExpression(node)
) {
return;
}

Expand Down
Expand Up @@ -185,42 +185,42 @@ const myObj = {
code: `
() => (): void => {};
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
},
{
filename: 'test.ts',
code: `
() => function (): void {};
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
},
{
filename: 'test.ts',
code: `
() => { return (): void => {} };
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
},
{
filename: 'test.ts',
code: `
() => { return function (): void {} };
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
},
{
filename: 'test.ts',
code: `
function fn() { return (): void => {} };
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
},
{
filename: 'test.ts',
code: `
function fn() { return function (): void {} };
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
},
{
filename: 'test.ts',
Expand All @@ -236,14 +236,14 @@ function FunctionDeclaration() {
}
}
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
},
{
filename: 'test.ts',
code: `
() => () => { return (): void => { return; } };
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
},
],
invalid: [
Expand Down Expand Up @@ -434,7 +434,7 @@ const x: Foo = {
code: `
() => () => {};
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -448,7 +448,7 @@ const x: Foo = {
code: `
() => function () {};
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -462,7 +462,7 @@ const x: Foo = {
code: `
() => { return () => {} };
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -476,7 +476,7 @@ const x: Foo = {
code: `
() => { return function () {} };
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -490,7 +490,7 @@ const x: Foo = {
code: `
function fn() { return () => {} };
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -504,7 +504,7 @@ function fn() { return () => {} };
code: `
function fn() { return function () {} };
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -527,7 +527,7 @@ function FunctionDeclaration() {
}
}
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
Expand All @@ -541,7 +541,7 @@ function FunctionDeclaration() {
code: `
() => () => { return () => { return; } };
`,
options: [{ allowCurrying: true }],
options: [{ allowHigherOrderFunctions: true }],
errors: [
{
messageId: 'missingReturnType',
Expand Down

0 comments on commit 3182953

Please sign in to comment.