Skip to content

Commit

Permalink
Add implicit return (#57)
Browse files Browse the repository at this point in the history
* refactor function args functions

* add implicit return lint
  • Loading branch information
pipopotamasu authored Mar 2, 2024
1 parent 99c7f81 commit 6a34cfd
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 12 deletions.
8 changes: 4 additions & 4 deletions rules/no-implicit-any/function-args/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function lintArg(
}
}

export const lintFunctionDeclaration = (
export const lintArgsOfFunctionDeclaration = (
context: Readonly<TSESLint.RuleContext<'missingAnyType', any[]>>,
node: TSESTree.FunctionDeclaration
) => {
Expand All @@ -93,7 +93,7 @@ export const lintFunctionDeclaration = (
});
};

export const lintTSFunctionType = (
export const lintArgsOfTSFunctionType = (
context: Readonly<TSESLint.RuleContext<'missingAnyType', any[]>>,
node: TSESTree.TSFunctionType
) => {
Expand All @@ -102,7 +102,7 @@ export const lintTSFunctionType = (
});
};

export const lintFunctionExpression = (
export const lintArgsOfFunctionExpression = (
context: Readonly<TSESLint.RuleContext<'missingAnyType', any[]>>,
node: TSESTree.FunctionExpression
) => {
Expand Down Expand Up @@ -140,7 +140,7 @@ export const lintFunctionExpression = (
});
};

export const lintArrowFunctionExpression = (
export const lintArgsOfArrowFunctionExpression = (
context: Readonly<TSESLint.RuleContext<'missingAnyType', any[]>>,
node: TSESTree.ArrowFunctionExpression
) => {
Expand Down
45 changes: 45 additions & 0 deletions rules/no-implicit-any/implicit-return/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ruleTester } from '../../testUtil';
import { rule } from '../../no-implicit-any';

ruleTester.run('implicit-return', rule, {
valid: [
{ code: 'const foo = () => { return 1 }' },
{ code: 'const foo = () => 1' },
{ code: 'const foo = () => null as null' },
{ code: 'const foo = () => (null as null)' },
{ code: 'const foo = () => (null) as null' },
{ code: 'const foo = (): null => null' },
],
invalid: [
{
code: 'const foo = () => null',
output: 'const foo = () => null as null',
errors: [{ messageId: 'missingAnyType' }],
},
{
code: 'const foo = () => (null)',
output: 'const foo = () => (null as null)',
errors: [{ messageId: 'missingAnyType' }],
},
{
code: 'const foo = () => undefined',
output: 'const foo = () => undefined as undefined',
errors: [{ messageId: 'missingAnyType' }],
},
{
code: 'const foo = () => (undefined)',
output: 'const foo = () => (undefined as undefined)',
errors: [{ messageId: 'missingAnyType' }],
},
{
code: 'const foo = () => []',
output: 'const foo = () => [] as any[]',
errors: [{ messageId: 'missingAnyType' }],
},
{
code: 'const foo = () => ([])',
output: 'const foo = () => ([] as any[])',
errors: [{ messageId: 'missingAnyType' }],
},
],
});
45 changes: 45 additions & 0 deletions rules/no-implicit-any/implicit-return/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { ESLintUtils, type TSESLint } from '@typescript-eslint/utils';
import { type TSESTree, AST_NODE_TYPES } from '@typescript-eslint/types';
import { isNull, isUndefined, enabledStrictNullChecks } from '../../helper';

export const lintImplicitReturn = (
context: Readonly<TSESLint.RuleContext<'missingAnyType', any[]>>,
node: TSESTree.ArrowFunctionExpression
) => {
const bodyNode = node.body;
const bodyType = bodyNode.type;
const parserServices = ESLintUtils.getParserServices(context);
if (
bodyType === AST_NODE_TYPES.BlockStatement ||
bodyType === AST_NODE_TYPES.TSAsExpression ||
node.returnType ||
enabledStrictNullChecks(parserServices.program.getCompilerOptions())
)
return;

if (isNull(bodyNode)) {
context.report({
node: bodyNode,
messageId: 'missingAnyType',
fix(fixer) {
return fixer.insertTextAfter(bodyNode, ' as null');
},
});
} else if (isUndefined(bodyNode)) {
context.report({
node: bodyNode,
messageId: 'missingAnyType',
fix(fixer) {
return fixer.insertTextAfter(bodyNode, ' as undefined');
},
});
} else if (bodyNode.type === AST_NODE_TYPES.ArrayExpression && bodyNode.elements.length === 0) {
context.report({
node: bodyNode,
messageId: 'missingAnyType',
fix(fixer) {
return fixer.insertTextAfter(bodyNode, ' as any[]');
},
});
}
};
18 changes: 10 additions & 8 deletions rules/no-implicit-any/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { ESLintUtils } from '@typescript-eslint/utils';

import {
lintFunctionDeclaration,
lintFunctionExpression,
lintArrowFunctionExpression,
lintTSFunctionType,
lintArgsOfFunctionDeclaration,
lintArgsOfFunctionExpression,
lintArgsOfArrowFunctionExpression,
lintArgsOfTSFunctionType,
} from './function-args';
import { lintMemberExpression } from './member-expression';
import { lintVariableDeclarator } from './variable-declarator';
import { lintReturnStatement } from './return-statement';
import { lintImplicitReturn } from './implicit-return';
import { lintObjectExpression } from './object-expression';

function hasJSExtension(filePath: string) {
Expand Down Expand Up @@ -36,16 +37,17 @@ export const rule = createRule({

return {
FunctionDeclaration(node) {
lintFunctionDeclaration(context, node);
lintArgsOfFunctionDeclaration(context, node);
},
FunctionExpression(node) {
lintFunctionExpression(context, node);
lintArgsOfFunctionExpression(context, node);
},
ArrowFunctionExpression(node) {
lintArrowFunctionExpression(context, node);
lintArgsOfArrowFunctionExpression(context, node);
lintImplicitReturn(context, node);
},
TSFunctionType(node) {
lintTSFunctionType(context, node);
lintArgsOfTSFunctionType(context, node);
},
VariableDeclarator(node) {
lintVariableDeclarator(context, node);
Expand Down

0 comments on commit 6a34cfd

Please sign in to comment.