eslint-plugin-sonarjs
Version:
32 lines (31 loc) • 1.62 kB
TypeScript
import type { TSESTree } from '@typescript-eslint/utils';
import type { RequiredParserServices } from './parser-services.js';
export type TypeOrigin = {
internal: TSESTree.TypeNode[];
external: TSESTree.TypeNode[];
};
/**
* Partitions the syntactic members of a type annotation into "internal"
* (user-writable in this project) and "external" (declared in node_modules
* or in the TypeScript default lib).
*
* Operates on the syntactic AST node, not on a resolved `ts.Type`. Returning
* the original AST nodes lets callers produce locations, suggestions, or
* fixes targeting exactly what the user wrote.
*
* Classification rules per top-level member:
* - Keyword / literal types -> internal (the user wrote them directly).
* - TSTypeReference -> resolves the type name to a symbol and inspects its
* declarations. A reference is external only when ALL declarations live in
* files that satisfy `isSourceFileFromExternalLibrary` or
* `isSourceFileDefaultLibrary`. Any local declaration (declaration-merging
* escape hatch) makes the reference internal.
* - Any other composite constructor (TSIntersectionType, TSArrayType,
* TSTypeLiteral, TSConditionalType, ...) -> internal at the top level. We
* do not recurse; callers can if they need to.
*
* Known limitation: alias chains are not followed. If the user re-aliases an
* external type locally (e.g. `type Inner = ReactNode`), the local alias is
* internal because the user has a place to edit.
*/
export declare function classifyTypesByOrigin(typeNode: TSESTree.TypeNode, services: RequiredParserServices): TypeOrigin;