eslint-plugin-sonarjs
Version:
114 lines (113 loc) • 6.01 kB
TypeScript
import type { Rule } from 'eslint';
import { ComputedCache } from '../cache.js';
import { type NormalizedAbsolutePath } from '../files.js';
import { type DependenciesList, type ModuleType } from './resolvers/types.js';
/**
* Cache for the available dependencies by closest manifest directory and search boundary.
* Exported for tests.
*/
declare class DependenciesCache {
private readonly cache;
get(dir: NormalizedAbsolutePath, topDir: NormalizedAbsolutePath): DependenciesList;
has(dir: NormalizedAbsolutePath, topDir: NormalizedAbsolutePath): boolean;
clear(): void;
get size(): number;
}
export declare const dependenciesCache: DependenciesCache;
/**
* Cache for module type signal by dirname. Exported for tests.
*/
export declare const moduleTypeCache: ComputedCache<NormalizedAbsolutePath, ModuleType | undefined, NormalizedAbsolutePath>;
/**
* Retrieve the dependencies of all the dependency manifest files available for the given file.
*
* @param dir dirname of the context.filename
* @param topDir working dir, will search up to that root
* @returns Map of dependency names to their version strings
*/
export declare function getDependencies(dir: NormalizedAbsolutePath, topDir: NormalizedAbsolutePath): DependenciesList;
/**
* Retrieve the module type signal for a file.
*
* Extension-specific module kinds (.mjs/.mts and .cjs/.cts) are explicit and
* take precedence. Otherwise, package.json#type from the closest manifest only
* is used. If that closest manifest exists but omits "type", default to CommonJS.
*/
export declare function getModuleType(filePath: NormalizedAbsolutePath, topDir: NormalizedAbsolutePath): ModuleType | undefined;
export declare function setCurrentFileInlineDependencies(deps: DependenciesList | null): void;
/**
* Merges inline npm: imports with manifest dependencies, giving precedence to inline imports.
*/
export declare function withCurrentFileInlineDependencies(manifest: DependenciesList): DependenciesList;
export declare function getDependenciesSanitizePaths(context: Rule.RuleContext): DependenciesList;
/**
* Determines whether a file belongs to an Angular project, based on the presence of
* `@angular/core` in the dependency manifests visible to the file: the closest `package.json` and
* every ancestor manifest up to `topDir`, unioned (see {@link getDependencies}). A root manifest
* that declares `@angular/core` therefore also marks nested packages as Angular — intentional, so
* the common monorepo layout where Angular is declared only at the workspace root (e.g. Nx) is
* still detected. The trade-off is that a nested non-Angular package inheriting a root Angular
* dependency is also treated as Angular; this only matters for paths already matching the narrow
* `environments/environment.<env>.ts` shape, so the practical over-reach is negligible.
*
* This is a path-based predicate (no `Rule.RuleContext`), so it can be used both by the
* scope-classification heuristic in `common/filter/filter-path.ts` — which decides MAIN/TEST
* before any rule runs — and, if needed, by rules. Keeping the Angular signal in one shared
* helper avoids duplicating the dependency list, and the underlying lookup is cached.
*
* @param filePath normalized absolute path of the file
* @param topDir normalized absolute directory bounding the upward manifest search (project base dir)
* @returns true when `@angular/core` is declared in the file's closest manifest or any ancestor up to `topDir`
*/
export declare function isAngularProject(filePath: NormalizedAbsolutePath, topDir: NormalizedAbsolutePath): boolean;
/**
* Gets the React version from the closest package.json.
*
* @param context ESLint rule context
* @returns React version string (coerced from range) or null if not found
*/
export declare function getReactVersion(context: Rule.RuleContext): string | null;
/**
* Parses a React version string and returns a valid semver version.
* Exported for testing purposes.
*
* @param reactVersion Version string from package.json (e.g., "^18.0.0", "19.0", "catalog:frontend")
* @returns Valid semver version string or null if parsing fails
*/
export declare function parseReactVersion(reactVersion: string): string | null;
/**
* Gets the raw Vue dependency range from the closest package.json.
*
* Unlike getReactVersion, this returns the range as declared (e.g. "^2.7.0 || ^3.0.0")
* rather than a single coerced version, since S8961 needs to know whether Vue 3
* is a possibility for the range, not just its lower bound.
*
* @param context ESLint rule context
* @returns Vue dependency range string, or null if not found
*/
export declare function getVueVersion(context: Rule.RuleContext): string | null;
export declare function getTypeScriptSignalsFromPackageJsonFiles(packageJsonFiles: Iterable<{
fileContent: string;
}>): {
typeScriptVersionSignals: string[];
hasTypeScriptNativePreview: boolean;
};
/**
* Gets a Node.js version signal by walking up from `dir` toward `topDir`,
* picking the closest package.json with @types/node (preferred) or engines.node.
*
* @param dir directory to start the upward search from (e.g. tsconfig dir, file dir)
* @param topDir analysis base directory; walk stops here. Defaults to `dir` to
* preserve the legacy "manifest at exactly this dir" semantics for callers
* that don't yet provide a separate analysis root.
* @returns raw version string from @types/node or engines.node, or null if not found
*/
export declare function getNodeVersionSignal(dir: NormalizedAbsolutePath, topDir?: NormalizedAbsolutePath): string | null;
/**
* Gets a TypeScript version signal from the closest package.json walking up
* from `dir` to `topDir`. Defaults `topDir = dir` for backward compatibility.
*
* @returns raw version string from typescript dependency, or null if not found
*/
export declare function getTypeScriptVersionSignal(dir: NormalizedAbsolutePath, topDir?: NormalizedAbsolutePath): string | null;
export {};