From 547b4bed87bf12c87f22c793b4995b9fe486b984 Mon Sep 17 00:00:00 2001 From: pipopotamasu Date: Tue, 27 Feb 2024 21:54:36 -0500 Subject: [PATCH] improve return statement type --- rules/helper.ts | 6 +++--- .../return-statement/index.spec.ts | 9 ++------- rules/no-implicit-any/return-statement/index.ts | 15 +++++++++++++-- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/rules/helper.ts b/rules/helper.ts index a2bdf21..77c84fa 100644 --- a/rules/helper.ts +++ b/rules/helper.ts @@ -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'; } diff --git a/rules/no-implicit-any/return-statement/index.spec.ts b/rules/no-implicit-any/return-statement/index.spec.ts index d6852ef..daa02ce 100644 --- a/rules/no-implicit-any/return-statement/index.spec.ts +++ b/rules/no-implicit-any/return-statement/index.spec.ts @@ -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' }], }, { diff --git a/rules/no-implicit-any/return-statement/index.ts b/rules/no-implicit-any/return-statement/index.ts index 7d4a99f..fac7d4e 100644 --- a/rules/no-implicit-any/return-statement/index.ts +++ b/rules/no-implicit-any/return-statement/index.ts @@ -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) { @@ -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) { @@ -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)}`); }, }); }