Skip to content

Commit

Permalink
improve return statement type
Browse files Browse the repository at this point in the history
  • Loading branch information
pipopotamasu committed Feb 28, 2024
1 parent 4942a21 commit 547b4be
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
6 changes: 3 additions & 3 deletions rules/helper.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { type TSESTree, AST_NODE_TYPES } from '@typescript-eslint/types';
import type { CompilerOptions } from 'typescript';

function isNull(node: TSESTree.Expression) {
export function isNull(node: TSESTree.Expression) {
return node.type === AST_NODE_TYPES.Literal && node.value === null;
}

function isUndefined(node: TSESTree.Expression) {
export function isUndefined(node: TSESTree.Expression) {
return node.type === AST_NODE_TYPES.Identifier && node.name === 'undefined';
}

function isVoid(node: TSESTree.Expression) {
export function isVoid(node: TSESTree.Expression) {
return node.type === AST_NODE_TYPES.UnaryExpression && node.operator === 'void';
}

Expand Down
9 changes: 2 additions & 7 deletions rules/no-implicit-any/return-statement/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,12 @@ ruleTester.run('return-statement', rule, {
invalid: [
{
code: 'const foo = () => { return null }',
output: 'const foo = () => { return null as any }',
errors: [{ messageId: 'missingAnyType' }],
},
{
code: 'const foo = () => { return null; }',
output: 'const foo = () => { return null as any; }',
output: 'const foo = () => { return null as null }',
errors: [{ messageId: 'missingAnyType' }],
},
{
code: 'const foo = () => { return undefined }',
output: 'const foo = () => { return undefined as any }',
output: 'const foo = () => { return undefined as undefined }',
errors: [{ messageId: 'missingAnyType' }],
},
{
Expand Down
15 changes: 13 additions & 2 deletions rules/no-implicit-any/return-statement/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { ESLintUtils, type TSESLint } from '@typescript-eslint/utils';
import { type TSESTree, AST_NODE_TYPES } from '@typescript-eslint/types';
import { isNullOrUndefinedOrVoid, enabledStrictNullChecks } from '../../helper';
import {
isNullOrUndefinedOrVoid,
isNull,
isUndefined,
enabledStrictNullChecks,
} from '../../helper';

function hasTypeAnnotationInAncestors(node: TSESTree.Node) {
if (node === null) {
Expand Down Expand Up @@ -74,6 +79,12 @@ function getReturnStatementNodes(nodes: TSESTree.Statement[]) {
return returnStatementNodes.flat(Infinity).filter(Boolean);
}

function getCastType(node: TSESTree.Expression) {
if (isNull(node)) return 'null';
if (isUndefined(node)) return 'undefined';
return 'any';
}

function isAllNullOrUndefinedOrVoid(node: TSESTree.LogicalExpression) {
const { left, right } = node;
if (left.type === AST_NODE_TYPES.LogicalExpression) {
Expand Down Expand Up @@ -120,7 +131,7 @@ export const lintReturnStatement = (
node: node,
messageId: 'missingAnyType',
fix(fixer) {
return fixer.insertTextAfter(node.argument, ' as any');
return fixer.insertTextAfter(node.argument, ` as ${getCastType(node.argument)}`);
},
});
}
Expand Down

0 comments on commit 547b4be

Please sign in to comment.