From c03292700fa4e7ec460fe958b0fa0997946b8d89 Mon Sep 17 00:00:00 2001 From: Hasegawa-Yukihiro Date: Sat, 20 Apr 2024 13:27:26 +0900 Subject: [PATCH 1/4] test: add testCase --- .../type-utils/tests/typeFlagUtils.test.ts | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 packages/type-utils/tests/typeFlagUtils.test.ts diff --git a/packages/type-utils/tests/typeFlagUtils.test.ts b/packages/type-utils/tests/typeFlagUtils.test.ts new file mode 100644 index 00000000000..fd9d0603150 --- /dev/null +++ b/packages/type-utils/tests/typeFlagUtils.test.ts @@ -0,0 +1,94 @@ +import { parseForESLint } from '@typescript-eslint/parser'; +import type { TSESTree } from '@typescript-eslint/typescript-estree'; +import path from 'path'; +import * as ts from 'typescript'; + +import { getTypeFlags, isTypeFlagSet } from '../src'; +import { expectToHaveParserServices } from './test-utils/expectToHaveParserServices'; + +describe('typeFlagUtils', () => { + const rootDir = path.join(__dirname, 'fixtures'); + + function getType(code: string): ts.Type { + const { ast, services } = parseForESLint(code, { + project: './tsconfig.json', + filePath: path.join(rootDir, 'file.ts'), + tsconfigRootDir: rootDir, + }); + expectToHaveParserServices(services); + const declaration = ast.body[0] as TSESTree.TSTypeAliasDeclaration; + + return services.getTypeAtLocation(declaration.id); + } + + describe('getTypeFlags', () => { + function runTestForAliasDeclaration( + code: string, + expected: ts.TypeFlags, + ): void { + const type = getType(code); + const result = getTypeFlags(type); + expect(result).toBe(expected); + } + + it.each([ + ['type Test = any;', 1], + ['type Test = unknown;', 2], + ['type Test = string;', 4], + ['type Test = number;', 8], + ['type Test = "text";', 128], + ['type Test = 123;', 256], + ['type Test = string | number', 12], + ['type Test = "text" | 123', 384], + ])('when code is "%s", type flags is %d', runTestForAliasDeclaration); + }); + + describe('isTypeFlagSet', () => { + function runTestForAliasDeclaration( + code: string, + flagsToCheck: ts.TypeFlags, + expected: boolean, + ): void { + const type = getType(code); + const result = isTypeFlagSet(type, flagsToCheck); + expect(result).toBe(expected); + } + + describe('is type flagas set', () => { + function runTestIsTypeFlagSet( + code: string, + flagsToCheck: ts.TypeFlags, + ): void { + runTestForAliasDeclaration(code, flagsToCheck, true); + } + + it.each([ + ['type Test = any;', ts.TypeFlags.Any], + ['type Test = string;', ts.TypeFlags.String], + ['type Test = string | number;', ts.TypeFlags.String], + ['type Test = string & { foo: string };', ts.TypeFlags.Intersection], + ])( + 'when code is "%s" and flagsToCheck is %d , returns is true', + runTestIsTypeFlagSet, + ); + }); + + describe('is not type flagas set', () => { + function runTestIsNotTypeFlagSet( + code: string, + flagsToCheck: ts.TypeFlags, + ): void { + runTestForAliasDeclaration(code, flagsToCheck, false); + } + + it.each([ + ['type Test = string', ts.TypeFlags.Any], + ['type Test = string | number;', ts.TypeFlags.Any], + ['type Test = string & { foo: string }', ts.TypeFlags.String], + ])( + 'when code is "%s" and flagsToCheck is %d , returns is false', + runTestIsNotTypeFlagSet, + ); + }); + }); +}); From 921b08ab05cf5d360fb105424f9089cb74ef7d7a Mon Sep 17 00:00:00 2001 From: Hasegawa-Yukihiro Date: Sun, 21 Apr 2024 14:42:20 +0900 Subject: [PATCH 2/4] fix: typo --- packages/type-utils/tests/typeFlagUtils.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/type-utils/tests/typeFlagUtils.test.ts b/packages/type-utils/tests/typeFlagUtils.test.ts index fd9d0603150..d914300891d 100644 --- a/packages/type-utils/tests/typeFlagUtils.test.ts +++ b/packages/type-utils/tests/typeFlagUtils.test.ts @@ -68,7 +68,7 @@ describe('typeFlagUtils', () => { ['type Test = string | number;', ts.TypeFlags.String], ['type Test = string & { foo: string };', ts.TypeFlags.Intersection], ])( - 'when code is "%s" and flagsToCheck is %d , returns is true', + 'when code is "%s" and flagsToCheck is %d , returns true', runTestIsTypeFlagSet, ); }); @@ -86,7 +86,7 @@ describe('typeFlagUtils', () => { ['type Test = string | number;', ts.TypeFlags.Any], ['type Test = string & { foo: string }', ts.TypeFlags.String], ])( - 'when code is "%s" and flagsToCheck is %d , returns is false', + 'when code is "%s" and flagsToCheck is %d , returns false', runTestIsNotTypeFlagSet, ); }); From b855fa1d16c92c43527be5a8b678a3449c279129 Mon Sep 17 00:00:00 2001 From: Hasegawa-Yukihiro Date: Mon, 6 May 2024 10:21:15 +0900 Subject: [PATCH 3/4] fix: typo --- packages/type-utils/tests/typeFlagUtils.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/type-utils/tests/typeFlagUtils.test.ts b/packages/type-utils/tests/typeFlagUtils.test.ts index d914300891d..9cf699efaa5 100644 --- a/packages/type-utils/tests/typeFlagUtils.test.ts +++ b/packages/type-utils/tests/typeFlagUtils.test.ts @@ -54,7 +54,7 @@ describe('typeFlagUtils', () => { expect(result).toBe(expected); } - describe('is type flagas set', () => { + describe('is type flags set', () => { function runTestIsTypeFlagSet( code: string, flagsToCheck: ts.TypeFlags, @@ -73,7 +73,7 @@ describe('typeFlagUtils', () => { ); }); - describe('is not type flagas set', () => { + describe('is not type flags set', () => { function runTestIsNotTypeFlagSet( code: string, flagsToCheck: ts.TypeFlags, From 4d6d8ceb46fd77361dd834d28d650dd38c6b6539 Mon Sep 17 00:00:00 2001 From: Hasegawa-Yukihiro Date: Sun, 2 Jun 2024 15:06:00 +0900 Subject: [PATCH 4/4] add deprecation notice --- packages/type-utils/src/typeFlagUtils.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/type-utils/src/typeFlagUtils.ts b/packages/type-utils/src/typeFlagUtils.ts index dde3ca3c596..cfbe48f5ee7 100644 --- a/packages/type-utils/src/typeFlagUtils.ts +++ b/packages/type-utils/src/typeFlagUtils.ts @@ -27,10 +27,12 @@ export function getTypeFlags(type: ts.Type): ts.TypeFlags { export function isTypeFlagSet( type: ts.Type, flagsToCheck: ts.TypeFlags, + /** @deprecated This params is not used and will be removed in the future.*/ isReceiver?: boolean, ): boolean { const flags = getTypeFlags(type); + // eslint-disable-next-line deprecation/deprecation -- not used if (isReceiver && flags & ANY_OR_UNKNOWN) { return true; }