UNPKG

eslint-codemod-utils

Version:

A collection of AST helper functions for more complex ESLint rule fixes.

368 lines (367 loc) 8.39 kB
import { AST_NODE_TYPES } from '@typescript-eslint/types'; import { node } from './utils/node'; /** * __TSAsExpression__ * * @example * ```ts * const x = 'hello' as string * ^^^^^^^^^^^^^^^^^ * ``` * * @returns {TSESTree.TSAsExpression} */ export const tsAsExpression = ({ expression, typeAnnotation, ...other }) => { return { ...other, expression, typeAnnotation, type: AST_NODE_TYPES.TSAsExpression, toString: () => `${node(expression)} as ${node(typeAnnotation)}`, }; }; /** * __TSStringKeyword__ * * @example * ```ts * const x = 'hello' as string * ^^^^^^ * ``` * * @returns {TSESTree.TSStringKeyword} */ export const tsStringKeyword = ({ ...other }) => { return { ...other, type: AST_NODE_TYPES.TSStringKeyword, toString: () => `string`, }; }; /** * __TSAnyKeyword__ * * @example * ```ts * const x = 'hello' as any * ^^^ * ``` * * @returns {TSESTree.TSAnyKeyword} */ export const tsAnyKeyword = ({ ...other }) => { return { ...other, type: AST_NODE_TYPES.TSAnyKeyword, toString: () => `any`, }; }; /** * __TSTypeReference__ * * @example * ```ts * type World = string * * const x = 'hello' as World * ^^^^^^^ * ``` * * @returns {TSESTree.TSTypeReference} */ export const tsTypeReference = ({ typeName, typeParameters, ...other }) => { return { ...other, typeName, typeParameters, type: AST_NODE_TYPES.TSTypeReference, toString: () => `${node(typeName)}${typeParameters ? node(typeParameters) : ''}`, }; }; /** * __TSNullKeyword__ * * @example * ```ts * const x = 'hello' as null * ^^^^ * ``` * * @returns {TSESTree.TSNullKeyword} */ export const tsNullKeyword = ({ ...other }) => { return { ...other, type: AST_NODE_TYPES.TSNullKeyword, toString: () => `null`, }; }; export const tsUnknownKeyword = (node) => { return { ...node, type: AST_NODE_TYPES.TSUnknownKeyword, toString: () => `unknown`, }; }; export const tsBooleanKeyword = ({ ...other }) => { return { ...other, type: AST_NODE_TYPES.TSBooleanKeyword, toString: () => `boolean`, }; }; export const tsReadonlyKeyword = ({ ...other }) => { return { ...other, type: AST_NODE_TYPES.TSReadonlyKeyword, toString: () => `readonly`, }; }; export const tsEmptyBodyFunctionExpression = ({ returnType, ...other }) => { return { returnType, ...other, type: AST_NODE_TYPES.TSEmptyBodyFunctionExpression, toString: () => `function(){}`, }; }; export const tsQualifiedName = ({ left, right, ...other }) => { return { left, right, ...other, type: AST_NODE_TYPES.TSQualifiedName, toString: () => `${node(left)}.${node(right)}`, }; }; export const tsTypeParameterInstantiation = ({ params, ...other }) => { return { params, ...other, type: AST_NODE_TYPES.TSTypeParameterInstantiation, toString: () => `<${params.map(node).join(', ')}>`, }; }; export const tsTypeParameterDeclaration = ({ params, ...other }) => { return { params, ...other, type: AST_NODE_TYPES.TSTypeParameterDeclaration, toString: () => `<${params.map(node).join(', ')}>`, }; }; /** * __TSTypeOperator__ * * @example * ``` * type X = 'hello' * type Y = typeof X * ^^^^^^^^ * ``` */ export const tsTypeOperator = ({ typeAnnotation, operator, ...other }) => { return { ...other, typeAnnotation, operator, type: AST_NODE_TYPES.TSTypeOperator, toString: () => `${operator}${typeAnnotation ? ` ${node(typeAnnotation)}` : ''}`, }; }; /** * __TSTypeQuery__ * * @example * ``` * type X = typeof 'hello' * ``` */ export const tsTypeQuery = ({ exprName, typeParameters, ...other }) => { return { ...other, typeParameters, exprName, type: AST_NODE_TYPES.TSTypeQuery, toString: () => `typeof ${node(exprName)}${typeParameters ? node(typeParameters) : ''}`, }; }; /** * FIXME Implementation does not meet spec */ export const tsTypeParameter = ({ name, ...other }) => { return { ...other, name, type: AST_NODE_TYPES.TSTypeParameter, toString: () => `${node(name)}`, }; }; export const tsLiteralType = ({ literal, ...other }) => { return { literal, ...other, type: AST_NODE_TYPES.TSLiteralType, toString: () => `${node(literal)}`, }; }; /** * @example * ``` * element!.select() * ^^^^^^^^ * ``` */ export const tsNonNullExpression = ({ expression, ...other }) => { return { expression, ...other, type: AST_NODE_TYPES.TSNonNullExpression, toString: () => `${node(expression)}!`, }; }; /** * __TSTypeAliasDeclaration__ * @example * ``` * type Alias = number | boolean * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ * ``` */ export const tsTypeAliasDeclaration = ({ id, typeAnnotation, typeParameters, declare, ...other }) => { return { id, typeAnnotation, typeParameters, declare, ...other, type: AST_NODE_TYPES.TSTypeAliasDeclaration, toString: () => `${declare ? 'declare ' : ''}type ${node(id)}${typeParameters ? `${node(typeParameters)}` : ''} = ${node(typeAnnotation)}`, }; }; /** * __TSUnionType__ * @example * ``` * type Alias = number | boolean * ^^^^^^^^^^^^^^^^ * ``` */ export const tsUnionType = ({ types, ...other }) => { return { types, ...other, type: AST_NODE_TYPES.TSUnionType, toString: () => `${types.map(node).join(' | ')}`, }; }; /** * __TSIntersectionType__ * @example * ``` * type Alias = number & boolean * ^^^^^^^^^^^^^^^^ * ``` */ export const tsIntersectionType = ({ types, ...other }) => { return { types, ...other, type: AST_NODE_TYPES.TSIntersectionType, toString: () => `${types.map(node).join(' & ')}`, }; }; /** * __TSArrayType__ * @example * ``` * type Alias = number[] * ^^^^^^^^ * ``` */ export const tsArrayType = ({ elementType, ...other }) => { return { elementType, ...other, type: AST_NODE_TYPES.TSArrayType, toString: () => `${node(elementType)}[]`, }; }; /** * __TSSatisfiesExpression__ */ export const tsSatisfiesExpression = ({ expression, typeAnnotation, ...other }) => { return { ...other, expression, typeAnnotation, type: AST_NODE_TYPES.TSSatisfiesExpression, toString: () => `${node(expression)} satisfies ${node(typeAnnotation)}`, }; }; /** * __TSConditionalType__ */ export const tsConditionalType = ({ checkType, extendsType, trueType, falseType, ...other }) => { return { checkType, extendsType, trueType, falseType, ...other, type: AST_NODE_TYPES.TSConditionalType, toString: () => `${node(checkType)} extends ${node(extendsType)} ? ${node(trueType)} : ${node(falseType)}`, }; }; /** * __TSVoidKeyword__ * @example * ``` * function x(): void {} * ^^^^ * ``` */ export const tsVoidKeyword = ({ ...other }) => { return { ...other, type: AST_NODE_TYPES.TSVoidKeyword, toString: () => `void`, }; }; /** * __TSAbstractKeyword__ * * @example * ``` * abstract class X {} * ^^^^^^^^ * ``` */ export const tsAbstractKeyword = (node) => { return { ...node, type: AST_NODE_TYPES.TSAbstractKeyword, toString: () => `abstract`, }; }; export const tsUndefinedKeyword = ({ ...other }) => { return { ...other, type: AST_NODE_TYPES.TSUndefinedKeyword, toString: () => `undefined`, }; }; export const tsNeverKeyword = ({ ...other }) => { return { ...other, type: AST_NODE_TYPES.TSNeverKeyword, toString: () => 'never', }; }; export const tsAsyncKeyword = ({ ...other }) => { return { ...other, type: AST_NODE_TYPES.TSAsyncKeyword, toString: () => 'async', }; };