xo
Version:
JavaScript/TypeScript linter (ESLint wrapper) with great defaults
144 lines (136 loc) • 4.97 kB
TypeScript
import { ESLint, type Linter } from 'eslint';
import prettier from 'prettier';
import { type XoLintResult, type LinterOptions, type LintTextOptions, type XoConfigOptions, type XoConfigItem } from './types.js';
import { xoToEslintConfig } from './xo-to-eslint.js';
export declare const ignoredFileWarningMessage = "File ignored because of a matching ignore pattern.";
export declare class Xo {
/**
Static helper to convert an XO config to an ESLint config to be used in `eslint.config.js`.
*/
static xoToEslintConfig: typeof xoToEslintConfig;
/**
Static helper for backwards compatibility and use in editor extensions and other tools.
*/
static lintText(code: string, options: LintTextOptions & LinterOptions & XoConfigOptions): Promise<XoLintResult>;
/**
Static helper for backwards compatibility and use in editor extensions and other tools.
*/
static lintFiles(globs: string | undefined, options: LinterOptions & XoConfigOptions): Promise<XoLintResult>;
/**
Write the fixes to disk.
*/
static outputFixes(results: XoLintResult): Promise<void>;
/**
Required linter options: `cwd`, `fix`, and `filePath` (in case of `lintText`).
*/
linterOptions: LinterOptions;
/**
Base XO config options that allow configuration from CLI or other sources. Not to be confused with the `xoConfig` property which is the resolved XO config from the flat config AND base config.
*/
baseXoConfig: XoConfigOptions;
/**
File path to the ESLint cache.
*/
cacheLocation: string;
/**
A re-usable ESLint instance configured with options calculated from the XO config.
*/
eslint?: ESLint;
/**
XO config derived from both the base config and the resolved flat config.
*/
xoConfig?: XoConfigItem[];
/**
The ESLint config calculated from the resolved XO config.
*/
eslintConfig?: Linter.Config[];
/**
The flat XO config path, if there is one.
*/
flatConfigPath?: string | undefined;
/**
If any user configs contain Prettier, we will need to fetch the Prettier config.
*/
prettier?: boolean;
/**
The Prettier config if it exists and is needed.
*/
prettierConfig?: prettier.Options;
/**
The glob pattern for TypeScript files, for which we will handle TS files and tsconfig.
We expand this based on the XO config and the files glob patterns.
*/
tsFilesGlob: string[];
/**
We use this to also add negative glob patterns in case a user overrides the parserOptions in their XO config.
*/
tsFilesIgnoresGlob: string[];
/**
Track whether ignores have been added to prevent duplicate ignore configs.
*/
private ignoresHandled;
/**
Store per-file configs separately from base config to prevent unbounded array growth.
Key: file path, Value: config for that file.
This prevents memory bloat in long-running processes (e.g., language servers).
*/
private readonly fileConfigs;
/**
Track virtual/stdin files that share a single tsconfig.stdin.json.
These are handled differently from regular files.
*/
private readonly virtualFiles;
constructor(_linterOptions: LinterOptions, _baseXoConfig?: XoConfigOptions);
/**
Sets the XO config on the XO instance.
@private
*/
setXoConfig(): Promise<void>;
/**
Sets the ESLint config on the XO instance.
@private
*/
setEslintConfig(): void;
/**
Sets the ignores on the XO instance.
@private
*/
setIgnores(): void;
/**
Ensures the cache directory exists. This needs to run once before both tsconfig handling and running ESLint occur.
@private
*/
ensureCacheDirectory(): Promise<void>;
/**
Checks every TS file to ensure its included in the tsconfig and any that are not included are added to an in-memory TypeScript Program for type aware linting.
@param files - The TypeScript files being linted.
*/
handleUnincludedTsFiles(files?: string[]): Promise<void>;
/**
Initializes the ESLint instance on the XO instance.
*/
initEslint(files?: string[]): Promise<void>;
/**
Lints the files on the XO instance.
@param globs - Glob pattern to pass to `globby`.
@throws Error
*/
lintFiles(globs?: string | string[]): Promise<XoLintResult>;
/**
Lints the text on the XO instance.
*/
lintText(code: string, lintTextOptions: LintTextOptions): Promise<XoLintResult>;
calculateConfigForFile(filePath: string): Promise<Linter.Config>;
getFormatter(name: string): Promise<ESLint.LoadedFormatter>;
/**
Add virtual files to the config with a tsconfig approach.
*/
private addVirtualFilesToConfig;
/**
Add existing files to the config with an in-memory TypeScript Program.
*/
private addExistingFilesToConfig;
private processReport;
private getReportStatistics;
}
export default Xo;