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/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/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 d6209d260673..ae98ead2dc86 100644 --- a/packages/ast-spec/src/type/spec.ts +++ b/packages/ast-spec/src/type/spec.ts @@ -1,14 +1,19 @@ +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'; export * from './TSInferType/spec'; export * from './TSIntersectionType/spec'; +export * from './TSIntrinsicKeyword/spec'; export * from './TSLiteralType/spec'; export * from './TSMappedType/spec'; export * from './TSNamedTupleMember/spec'; @@ -17,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 f6d4cbafffd1..6505270427b2 100644 --- a/packages/ast-spec/src/unions/Node.ts +++ b/packages/ast-spec/src/unions/Node.ts @@ -116,27 +116,22 @@ 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'; 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'; @@ -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 55436b7a44d4..461281cd4ce3 100644 --- a/packages/ast-spec/src/unions/TypeNode.ts +++ b/packages/ast-spec/src/unions/TypeNode.ts @@ -1,15 +1,19 @@ +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'; 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'; @@ -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..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 @@ -75,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}`); diff --git a/packages/utils/src/ast-utils/helpers.ts b/packages/utils/src/ast-utils/helpers.ts index fa2b8bb9f5cb..1c29164991de 100644 --- a/packages/utils/src/ast-utils/helpers.ts +++ b/packages/utils/src/ast-utils/helpers.ts @@ -1,75 +1,74 @@ 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 = (nodeType: NodeType) => ( node: TSESTree.Node | null | undefined, - ): node is TSESTree.Node & { type: NodeType } => + ): node is Extract => node?.type === nodeType; 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 = < 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 TSESTree.Node & { type: NodeType } & Conditions) => { +) => node is ExtractedNode & Conditions) => { const entries = Object.entries(conditions) as ObjectEntries; return ( node: TSESTree.Node | null | undefined, - ): node is TSESTree.Node & { type: NodeType } & Conditions => + ): node is ExtractedNode & Conditions => node?.type === nodeType && - entries.every(([key, value]) => node[key] === value); + 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 TSESTree.Token & { type: TokenType } & Conditions) => { +) => token is ExtractedToken & Conditions) => { const entries = Object.entries(conditions) as ObjectEntries; return ( token: TSESTree.Token | null | undefined, - ): token is TSESTree.Token & { type: TokenType } & Conditions => + ): token is ExtractedToken & Conditions => token?.type === tokenType && - entries.every(([key, value]) => token[key] === value); + entries.every( + ([key, value]) => token[key as keyof TSESTree.Token] === value, + ); }; 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, - TSESTree.Token & { type: TokenType } & Conditions - >) => - ( - token, - ): token is Exclude< - TSESTree.Token, - TSESTree.Token & { type: TokenType } & Conditions - > => + ) => token is Exclude) => + (token): token is Exclude => !isTokenOfTypeWithConditions(tokenType, conditions)(token);