From 0dc5052b1cf7c7a30335e29f0f3c27a4bc04d919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Sat, 12 Feb 2022 16:28:29 +0100 Subject: [PATCH 01/10] chore(utils): use `Extract` generic for `ast-utils`' `predicates`' helper functions --- .../src/type/{TSIntrinsicType => TSIntrinsicKeyword}/spec.ts | 0 packages/ast-spec/src/type/spec.ts | 1 + packages/ast-spec/src/unions/Node.ts | 2 +- packages/ast-spec/src/unions/TypeNode.ts | 2 +- packages/utils/src/ast-utils/helpers.ts | 2 +- 5 files changed, 4 insertions(+), 3 deletions(-) rename packages/ast-spec/src/type/{TSIntrinsicType => TSIntrinsicKeyword}/spec.ts (100%) diff --git a/packages/ast-spec/src/type/TSIntrinsicType/spec.ts b/packages/ast-spec/src/type/TSIntrinsicKeyword/spec.ts similarity index 100% rename from packages/ast-spec/src/type/TSIntrinsicType/spec.ts rename to packages/ast-spec/src/type/TSIntrinsicKeyword/spec.ts diff --git a/packages/ast-spec/src/type/spec.ts b/packages/ast-spec/src/type/spec.ts index d6209d260673..20fffd6e20ff 100644 --- a/packages/ast-spec/src/type/spec.ts +++ b/packages/ast-spec/src/type/spec.ts @@ -9,6 +9,7 @@ export * from './TSImportType/spec'; export * from './TSIndexedAccessType/spec'; export * from './TSInferType/spec'; export * from './TSIntersectionType/spec'; +export * from './TSIntrinsicKeyword/spec'; export * from './TSLiteralType/spec'; export * from './TSMappedType/spec'; export * from './TSNamedTupleMember/spec'; diff --git a/packages/ast-spec/src/unions/Node.ts b/packages/ast-spec/src/unions/Node.ts index f6d4cbafffd1..fa9a42e1cbf9 100644 --- a/packages/ast-spec/src/unions/Node.ts +++ b/packages/ast-spec/src/unions/Node.ts @@ -136,7 +136,7 @@ import type { TSImportType } from '../type/TSImportType/spec'; import type { TSIndexedAccessType } from '../type/TSIndexedAccessType/spec'; import type { TSInferType } from '../type/TSInferType/spec'; import type { TSIntersectionType } from '../type/TSIntersectionType/spec'; -import type { TSIntrinsicKeyword } from '../type/TSIntrinsicType/spec'; +import type { TSIntrinsicKeyword } from '../type/TSIntrinsicKeyword/spec'; import type { TSLiteralType } from '../type/TSLiteralType/spec'; import type { TSMappedType } from '../type/TSMappedType/spec'; import type { TSNamedTupleMember } from '../type/TSNamedTupleMember/spec'; diff --git a/packages/ast-spec/src/unions/TypeNode.ts b/packages/ast-spec/src/unions/TypeNode.ts index 55436b7a44d4..76ab8f9689c1 100644 --- a/packages/ast-spec/src/unions/TypeNode.ts +++ b/packages/ast-spec/src/unions/TypeNode.ts @@ -9,7 +9,7 @@ import type { TSImportType } from '../type/TSImportType/spec'; import type { TSIndexedAccessType } from '../type/TSIndexedAccessType/spec'; import type { TSInferType } from '../type/TSInferType/spec'; import type { TSIntersectionType } from '../type/TSIntersectionType/spec'; -import type { TSIntrinsicKeyword } from '../type/TSIntrinsicType/spec'; +import type { TSIntrinsicKeyword } from '../type/TSIntrinsicKeyword/spec'; import type { TSLiteralType } from '../type/TSLiteralType/spec'; import type { TSMappedType } from '../type/TSMappedType/spec'; import type { TSNamedTupleMember } from '../type/TSNamedTupleMember/spec'; diff --git a/packages/utils/src/ast-utils/helpers.ts b/packages/utils/src/ast-utils/helpers.ts index fa2b8bb9f5cb..94375f7fc850 100644 --- a/packages/utils/src/ast-utils/helpers.ts +++ b/packages/utils/src/ast-utils/helpers.ts @@ -7,7 +7,7 @@ export const isNodeOfType = (nodeType: NodeType) => ( node: TSESTree.Node | null | undefined, - ): node is TSESTree.Node & { type: NodeType } => + ): node is Extract => node?.type === nodeType; export const isNodeOfTypes = From 04da28620d33a19f177dca73a142f2304faf8050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Mon, 7 Mar 2022 04:23:50 +0100 Subject: [PATCH 02/10] chore: enable for `isNodeOfTypes` --- packages/utils/src/ast-utils/helpers.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/utils/src/ast-utils/helpers.ts b/packages/utils/src/ast-utils/helpers.ts index 94375f7fc850..f596f3af3612 100644 --- a/packages/utils/src/ast-utils/helpers.ts +++ b/packages/utils/src/ast-utils/helpers.ts @@ -14,7 +14,7 @@ export const isNodeOfTypes = (nodeTypes: NodeTypes) => ( node: TSESTree.Node | null | undefined, - ): node is TSESTree.Node & { type: NodeTypes[number] } => + ): node is Extract => !!node && nodeTypes.includes(node.type); export const isNodeOfTypeWithConditions = < From 8921bd4981117817bb7604835017c2f44040488e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Mon, 7 Mar 2022 17:37:45 +0100 Subject: [PATCH 03/10] chore: move TS keywords to Node --- packages/ast-spec/src/token/spec.ts | 9 --------- .../{token => type}/TSAbstractKeyword/spec.ts | 0 .../src/{token => type}/TSAsyncKeyword/spec.ts | 0 .../{token => type}/TSDeclareKeyword/spec.ts | 0 .../{token => type}/TSExportKeyword/spec.ts | 0 .../{token => type}/TSPrivateKeyword/spec.ts | 0 .../{token => type}/TSProtectedKeyword/spec.ts | 0 .../{token => type}/TSPublicKeyword/spec.ts | 0 .../{token => type}/TSReadonlyKeyword/spec.ts | 0 .../{token => type}/TSStaticKeyword/spec.ts | 0 packages/ast-spec/src/type/spec.ts | 9 +++++++++ packages/ast-spec/src/unions/Modifier.ts | 14 +++++++------- packages/ast-spec/src/unions/Node.ts | 18 +++++++++--------- packages/ast-spec/src/unions/TypeNode.ts | 18 ++++++++++++++++++ .../sort-type-union-intersection-members.ts | 9 +++++++++ 15 files changed, 52 insertions(+), 25 deletions(-) rename packages/ast-spec/src/{token => type}/TSAbstractKeyword/spec.ts (100%) rename packages/ast-spec/src/{token => type}/TSAsyncKeyword/spec.ts (100%) rename packages/ast-spec/src/{token => type}/TSDeclareKeyword/spec.ts (100%) rename packages/ast-spec/src/{token => type}/TSExportKeyword/spec.ts (100%) rename packages/ast-spec/src/{token => type}/TSPrivateKeyword/spec.ts (100%) rename packages/ast-spec/src/{token => type}/TSProtectedKeyword/spec.ts (100%) rename packages/ast-spec/src/{token => type}/TSPublicKeyword/spec.ts (100%) rename packages/ast-spec/src/{token => type}/TSReadonlyKeyword/spec.ts (100%) rename packages/ast-spec/src/{token => type}/TSStaticKeyword/spec.ts (100%) diff --git a/packages/ast-spec/src/token/spec.ts b/packages/ast-spec/src/token/spec.ts index 45df05189eb2..3cffaf49e837 100644 --- a/packages/ast-spec/src/token/spec.ts +++ b/packages/ast-spec/src/token/spec.ts @@ -10,13 +10,4 @@ export * from './NumericToken/spec'; export * from './PunctuatorToken/spec'; export * from './RegularExpressionToken/spec'; export * from './StringToken/spec'; -export * from './TSAbstractKeyword/spec'; -export * from './TSAsyncKeyword/spec'; -export * from './TSDeclareKeyword/spec'; -export * from './TSExportKeyword/spec'; -export * from './TSPrivateKeyword/spec'; -export * from './TSProtectedKeyword/spec'; -export * from './TSPublicKeyword/spec'; -export * from './TSReadonlyKeyword/spec'; -export * from './TSStaticKeyword/spec'; export * from './TemplateToken/spec'; diff --git a/packages/ast-spec/src/token/TSAbstractKeyword/spec.ts b/packages/ast-spec/src/type/TSAbstractKeyword/spec.ts similarity index 100% rename from packages/ast-spec/src/token/TSAbstractKeyword/spec.ts rename to packages/ast-spec/src/type/TSAbstractKeyword/spec.ts diff --git a/packages/ast-spec/src/token/TSAsyncKeyword/spec.ts b/packages/ast-spec/src/type/TSAsyncKeyword/spec.ts similarity index 100% rename from packages/ast-spec/src/token/TSAsyncKeyword/spec.ts rename to packages/ast-spec/src/type/TSAsyncKeyword/spec.ts diff --git a/packages/ast-spec/src/token/TSDeclareKeyword/spec.ts b/packages/ast-spec/src/type/TSDeclareKeyword/spec.ts similarity index 100% rename from packages/ast-spec/src/token/TSDeclareKeyword/spec.ts rename to packages/ast-spec/src/type/TSDeclareKeyword/spec.ts diff --git a/packages/ast-spec/src/token/TSExportKeyword/spec.ts b/packages/ast-spec/src/type/TSExportKeyword/spec.ts similarity index 100% rename from packages/ast-spec/src/token/TSExportKeyword/spec.ts rename to packages/ast-spec/src/type/TSExportKeyword/spec.ts diff --git a/packages/ast-spec/src/token/TSPrivateKeyword/spec.ts b/packages/ast-spec/src/type/TSPrivateKeyword/spec.ts similarity index 100% rename from packages/ast-spec/src/token/TSPrivateKeyword/spec.ts rename to packages/ast-spec/src/type/TSPrivateKeyword/spec.ts diff --git a/packages/ast-spec/src/token/TSProtectedKeyword/spec.ts b/packages/ast-spec/src/type/TSProtectedKeyword/spec.ts similarity index 100% rename from packages/ast-spec/src/token/TSProtectedKeyword/spec.ts rename to packages/ast-spec/src/type/TSProtectedKeyword/spec.ts diff --git a/packages/ast-spec/src/token/TSPublicKeyword/spec.ts b/packages/ast-spec/src/type/TSPublicKeyword/spec.ts similarity index 100% rename from packages/ast-spec/src/token/TSPublicKeyword/spec.ts rename to packages/ast-spec/src/type/TSPublicKeyword/spec.ts diff --git a/packages/ast-spec/src/token/TSReadonlyKeyword/spec.ts b/packages/ast-spec/src/type/TSReadonlyKeyword/spec.ts similarity index 100% rename from packages/ast-spec/src/token/TSReadonlyKeyword/spec.ts rename to packages/ast-spec/src/type/TSReadonlyKeyword/spec.ts diff --git a/packages/ast-spec/src/token/TSStaticKeyword/spec.ts b/packages/ast-spec/src/type/TSStaticKeyword/spec.ts similarity index 100% rename from packages/ast-spec/src/token/TSStaticKeyword/spec.ts rename to packages/ast-spec/src/type/TSStaticKeyword/spec.ts diff --git a/packages/ast-spec/src/type/spec.ts b/packages/ast-spec/src/type/spec.ts index 20fffd6e20ff..ae98ead2dc86 100644 --- a/packages/ast-spec/src/type/spec.ts +++ b/packages/ast-spec/src/type/spec.ts @@ -1,9 +1,13 @@ +export * from './TSAbstractKeyword/spec'; export * from './TSAnyKeyword/spec'; export * from './TSArrayType/spec'; +export * from './TSAsyncKeyword/spec'; export * from './TSBigIntKeyword/spec'; export * from './TSBooleanKeyword/spec'; export * from './TSConditionalType/spec'; export * from './TSConstructorType/spec'; +export * from './TSDeclareKeyword/spec'; +export * from './TSExportKeyword/spec'; export * from './TSFunctionType/spec'; export * from './TSImportType/spec'; export * from './TSIndexedAccessType/spec'; @@ -18,8 +22,13 @@ export * from './TSNullKeyword/spec'; export * from './TSNumberKeyword/spec'; export * from './TSObjectKeyword/spec'; export * from './TSOptionalType/spec'; +export * from './TSPrivateKeyword/spec'; +export * from './TSProtectedKeyword/spec'; +export * from './TSPublicKeyword/spec'; export * from './TSQualifiedName/spec'; +export * from './TSReadonlyKeyword/spec'; export * from './TSRestType/spec'; +export * from './TSStaticKeyword/spec'; export * from './TSStringKeyword/spec'; export * from './TSSymbolKeyword/spec'; export * from './TSTemplateLiteralType/spec'; diff --git a/packages/ast-spec/src/unions/Modifier.ts b/packages/ast-spec/src/unions/Modifier.ts index 0922a52eca2e..f2501e6585cb 100644 --- a/packages/ast-spec/src/unions/Modifier.ts +++ b/packages/ast-spec/src/unions/Modifier.ts @@ -1,10 +1,10 @@ -import type { TSAbstractKeyword } from '../token/TSAbstractKeyword/spec'; -import type { TSAsyncKeyword } from '../token/TSAsyncKeyword/spec'; -import type { TSPrivateKeyword } from '../token/TSPrivateKeyword/spec'; -import type { TSProtectedKeyword } from '../token/TSProtectedKeyword/spec'; -import type { TSPublicKeyword } from '../token/TSPublicKeyword/spec'; -import type { TSReadonlyKeyword } from '../token/TSReadonlyKeyword/spec'; -import type { TSStaticKeyword } from '../token/TSStaticKeyword/spec'; +import type { TSAbstractKeyword } from '../type/TSAbstractKeyword/spec'; +import type { TSAsyncKeyword } from '../type/TSAsyncKeyword/spec'; +import type { TSPrivateKeyword } from '../type/TSPrivateKeyword/spec'; +import type { TSProtectedKeyword } from '../type/TSProtectedKeyword/spec'; +import type { TSPublicKeyword } from '../type/TSPublicKeyword/spec'; +import type { TSReadonlyKeyword } from '../type/TSReadonlyKeyword/spec'; +import type { TSStaticKeyword } from '../type/TSStaticKeyword/spec'; export type Modifier = | TSAbstractKeyword diff --git a/packages/ast-spec/src/unions/Node.ts b/packages/ast-spec/src/unions/Node.ts index fa9a42e1cbf9..6505270427b2 100644 --- a/packages/ast-spec/src/unions/Node.ts +++ b/packages/ast-spec/src/unions/Node.ts @@ -116,21 +116,16 @@ import type { TryStatement } from '../statement/TryStatement/spec'; import type { TSExportAssignment } from '../statement/TSExportAssignment/spec'; import type { WhileStatement } from '../statement/WhileStatement/spec'; import type { WithStatement } from '../statement/WithStatement/spec'; -import type { TSAbstractKeyword } from '../token/TSAbstractKeyword/spec'; -import type { TSAsyncKeyword } from '../token/TSAsyncKeyword/spec'; -import type { TSDeclareKeyword } from '../token/TSDeclareKeyword/spec'; -import type { TSExportKeyword } from '../token/TSExportKeyword/spec'; -import type { TSPrivateKeyword } from '../token/TSPrivateKeyword/spec'; -import type { TSProtectedKeyword } from '../token/TSProtectedKeyword/spec'; -import type { TSPublicKeyword } from '../token/TSPublicKeyword/spec'; -import type { TSReadonlyKeyword } from '../token/TSReadonlyKeyword/spec'; -import type { TSStaticKeyword } from '../token/TSStaticKeyword/spec'; +import type { TSAbstractKeyword } from '../type/TSAbstractKeyword/spec'; import type { TSAnyKeyword } from '../type/TSAnyKeyword/spec'; import type { TSArrayType } from '../type/TSArrayType/spec'; +import type { TSAsyncKeyword } from '../type/TSAsyncKeyword/spec'; import type { TSBigIntKeyword } from '../type/TSBigIntKeyword/spec'; import type { TSBooleanKeyword } from '../type/TSBooleanKeyword/spec'; import type { TSConditionalType } from '../type/TSConditionalType/spec'; import type { TSConstructorType } from '../type/TSConstructorType/spec'; +import type { TSDeclareKeyword } from '../type/TSDeclareKeyword/spec'; +import type { TSExportKeyword } from '../type/TSExportKeyword/spec'; import type { TSFunctionType } from '../type/TSFunctionType/spec'; import type { TSImportType } from '../type/TSImportType/spec'; import type { TSIndexedAccessType } from '../type/TSIndexedAccessType/spec'; @@ -145,8 +140,13 @@ import type { TSNullKeyword } from '../type/TSNullKeyword/spec'; import type { TSNumberKeyword } from '../type/TSNumberKeyword/spec'; import type { TSObjectKeyword } from '../type/TSObjectKeyword/spec'; import type { TSOptionalType } from '../type/TSOptionalType/spec'; +import type { TSPrivateKeyword } from '../type/TSPrivateKeyword/spec'; +import type { TSProtectedKeyword } from '../type/TSProtectedKeyword/spec'; +import type { TSPublicKeyword } from '../type/TSPublicKeyword/spec'; import type { TSQualifiedName } from '../type/TSQualifiedName/spec'; +import type { TSReadonlyKeyword } from '../type/TSReadonlyKeyword/spec'; import type { TSRestType } from '../type/TSRestType/spec'; +import type { TSStaticKeyword } from '../type/TSStaticKeyword/spec'; import type { TSStringKeyword } from '../type/TSStringKeyword/spec'; import type { TSSymbolKeyword } from '../type/TSSymbolKeyword/spec'; import type { TSTemplateLiteralType } from '../type/TSTemplateLiteralType/spec'; diff --git a/packages/ast-spec/src/unions/TypeNode.ts b/packages/ast-spec/src/unions/TypeNode.ts index 76ab8f9689c1..461281cd4ce3 100644 --- a/packages/ast-spec/src/unions/TypeNode.ts +++ b/packages/ast-spec/src/unions/TypeNode.ts @@ -1,9 +1,13 @@ +import type { TSAbstractKeyword } from '../type/TSAbstractKeyword/spec'; import type { TSAnyKeyword } from '../type/TSAnyKeyword/spec'; import type { TSArrayType } from '../type/TSArrayType/spec'; +import type { TSAsyncKeyword } from '../type/TSAsyncKeyword/spec'; import type { TSBigIntKeyword } from '../type/TSBigIntKeyword/spec'; import type { TSBooleanKeyword } from '../type/TSBooleanKeyword/spec'; import type { TSConditionalType } from '../type/TSConditionalType/spec'; import type { TSConstructorType } from '../type/TSConstructorType/spec'; +import type { TSDeclareKeyword } from '../type/TSDeclareKeyword/spec'; +import type { TSExportKeyword } from '../type/TSExportKeyword/spec'; import type { TSFunctionType } from '../type/TSFunctionType/spec'; import type { TSImportType } from '../type/TSImportType/spec'; import type { TSIndexedAccessType } from '../type/TSIndexedAccessType/spec'; @@ -18,7 +22,12 @@ import type { TSNullKeyword } from '../type/TSNullKeyword/spec'; import type { TSNumberKeyword } from '../type/TSNumberKeyword/spec'; import type { TSObjectKeyword } from '../type/TSObjectKeyword/spec'; import type { TSOptionalType } from '../type/TSOptionalType/spec'; +import type { TSPrivateKeyword } from '../type/TSPrivateKeyword/spec'; +import type { TSProtectedKeyword } from '../type/TSProtectedKeyword/spec'; +import type { TSPublicKeyword } from '../type/TSPublicKeyword/spec'; +import type { TSReadonlyKeyword } from '../type/TSReadonlyKeyword/spec'; import type { TSRestType } from '../type/TSRestType/spec'; +import type { TSStaticKeyword } from '../type/TSStaticKeyword/spec'; import type { TSStringKeyword } from '../type/TSStringKeyword/spec'; import type { TSSymbolKeyword } from '../type/TSSymbolKeyword/spec'; import type { TSTemplateLiteralType } from '../type/TSTemplateLiteralType/spec'; @@ -35,12 +44,16 @@ import type { TSUnknownKeyword } from '../type/TSUnknownKeyword/spec'; import type { TSVoidKeyword } from '../type/TSVoidKeyword/spec'; export type TypeNode = + | TSAbstractKeyword | TSAnyKeyword | TSArrayType + | TSAsyncKeyword | TSBigIntKeyword | TSBooleanKeyword | TSConditionalType | TSConstructorType + | TSDeclareKeyword + | TSExportKeyword | TSFunctionType | TSImportType | TSIndexedAccessType @@ -55,7 +68,12 @@ export type TypeNode = | TSNumberKeyword | TSObjectKeyword | TSOptionalType + | TSPrivateKeyword + | TSProtectedKeyword + | TSPublicKeyword + | TSReadonlyKeyword | TSRestType + | TSStaticKeyword | TSStringKeyword | TSSymbolKeyword | TSTemplateLiteralType diff --git a/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts b/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts index 201c411afee8..7aa8dd734588 100644 --- a/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts +++ b/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts @@ -32,12 +32,21 @@ function getGroup(node: TSESTree.TypeNode): Group { case AST_NODE_TYPES.TSIntersectionType: return Group.intersection; + case AST_NODE_TYPES.TSAbstractKeyword: case AST_NODE_TYPES.TSAnyKeyword: + case AST_NODE_TYPES.TSAsyncKeyword: case AST_NODE_TYPES.TSBigIntKeyword: case AST_NODE_TYPES.TSBooleanKeyword: + case AST_NODE_TYPES.TSDeclareKeyword: + case AST_NODE_TYPES.TSExportKeyword: case AST_NODE_TYPES.TSNeverKeyword: case AST_NODE_TYPES.TSNumberKeyword: case AST_NODE_TYPES.TSObjectKeyword: + case AST_NODE_TYPES.TSPrivateKeyword: + case AST_NODE_TYPES.TSProtectedKeyword: + case AST_NODE_TYPES.TSPublicKeyword: + case AST_NODE_TYPES.TSReadonlyKeyword: + case AST_NODE_TYPES.TSStaticKeyword: case AST_NODE_TYPES.TSStringKeyword: case AST_NODE_TYPES.TSSymbolKeyword: case AST_NODE_TYPES.TSThisType: From 98b4df0de2028d19bdd347c13a5f2292c249f3d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Mon, 7 Mar 2022 17:07:22 +0100 Subject: [PATCH 04/10] chore: enable for `isNodeOfTypeWithConditions` --- packages/utils/src/ast-utils/helpers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/utils/src/ast-utils/helpers.ts b/packages/utils/src/ast-utils/helpers.ts index f596f3af3612..cdc9f801d8e3 100644 --- a/packages/utils/src/ast-utils/helpers.ts +++ b/packages/utils/src/ast-utils/helpers.ts @@ -25,12 +25,12 @@ export const isNodeOfTypeWithConditions = < conditions: Conditions, ): (( node: TSESTree.Node | null | undefined, -) => node is TSESTree.Node & { type: NodeType } & Conditions) => { +) => node is Extract & Conditions) => { const entries = Object.entries(conditions) as ObjectEntries; return ( node: TSESTree.Node | null | undefined, - ): node is TSESTree.Node & { type: NodeType } & Conditions => + ): node is Extract & Conditions => node?.type === nodeType && entries.every(([key, value]) => node[key] === value); }; From 328a82f8b9cd9d076186ca8302300c236ceb5752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Mon, 7 Mar 2022 19:20:34 +0100 Subject: [PATCH 05/10] chore: enable for `isTokenOfTypeWithConditions` --- packages/utils/src/ast-utils/helpers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/utils/src/ast-utils/helpers.ts b/packages/utils/src/ast-utils/helpers.ts index cdc9f801d8e3..a550694d2bd5 100644 --- a/packages/utils/src/ast-utils/helpers.ts +++ b/packages/utils/src/ast-utils/helpers.ts @@ -43,12 +43,12 @@ export const isTokenOfTypeWithConditions = < conditions: Conditions, ): (( token: TSESTree.Token | null | undefined, -) => token is TSESTree.Token & { type: TokenType } & Conditions) => { +) => token is Extract & Conditions) => { const entries = Object.entries(conditions) as ObjectEntries; return ( token: TSESTree.Token | null | undefined, - ): token is TSESTree.Token & { type: TokenType } & Conditions => + ): token is Extract & Conditions => token?.type === tokenType && entries.every(([key, value]) => token[key] === value); }; From 52db9d8b987e629ad0a803d156a1a3d4ae1bfe34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Mon, 7 Mar 2022 19:23:23 +0100 Subject: [PATCH 06/10] chore: enable for `isNotTokenOfTypeWithConditions` --- packages/utils/src/ast-utils/helpers.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/utils/src/ast-utils/helpers.ts b/packages/utils/src/ast-utils/helpers.ts index a550694d2bd5..65faca00d398 100644 --- a/packages/utils/src/ast-utils/helpers.ts +++ b/packages/utils/src/ast-utils/helpers.ts @@ -64,12 +64,12 @@ export const isNotTokenOfTypeWithConditions = token: TSESTree.Token | null | undefined, ) => token is Exclude< TSESTree.Token, - TSESTree.Token & { type: TokenType } & Conditions + Extract & Conditions >) => ( token, ): token is Exclude< TSESTree.Token, - TSESTree.Token & { type: TokenType } & Conditions + Extract & Conditions > => !isTokenOfTypeWithConditions(tokenType, conditions)(token); From d3f4b5dfa5a06642900d9d71fdd5c869211be70b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Mon, 7 Mar 2022 19:36:18 +0100 Subject: [PATCH 07/10] fix: fix `Conditions` type for `isNodeOfTypeWithConditions` --- packages/utils/src/ast-utils/helpers.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/utils/src/ast-utils/helpers.ts b/packages/utils/src/ast-utils/helpers.ts index 65faca00d398..6d506c3a83d9 100644 --- a/packages/utils/src/ast-utils/helpers.ts +++ b/packages/utils/src/ast-utils/helpers.ts @@ -1,6 +1,8 @@ import { AST_NODE_TYPES, AST_TOKEN_TYPES, TSESTree } from '../ts-estree'; -type ObjectEntry = [keyof BaseType, BaseType[keyof BaseType]]; +type ObjectEntry = BaseType extends unknown + ? [keyof BaseType, BaseType[keyof BaseType]] + : never; type ObjectEntries = Array>; export const isNodeOfType = @@ -19,7 +21,7 @@ export const isNodeOfTypes = export const isNodeOfTypeWithConditions = < NodeType extends AST_NODE_TYPES, - Conditions extends Partial, + Conditions extends Partial>, >( nodeType: NodeType, conditions: Conditions, @@ -32,7 +34,7 @@ export const isNodeOfTypeWithConditions = < node: TSESTree.Node | null | undefined, ): node is Extract & Conditions => node?.type === nodeType && - entries.every(([key, value]) => node[key] === value); + entries.every(([key, value]) => key in node && node[key] === value); }; export const isTokenOfTypeWithConditions = < @@ -50,13 +52,13 @@ export const isTokenOfTypeWithConditions = < token: TSESTree.Token | null | undefined, ): token is Extract & Conditions => token?.type === tokenType && - entries.every(([key, value]) => token[key] === value); + entries.every(([key, value]) => key in token && token[key] === value); }; export const isNotTokenOfTypeWithConditions = < TokenType extends AST_TOKEN_TYPES, - Conditions extends Partial, + Conditions extends Partial>, >( tokenType: TokenType, conditions: Conditions, From 5b52f9be6693e3dd6bb15df499f9fa0306cb4f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Mon, 13 Jun 2022 13:50:24 +0200 Subject: [PATCH 08/10] fix: fix `entries.every` condition in `isNodeOfTypeWithConditions` --- packages/utils/src/ast-utils/helpers.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/utils/src/ast-utils/helpers.ts b/packages/utils/src/ast-utils/helpers.ts index 6d506c3a83d9..e0a39c86a927 100644 --- a/packages/utils/src/ast-utils/helpers.ts +++ b/packages/utils/src/ast-utils/helpers.ts @@ -34,7 +34,7 @@ export const isNodeOfTypeWithConditions = < node: TSESTree.Node | null | undefined, ): node is Extract & Conditions => node?.type === nodeType && - entries.every(([key, value]) => key in node && node[key] === value); + entries.every(([key, value]) => node[key as keyof TSESTree.Node] === value); }; export const isTokenOfTypeWithConditions = < @@ -52,7 +52,9 @@ export const isTokenOfTypeWithConditions = < token: TSESTree.Token | null | undefined, ): token is Extract & Conditions => token?.type === tokenType && - entries.every(([key, value]) => key in token && token[key] === value); + entries.every( + ([key, value]) => token[key as keyof TSESTree.Token] === value, + ); }; export const isNotTokenOfTypeWithConditions = From 3da4a942145a8db5bc03b330f3d9f99d1c913fa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Mon, 13 Jun 2022 13:58:43 +0200 Subject: [PATCH 09/10] chore: extract `ExtractedNode` & `ExtractedToken` generics --- packages/utils/src/ast-utils/helpers.ts | 27 ++++++++++--------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/packages/utils/src/ast-utils/helpers.ts b/packages/utils/src/ast-utils/helpers.ts index e0a39c86a927..1c29164991de 100644 --- a/packages/utils/src/ast-utils/helpers.ts +++ b/packages/utils/src/ast-utils/helpers.ts @@ -21,36 +21,38 @@ export const isNodeOfTypes = export const isNodeOfTypeWithConditions = < NodeType extends AST_NODE_TYPES, - Conditions extends Partial>, + ExtractedNode extends Extract, + Conditions extends Partial, >( nodeType: NodeType, conditions: Conditions, ): (( node: TSESTree.Node | null | undefined, -) => node is Extract & Conditions) => { +) => node is ExtractedNode & Conditions) => { const entries = Object.entries(conditions) as ObjectEntries; return ( node: TSESTree.Node | null | undefined, - ): node is Extract & Conditions => + ): node is ExtractedNode & Conditions => node?.type === nodeType && entries.every(([key, value]) => node[key as keyof TSESTree.Node] === value); }; export const isTokenOfTypeWithConditions = < TokenType extends AST_TOKEN_TYPES, + ExtractedToken extends Extract, Conditions extends Partial, >( tokenType: TokenType, conditions: Conditions, ): (( token: TSESTree.Token | null | undefined, -) => token is Extract & Conditions) => { +) => token is ExtractedToken & Conditions) => { const entries = Object.entries(conditions) as ObjectEntries; return ( token: TSESTree.Token | null | undefined, - ): token is Extract & Conditions => + ): token is ExtractedToken & Conditions => token?.type === tokenType && entries.every( ([key, value]) => token[key as keyof TSESTree.Token] === value, @@ -60,20 +62,13 @@ export const isTokenOfTypeWithConditions = < export const isNotTokenOfTypeWithConditions = < TokenType extends AST_TOKEN_TYPES, - Conditions extends Partial>, + ExtractedToken extends Extract, + Conditions extends Partial, >( tokenType: TokenType, conditions: Conditions, ): (( token: TSESTree.Token | null | undefined, - ) => token is Exclude< - TSESTree.Token, - Extract & Conditions - >) => - ( - token, - ): token is Exclude< - TSESTree.Token, - Extract & Conditions - > => + ) => token is Exclude) => + (token): token is Exclude => !isTokenOfTypeWithConditions(tokenType, conditions)(token); From 8def652fced1a524cfead524a7f4dc1cdaafb9c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20De=20Boey?= Date: Mon, 22 Aug 2022 14:53:50 +0200 Subject: [PATCH 10/10] fix: fix `getGroup` --- .../sort-type-union-intersection-members.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts b/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts index 7aa8dd734588..074578acf006 100644 --- a/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts +++ b/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts @@ -32,21 +32,12 @@ function getGroup(node: TSESTree.TypeNode): Group { case AST_NODE_TYPES.TSIntersectionType: return Group.intersection; - case AST_NODE_TYPES.TSAbstractKeyword: case AST_NODE_TYPES.TSAnyKeyword: - case AST_NODE_TYPES.TSAsyncKeyword: case AST_NODE_TYPES.TSBigIntKeyword: case AST_NODE_TYPES.TSBooleanKeyword: - case AST_NODE_TYPES.TSDeclareKeyword: - case AST_NODE_TYPES.TSExportKeyword: case AST_NODE_TYPES.TSNeverKeyword: case AST_NODE_TYPES.TSNumberKeyword: case AST_NODE_TYPES.TSObjectKeyword: - case AST_NODE_TYPES.TSPrivateKeyword: - case AST_NODE_TYPES.TSProtectedKeyword: - case AST_NODE_TYPES.TSPublicKeyword: - case AST_NODE_TYPES.TSReadonlyKeyword: - case AST_NODE_TYPES.TSStaticKeyword: case AST_NODE_TYPES.TSStringKeyword: case AST_NODE_TYPES.TSSymbolKeyword: case AST_NODE_TYPES.TSThisType: @@ -84,9 +75,18 @@ function getGroup(node: TSESTree.TypeNode): Group { return Group.union; // These types should never occur as part of a union/intersection + case AST_NODE_TYPES.TSAbstractKeyword: + case AST_NODE_TYPES.TSAsyncKeyword: + case AST_NODE_TYPES.TSDeclareKeyword: + case AST_NODE_TYPES.TSExportKeyword: case AST_NODE_TYPES.TSNamedTupleMember: case AST_NODE_TYPES.TSOptionalType: + case AST_NODE_TYPES.TSPrivateKeyword: + case AST_NODE_TYPES.TSProtectedKeyword: + case AST_NODE_TYPES.TSPublicKeyword: + case AST_NODE_TYPES.TSReadonlyKeyword: case AST_NODE_TYPES.TSRestType: + case AST_NODE_TYPES.TSStaticKeyword: case AST_NODE_TYPES.TSTypePredicate: /* istanbul ignore next */ throw new Error(`Unexpected Type ${node.type}`);