Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix(eslint-plugin): [no-unsafe-return] handle recursive type#10883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
11 changes: 11 additions & 0 deletionspackages/eslint-plugin/tests/rules/no-unsafe-return.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletionspackages/type-utils/src/discriminateAnyType.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import type * as ts from 'typescript'; | ||
| import * as tsutils from 'ts-api-utils'; | ||
| import { isTypeAnyType, isTypeAnyArrayType } from './predicates'; | ||
| export enum AnyType { | ||
| Any, | ||
| PromiseAny, | ||
| AnyArray, | ||
| Safe, | ||
| } | ||
| /** | ||
| * @returns `AnyType.Any` if the type is `any`, `AnyType.AnyArray` if the type is `any[]` or `readonly any[]`, `AnyType.PromiseAny` if the type is `Promise<any>`, | ||
| * otherwise it returns `AnyType.Safe`. | ||
| */ | ||
| export function discriminateAnyType( | ||
| type: ts.Type, | ||
| checker: ts.TypeChecker, | ||
| program: ts.Program, | ||
| tsNode: ts.Node, | ||
| ): AnyType { | ||
| return discriminateAnyTypeWorker(type, checker, program, tsNode, new Set()); | ||
| } | ||
| function discriminateAnyTypeWorker( | ||
| type: ts.Type, | ||
| checker: ts.TypeChecker, | ||
| program: ts.Program, | ||
| tsNode: ts.Node, | ||
| visited: Set<ts.Type>, | ||
| ) { | ||
| if (visited.has(type)) { | ||
| return AnyType.Safe; | ||
| } | ||
| visited.add(type); | ||
| if (isTypeAnyType(type)) { | ||
| return AnyType.Any; | ||
| } | ||
| if (isTypeAnyArrayType(type, checker)) { | ||
| return AnyType.AnyArray; | ||
| } | ||
| for (const part of tsutils.typeParts(type)) { | ||
| if (tsutils.isThenableType(checker, tsNode, part)) { | ||
| const awaitedType = checker.getAwaitedType(part); | ||
| if (awaitedType) { | ||
| const awaitedAnyType = discriminateAnyTypeWorker( | ||
| awaitedType, | ||
| checker, | ||
| program, | ||
| tsNode, | ||
| visited, | ||
| ); | ||
| if (awaitedAnyType === AnyType.Any) { | ||
| return AnyType.PromiseAny; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return AnyType.Safe; | ||
| } |
1 change: 1 addition & 0 deletionspackages/type-utils/src/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
42 changes: 0 additions & 42 deletionspackages/type-utils/src/predicates.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletionspackages/type-utils/tests/discriminateAnyType.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import type { TSESTree } from '@typescript-eslint/typescript-estree'; | ||
| import { parseForESLint } from '@typescript-eslint/parser'; | ||
| import path from 'node:path'; | ||
| import { AnyType, discriminateAnyType } from '../src'; | ||
| import { expectToHaveParserServices } from './test-utils/expectToHaveParserServices'; | ||
| type GetNode = (ast: TSESTree.Program) => TSESTree.Node; | ||
| describe('discriminateAnyType', () => { | ||
| const rootDir = path.join(__dirname, 'fixtures'); | ||
| function getDeclarationId(ast: TSESTree.Program): TSESTree.Node { | ||
| const declaration = ast.body.at(-1) as TSESTree.VariableDeclaration; | ||
| const id = declaration.declarations[0].id; | ||
| return id; | ||
| } | ||
| function getTypes(code: string, getNode: GetNode) { | ||
| const { ast, services } = parseForESLint(code, { | ||
| disallowAutomaticSingleRunInference: true, | ||
| filePath: path.join(rootDir, 'file.ts'), | ||
| project: './tsconfig.json', | ||
| tsconfigRootDir: rootDir, | ||
| }); | ||
| expectToHaveParserServices(services); | ||
| const node = getNode(ast); | ||
| const type = services.getTypeAtLocation(getNode(ast)); | ||
| return { | ||
| checker: services.program.getTypeChecker(), | ||
| program: services.program, | ||
| tsNode: services.esTreeNodeToTSNodeMap.get(node), | ||
| type, | ||
| }; | ||
| } | ||
| function runTest( | ||
| code: string, | ||
| expected: AnyType, | ||
| getNode: GetNode = getDeclarationId, | ||
| ): void { | ||
| const { checker, program, tsNode, type } = getTypes(code, getNode); | ||
| const result = discriminateAnyType(type, checker, program, tsNode); | ||
| expect(result).toBe(expected); | ||
| } | ||
| describe('returns Safe', () => { | ||
| it.each([ | ||
| ['const foo = "foo";', AnyType.Safe], | ||
| ['const foo = 1;', AnyType.Safe], | ||
| ['const foo = [1, 2];', AnyType.Safe], | ||
| ])('when code is %s, returns %s', runTest); | ||
| it('should returns Safe for a recursive thenable.', () => { | ||
| const code = ` | ||
| class Foo { | ||
| foo() { | ||
| return this; | ||
| } | ||
| protected then(resolve: () => void): void { | ||
| resolve(); | ||
| } | ||
| }; | ||
| `; | ||
| runTest(code, AnyType.Safe, ast => { | ||
| const classDeclration = ast.body[0] as TSESTree.ClassDeclaration; | ||
| const method = classDeclration.body | ||
| .body[0] as TSESTree.MethodDefinition; | ||
| const returnStatement = method.value.body?.body.at( | ||
| -1, | ||
| ) as TSESTree.ReturnStatement; | ||
| return returnStatement.argument!; | ||
| }); | ||
| }); | ||
| }); | ||
| describe('returns Any', () => { | ||
| it.each([ | ||
| ['const foo = 1 as any;', AnyType.Any], | ||
| ['let foo;', AnyType.Any], | ||
| ])('when code is %s, returns %s', runTest); | ||
| }); | ||
| describe('returns PromiseAny', () => { | ||
| it.each([ | ||
| ['const foo = Promise.resolve({} as any);', AnyType.PromiseAny], | ||
| [ | ||
| 'const foo = Promise.resolve(Promise.resolve({} as any));', | ||
| AnyType.PromiseAny, | ||
| ], | ||
| ])('when code is %s, returns %s', runTest); | ||
| }); | ||
| describe('returns AnyArray', () => { | ||
| it.each([ | ||
| ['const foo = [{} as any];', AnyType.AnyArray], | ||
| ['const foo = [{} as any, 2];', AnyType.AnyArray], | ||
| ])('when code is %s, returns %s', runTest); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.