eslint-plugin-sonarjs
Version:
213 lines (212 loc) • 9.2 kB
TypeScript
import type estree from 'estree';
import ts from 'typescript';
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import type { RequiredParserServices } from './parser-services.js';
import type { Rule } from 'eslint';
export type RuleContext = TSESLint.RuleContext<string, string[]>;
/**
* Returns true when `node` resolves to the built-in `Array` type.
*/
export declare function isArray(node: estree.Node, services: RequiredParserServices): boolean;
/**
* TypeScript provides a set of utility types to facilitate type transformations.
* @see https://www.typescriptlang.org/docs/handbook/utility-types.html
*/
export declare const UTILITY_TYPES: Set<string>;
/**
* Checks if the provided node is a JS typed array like "BigInt64Array". See TYPED_ARRAY_TYPES
*
* @param node
* @param services
* @returns
*/
export declare function isTypedArray(node: estree.Node, services: RequiredParserServices): boolean;
/**
* Returns true when `node` resolves to a string-like type.
*/
export declare function isString(node: estree.Node, services: RequiredParserServices): boolean;
/**
* Returns true when `node` resolves to a number-like type.
*/
export declare function isNumber(node: estree.Node, services: RequiredParserServices): boolean;
/**
* Returns true when `type` is bigint-like.
*/
export declare function isBigIntType(type: ts.Type): boolean;
/**
* Returns true when `type` is number-like.
*/
export declare function isNumberType(type: ts.Type): boolean;
/**
* Returns true when `type` is string-like.
*/
export declare function isStringType(type: ts.Type): boolean;
/**
* Returns true when `node` resolves to a function type.
*/
export declare function isFunction(node: estree.Node, services: RequiredParserServices): boolean;
/**
* Returns true when `node` resolves to a union type.
*/
export declare function isUnion(node: estree.Node, services: RequiredParserServices): boolean;
/**
* Returns true when `node` resolves to `undefined`, `null`, or a union containing either.
*/
export declare function isUndefinedOrNull(node: estree.Node, services: RequiredParserServices): boolean;
/**
* Returns true when `node` resolves to a thenable type.
*/
export declare function isThenable(node: estree.Node, services: RequiredParserServices): boolean;
/**
* Checks if a node's type is either:
* - Thenable (Promise-like), OR
* - A union where ALL members are either thenable or "nothing" types (void, undefined, null)
*
* This is useful for rules like S3735 that allow voiding Promise operations,
* including cases like optional chaining (Promise<T> | undefined) or
* optional async callbacks (() => void | Promise<void>).
*/
export declare function isThenableOrVoidUnion(node: estree.Node, services: RequiredParserServices): boolean;
/**
* Like {@link isThenableOrVoidUnion}, but also accepts boolean members as guards.
*
* Short-circuit expressions such as `skipCheck || checkPromise` resolve to
* `boolean | Promise<void>`, where the boolean is only a guard deciding whether
* the promise runs. Voiding such an expression is the `no-floating-promises`
* idiom and must not be flagged.
*/
export declare function isThenableOrGuardUnion(node: estree.Node, services: RequiredParserServices): boolean;
/**
* Returns true when `type` is exactly `any`.
*/
export declare function isAny(type: ts.Type): boolean;
/**
* Returns true when both types are specific enough to compare (neither `any`
* nor `unknown`) and are mutually assignable.
*
* Mutual assignability is stricter than symbol equality for generic aliases:
* `Props<User>` and `Props<Product>` share the same alias symbol, but they are
* not mutually assignable and therefore do not represent the same effective type.
*/
export declare function areMutuallyAssignableTypes(checker: ts.TypeChecker, left: ts.Type | undefined, right: ts.Type | undefined): boolean;
/**
* Returns true when both types come from the same named declaration and, for
* generic declarations, are instantiated with the same type arguments.
*
* The comparison has two steps:
* 1. the declarations must resolve to the same symbol
* 2. each type argument must match through `areSameTypeArguments(...)`
*
* That helper can call back into this function for named type arguments, so
* generic comparisons recurse through nested declarations.
*
* This is intentionally stricter than structural equality: `Props<User>` and
* `Props<Product>` are different, and `Props<User>` also stays different from
* `Props<{ id: string }>` even if the shapes happen to match.
*/
export declare function areSameTypeDeclarations(checker: ts.TypeChecker, left: ts.Type | undefined, right: ts.Type | undefined): boolean;
export declare function isAnyOrUnknownType(type: ts.Type): boolean;
/**
* Checks if a node has a generic type like:
*
* function foo<T> (bar: T) {
* bar // is generic
* }
*
* @param node TSESTree.Node
* @param services RuleContext.parserServices
* @returns
*/
export declare function isGenericType(node: TSESTree.Node, services: RequiredParserServices): boolean;
/**
* Returns the TypeScript type resolved at `node`.
*/
export declare function getTypeFromTreeNode(node: estree.Node, services: RequiredParserServices): ts.Type;
/**
* Returns the widened string representation of the type resolved at `node`.
*/
export declare function getTypeAsString(node: estree.Node, services: RequiredParserServices): string;
/**
* Returns the symbol resolved at `node`, if any.
*/
export declare function getSymbolAtLocation(node: estree.Node, services: RequiredParserServices): ts.Symbol | undefined;
/**
* Returns the resolved call signature for the call-like expression at `node`.
*/
export declare function getSignatureFromCallee(node: estree.Node, services: RequiredParserServices): ts.Signature | undefined;
/**
* This function checks if a type may correspond to an array type. Beyond simple array types, it will also
* consider the union of array types and generic types extending an array type.
* @param type A type to check
* @param services The services used to get access to the TypeScript type checker
*/
export declare function isArrayLikeType(type: ts.Type, services: RequiredParserServices): boolean;
/**
* Test if the provided type is an array of strings.
* @param type A TypeScript type.
* @param services The services used to get access to the TypeScript type checker
*/
export declare function isStringArray(type: ts.Type, services: RequiredParserServices): boolean;
/**
* Test if the provided type is an array of numbers.
* @param type A TypeScript type.
* @param services The services used to get access to the TypeScript type checker
*/
export declare function isNumberArray(type: ts.Type, services: RequiredParserServices): boolean;
/**
* Test if the provided type is an array of big integers.
* @param type A TypeScript type.
* @param services The services used to get access to the TypeScript type checker
*/
export declare function isBigIntArray(type: ts.Type, services: RequiredParserServices): boolean;
/**
* Test if the provided type is an array of booleans.
* @param type A TypeScript type.
* @param services The services used to get access to the TypeScript type checker
*/
export declare function isBooleanArray(type: ts.Type, services: RequiredParserServices): boolean;
/**
* Checks whether a TypeScript type node denotes a type alias.
* @param node a type node to check
* @param context the rule context
*/
export declare function isTypeAlias(node: TSESTree.TypeNode, context: Rule.RuleContext): boolean | undefined;
/**
* Returns true when `type` is the boolean literal `true`.
*/
export declare function isBooleanTrueType(type: ts.Type): boolean;
/**
* Returns true when `type` is boolean-like.
*/
export declare function isBooleanType({ flags }: ts.Type): boolean;
/**
* Returns true when `type` is `null`, `undefined`, or a union containing either.
*/
export declare function isNullOrUndefinedType({ flags }: ts.Type): number;
/**
* Returns true when `type` is object-like.
*/
export declare function isObjectType({ flags }: ts.Type): number;
/**
* Returns true when `node` exposes a callable member named `methodName`.
*/
export declare function typeHasMethod(node: estree.Node, methodName: string, services: RequiredParserServices): boolean;
/**
* Checks if a type is iterable (can be used in for-of loops).
* @param node The node to check
* @param services The parser services
* @returns true if the type is iterable, false otherwise
*/
export declare function isIterable(node: estree.Node, services: RequiredParserServices): boolean;
/**
* Gets the number of parameters for a function-typed node using TypeScript type information.
* Returns null if the parameter count cannot be determined.
*
* For functions with multiple call signatures (overloads), returns the maximum
* parameter count across all signatures.
*
* @param node The node to get parameter count for
* @param services TypeScript parser services
* @returns The parameter count, or null if it cannot be determined
*/
export declare function getFunctionParameterCount(node: estree.Node, services: RequiredParserServices): number | null;