Skip to content

Commit

Permalink
feat(eslint-plugin): [explicit-function-return-type] add ignoreUnexpo…
Browse files Browse the repository at this point in the history
…rtedFunctions option
  • Loading branch information
GuyPie committed Sep 29, 2019
1 parent 8ce3a81 commit 6a0d3b6
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/eslint-plugin/docs/rules/explicit-function-return-type.md
Expand Up @@ -69,6 +69,8 @@ type Options = {
allowTypedFunctionExpressions?: boolean;
// if true, functions immediately returning another function expression will not be checked
allowHigherOrderFunctions?: boolean;
// if true, functions not directly exported will not be checked
ignoreUnexportedFunctions?: boolean;
};

const defaults = {
Expand Down Expand Up @@ -198,6 +200,28 @@ function fn() {
}
```

### ignoreUnexportedFunctions

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

```ts
export default () => () => {};

export const arrowFn = () => {};

export function fn() {
return function() {};
}
```

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

```ts
function viaDoubleVariableReference() {}
const variableRefOne = viaDoubleVariableReference;
export const variableRefTwo = variableRefOne;
```

## 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
25 changes: 25 additions & 0 deletions packages/eslint-plugin/src/rules/explicit-function-return-type.ts
Expand Up @@ -11,6 +11,7 @@ type Options = [
allowTypedFunctionExpressions?: boolean;
allowHigherOrderFunctions?: boolean;
allowDirectConstAssertionInArrowFunctions?: boolean;
ignoreUnexportedFunctions?: boolean;
},
];
type MessageIds = 'missingReturnType';
Expand Down Expand Up @@ -44,6 +45,9 @@ export default util.createRule<Options, MessageIds>({
allowDirectConstAssertionInArrowFunctions: {
type: 'boolean',
},
ignoreUnexportedFunctions: {
type: 'boolean',
},
},
additionalProperties: false,
},
Expand All @@ -55,6 +59,7 @@ export default util.createRule<Options, MessageIds>({
allowTypedFunctionExpressions: true,
allowHigherOrderFunctions: true,
allowDirectConstAssertionInArrowFunctions: true,
ignoreUnexportedFunctions: false,
},
],
create(context, [options]) {
Expand Down Expand Up @@ -197,6 +202,22 @@ export default util.createRule<Options, MessageIds>({
);
}

function isUnexported(node: TSESTree.Node | undefined): boolean {
while (node) {
if (
node.type === AST_NODE_TYPES.ExportDefaultDeclaration ||
node.type === AST_NODE_TYPES.ExportNamedDeclaration ||
node.type === AST_NODE_TYPES.ExportSpecifier
) {
return false;
}

node = node.parent;
}

return true;
}

/**
* Checks if a function belongs to:
* `() => () => ...`
Expand Down Expand Up @@ -296,6 +317,10 @@ export default util.createRule<Options, MessageIds>({
return;
}

if (options.ignoreUnexportedFunctions && isUnexported(node.parent)) {
return;
}

if (
node.returnType ||
isConstructor(node.parent) ||
Expand Down
Expand Up @@ -322,6 +322,82 @@ const func = (value: number) => x as const;
},
],
},
{
filename: 'test.ts',
code: `
function test(
a: number,
b: number,
) {
return;
}
`,
options: [
{
ignoreUnexportedFunctions: true,
},
],
},
{
filename: 'test.ts',
code: `
export var arrowFn = (): string => 'test';
var fn = function() {
return 1;
};
`,
options: [
{
ignoreUnexportedFunctions: true,
},
],
},
{
filename: 'test.ts',
code: `
class Test {
constructor() {}
get prop() {
return 1;
}
set prop() {}
method() {
return;
}
arrow = () => 'arrow';
private method() {
return;
}
}
`,
options: [
{
ignoreUnexportedFunctions: true,
},
],
},
{
filename: 'test.ts',
code: `
function viaDoubleVariableReference() {}
const variableRefOne = viaDoubleVariableReference;
export const variableRefTwo = variableRefOne;
`,
options: [
{
ignoreUnexportedFunctions: true,
},
],
},
{
filename: 'test.ts',
code: `export default (): void => {}`,
options: [
{
ignoreUnexportedFunctions: true,
},
],
},
],
invalid: [
{
Expand Down Expand Up @@ -968,5 +1044,99 @@ const func = (value: number) => ({ type: "X", value } as const);
},
],
},
{
filename: 'test.ts',
code: `
export function test(
a: number,
b: number,
) {
return;
}
`,
options: [
{
ignoreUnexportedFunctions: true,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 2,
endLine: 5,
column: 8,
endColumn: 2,
},
],
},
{
filename: 'test.ts',
code: `
export var fn = function() {
return 1;
};
`,
options: [
{
ignoreUnexportedFunctions: false,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 2,
endLine: 2,
column: 17,
endColumn: 27,
},
],
},
{
filename: 'test.ts',
code: `
export class Test {
constructor() {}
get prop() {
return 1;
}
set prop() {}
method() {
return;
}
arrow = (): string => 'arrow';
private method() {
return;
}
}
`,
options: [
{
ignoreUnexportedFunctions: false,
},
],
errors: [
{
messageId: 'missingReturnType',
line: 4,
endLine: 4,
column: 3,
endColumn: 13,
},
{
messageId: 'missingReturnType',
line: 8,
endLine: 8,
column: 3,
endColumn: 11,
},
{
messageId: 'missingReturnType',
line: 12,
endLine: 12,
column: 3,
endColumn: 19,
},
],
},
],
});

0 comments on commit 6a0d3b6

Please sign in to comment.