UNPKG

eslint-plugin-sonarjs

Version:
91 lines (90 loc) 4.09 kB
import type { Rule, SourceCode } from 'eslint'; import type estree from 'estree'; import type { RequiredParserServices } from '../../helpers/parser-services.js'; /** * Names of array sorting methods covered by S2871. * * This includes mutating sort-like methods such as `sort` and copying * sort-like methods such as `toSorted`. */ export declare const allSortLike: Set<string>; /** * Rule-scoped services needed by sort false-positive pattern matchers. * * Passing this object around keeps the extracted matchers independent from the * ESLint rule closure while still giving them access to scope lookup, source * text, and TypeScript type information. */ export type SortMatcherContext = { context: Rule.RuleContext; sourceCode: SourceCode; services: RequiredParserServices; }; /** * Information extracted from a comparator-less sort-like call. * * `methodName` distinguishes `sort()` from `toSorted()` so normalization * patterns can require both sides of a comparison to use the same sort family. * `receiver` is the array-like expression being sorted. */ export type ComparatorlessSortCallInfo = { methodName: string; receiver: estree.Node; }; /** * Checks whether a node is a direct `JSON.stringify(<expr>)` call. * * The match is intentionally narrow: exactly one argument, non-computed member * access, and the global-looking `JSON` identifier as receiver. It does not * match calls with replacer/space arguments, `JSON['stringify'](...)`, or * custom objects exposing a `stringify` method. */ export declare function isJsonStringifyCall(node: estree.Node | null): node is estree.CallExpression; /** * Returns the other side of an equality or inequality comparison. * * If `node` is not directly inside `==`, `!=`, `===`, or `!==`, there is no * comparison sibling to inspect and the function returns `null`. */ export declare function getEqualityComparisonSibling(node: estree.Node): estree.Node | null; /** * Returns a method call chained directly after an expression. * * For `object` representing `value.sort()`, asking for `map` matches * `value.sort().map(...)` and returns the `map(...)` call expression. Computed * member access such as `value.sort()['map'](...)` is intentionally ignored. */ export declare function getChainedMethodCall(object: estree.Node, methodName: string): estree.CallExpression | null; /** * Checks whether a call expression is `array.map(String)`. * * This recognizes the explicit conversion to strings that makes subsequent * default sorting intentional in the false-positive patterns handled by S2871. * Computed access such as `array[map](String)` is intentionally excluded. */ export declare function isStringMapCall(call: estree.CallExpression): boolean; /** * Extracts the separator from a `join` call when it is statically known. * * `join()` is treated as `join(',')`, matching JavaScript's default separator. * A single string literal argument is returned as-is. Other calls, such as * `join(separator)` or `join(',', extra)`, return `null` because the comparison * pattern can no longer prove both sides serialize identically. */ export declare function getJoinSeparator(call: estree.CallExpression): string | null; /** * Extracts information from a comparator-less `sort()` or `toSorted()` call. * * The call must have no arguments, use a known sort-like method name, and be * invoked on an array-like receiver according to TypeScript type information. */ export declare function getComparatorlessSortCallInfo(node: estree.Node, { sourceCode, services }: SortMatcherContext): ComparatorlessSortCallInfo | null; /** * Checks whether a sort receiver is an array of primitive values with safe * normalization semantics for the S2871 false-positive patterns. * * Only number, bigint, string, and boolean arrays are accepted. Arrays of * objects, unknown values, unions, or custom wrappers still require S2871 to * report comparator-less sorting. */ export declare function isPrimitiveSortReceiver(receiver: estree.Node, { services }: SortMatcherContext): boolean;