Helper functions for path manipulation, package name mangling, project affinity sorting, and file system operations.
Converts scoped package names for @types/* lookup by replacing forward slashes with double underscores.
/**
* Converts scoped package names for @types/* lookup
* For a scoped package, we must look in `@types/foo__bar` instead of `@types/@foo/bar`
* @param moduleName - Package name to mangle
* @returns Mangled package name suitable for @types/* lookup
*/
function mangleScopedPackage(moduleName: string): string;Usage Examples:
import { mangleScopedPackage } from "eslint-import-resolver-typescript";
// Scoped package conversion
console.log(mangleScopedPackage("@angular/core")); // "angular__core"
console.log(mangleScopedPackage("@types/node")); // "types__node"
// Non-scoped packages remain unchanged
console.log(mangleScopedPackage("lodash")); // "lodash"
console.log(mangleScopedPackage("react")); // "react"
// Used internally for @types/* resolution
const typesPackage = `@types/${mangleScopedPackage("@angular/core")}`;
// Results in: "@types/angular__core"Removes trailing querystring parameters from module identifiers while preserving the core module path.
/**
* Remove any trailing querystring from module identifier
* @param id - Module identifier that may contain query parameters
* @returns Clean module identifier without querystring
*/
function removeQuerystring(id: string): string;Usage Examples:
import { removeQuerystring } from "eslint-import-resolver-typescript";
// Remove query parameters
console.log(removeQuerystring("./module?inline")); // "./module"
console.log(removeQuerystring("@types/node?v=1.0")); // "@types/node"
console.log(removeQuerystring("./styles.css?url")); // "./styles.css"
// No querystring - returns original
console.log(removeQuerystring("./module")); // "./module"
console.log(removeQuerystring("react")); // "react"Sorts TypeScript project paths by directory affinity to the target file, placing the most relevant configurations first.
/**
* Sorts TypeScript project paths by directory affinity to target file
* Projects closer to the target file in the directory hierarchy are prioritized
* @param projects - Array of TypeScript project configuration paths
* @param file - Target file path for affinity calculation
* @returns Sorted array of project paths, most relevant first
*/
function sortProjectsByAffinity(projects: string[], file: string): string[];Usage Examples:
import { sortProjectsByAffinity } from "eslint-import-resolver-typescript";
const projects = [
"/root/packages/utils/tsconfig.json",
"/root/tsconfig.json",
"/root/packages/core/tsconfig.json"
];
// Sort by affinity to a core package file
const sorted = sortProjectsByAffinity(projects, "/root/packages/core/src/index.ts");
// Result: [
// "/root/packages/core/tsconfig.json", // Closest match
// "/root/packages/utils/tsconfig.json", // Same level
// "/root/tsconfig.json" // Root level
// ]
// Sort by affinity to a root-level file
const rootSorted = sortProjectsByAffinity(projects, "/root/src/main.ts");
// Result: [
// "/root/tsconfig.json", // Closest match
// "/root/packages/core/tsconfig.json", // Child directories
// "/root/packages/utils/tsconfig.json"
// ]Attempts to locate files or directories with support for multiple paths and various search options.
/**
* Attempts to locate a file or directory
* @param filename - Single file/directory path or array of paths to try
* @param includeDir - Whether to include directories in results (default: false)
* @param base - Base directory for relative path resolution (default: process.cwd())
* @returns First found file/directory path, or empty string if none found
*/
function tryFile(
filename?: string[] | string,
includeDir?: boolean,
base?: string
): string;Usage Examples:
import { tryFile } from "eslint-import-resolver-typescript";
// Single file lookup
const configPath = tryFile("tsconfig.json");
console.log(configPath); // "/path/to/tsconfig.json" or ""
// Multiple file lookup (tries in order)
const foundConfig = tryFile([
"tsconfig.json",
"jsconfig.json",
"tsconfig.base.json"
]);
// Directory lookup
const srcDir = tryFile("src", true);
console.log(srcDir); // "/path/to/src" or ""
// Custom base directory
const configInCustomDir = tryFile(
"tsconfig.json",
false,
"/custom/project/path"
);
// Array of paths with custom base
const configFiles = tryFile([
"configs/tsconfig.json",
"configs/jsconfig.json"
], false, "/project/root");Functions for converting between different path formats for cross-platform compatibility.
/**
* Converts native path separators to forward slashes for glob patterns
* @param pathname - Path with native separators
* @returns Path with forward slashes
*/
function toGlobPath(pathname: string): string;
/**
* Converts forward slashes to native path separators
* @param pathname - Path with forward slashes
* @returns Path with native separators
*/
function toNativePath(pathname: string): string;Usage Examples:
import { toGlobPath, toNativePath } from "eslint-import-resolver-typescript";
// Convert to glob-compatible paths (always forward slashes)
console.log(toGlobPath("src\\components\\Button.tsx")); // "src/components/Button.tsx"
console.log(toGlobPath("src/components/Button.tsx")); // "src/components/Button.tsx"
// Convert to native paths (platform-specific separators)
// On Windows:
console.log(toNativePath("src/components/Button.tsx")); // "src\\components\\Button.tsx"
// On Unix/Linux/macOS:
console.log(toNativePath("src/components/Button.tsx")); // "src/components/Button.tsx"Debug logger instance for the resolver using the 'debug' package.
/**
* Debug logger instance for the resolver
* Uses debug package with 'eslint-import-resolver-typescript' namespace
*/
declare const log: (message?: any, ...optionalParams: any[]) => void;
/**
* Mutable variable holding the default configuration file path
* Automatically detected from the current working directory
*/
declare let defaultConfigFile: string;Additional internal utilities used by the resolver:
/**
* Computes directory affinity between two paths
* Lower values indicate higher affinity (closer relationship)
*/
declare function computeAffinity(projectDir: string, targetDir: string): number;The utility functions handle various error conditions gracefully:
The utilities implement several performance optimizations: