-
-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathxo.ts
More file actions
824 lines (673 loc) · 24.5 KB
/
xo.ts
File metadata and controls
824 lines (673 loc) · 24.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
import path from 'node:path';
import os from 'node:os';
import syncFs from 'node:fs';
import fs from 'node:fs/promises';
import process from 'node:process';
import {ESLint, type Linter} from 'eslint';
import findCacheDirectory from 'find-cache-directory';
import {globby, isDynamicPattern} from 'globby';
import micromatch from 'micromatch';
import arrify from 'arrify';
import defineLazyProperty from 'define-lazy-prop';
import prettier from 'prettier';
import type ts from 'typescript';
import {
type XoLintResult,
type LinterOptions,
type LintTextOptions,
type XoConfigOptions,
type XoConfigItem,
} from './types.js';
import {
defaultIgnores,
cacheDirName,
allExtensions,
tsFilesGlob,
tsconfigDefaults,
} from './constants.js';
import {xoToEslintConfig} from './xo-to-eslint.js';
import resolveXoConfig from './resolve-config.js';
import {handleTsconfig} from './handle-ts-files.js';
import {
matchFilesForTsConfig,
preProcessXoConfig,
typescriptParser,
} from './utils.js';
type TypeScriptParserOptions = Linter.ParserOptions & {
project?: string | string[] | boolean;
projectService?: boolean;
tsconfigRootDir?: string;
programs?: unknown[];
};
type XoError = Error & {
exitCode: number;
};
export const ignoredFileWarningMessage = 'File ignored because of a matching ignore pattern.';
export const noFilesFoundErrorMessage = 'No files matching the pattern were found.';
const suppressionsFileMissingErrorMessage = 'The suppressions file does not exist. Please run the command with `--suppress-all` or `--suppress-rule` to create it.';
const createErrorWithExitCode = (message: string, exitCode: number): XoError => Object.assign(new Error(message), {exitCode});
const createIgnoredLintResult = (filePath: string): ESLint.LintResult => ({
filePath,
messages: [
{
ruleId: null,
severity: 1,
message: ignoredFileWarningMessage,
line: 0,
column: 0,
},
],
suppressedMessages: [],
errorCount: 0,
fatalErrorCount: 0,
warningCount: 1,
fixableErrorCount: 0,
fixableWarningCount: 0,
usedDeprecatedRules: [],
});
const normalizeGlobPath = (filePath: string): string => filePath.split(path.sep).join('/');
const pathMatchesPattern = (filePath: string, pattern: string): boolean => micromatch.isMatch(normalizeGlobPath(filePath), normalizeGlobPath(pattern), {dot: true});
const isObjectRecord = (value: unknown): value is Record<string, unknown> => typeof value === 'object' && value !== null;
const isIgnoredByPatterns = (filePath: string, patterns: string[]): boolean => {
let ignored = false;
for (const pattern of patterns) {
if (pattern.startsWith('!')) {
if (pathMatchesPattern(filePath, pattern.slice(1))) {
ignored = false;
}
continue;
}
if (pathMatchesPattern(filePath, pattern)) {
ignored = true;
}
}
return ignored;
};
const isIgnoredFile = (cwd: string, filePath: string, patterns: string[]): boolean => isIgnoredByPatterns(path.relative(cwd, filePath), patterns);
const resolveExplicitFilePath = (cwd: string, glob: string): string | undefined => {
if (isDynamicPattern(glob)) {
// Negated and wildcard globs are treated as regular glob filtering, not as explicit file paths that should trigger an ignored-file warning.
return undefined;
}
const absolutePath = path.resolve(cwd, glob);
try {
if (syncFs.statSync(absolutePath).isFile()) {
return absolutePath;
}
} catch {
// File does not exist or is inaccessible.
}
return undefined;
};
const getIgnoredExplicitFileResults = async (cwd: string, globs: string[], eslint: ESLint, discoveryIgnores: string[] = []): Promise<ESLint.LintResult[]> => {
const explicitFilePaths = [...new Set(globs
.map(glob => resolveExplicitFilePath(cwd, glob))
.filter(filePath => filePath !== undefined))];
const results = await Promise.all(explicitFilePaths.map(async filePath => {
if (isIgnoredFile(cwd, filePath, discoveryIgnores)) {
return createIgnoredLintResult(filePath);
}
return await eslint.isPathIgnored(filePath) ? createIgnoredLintResult(filePath) : undefined;
}));
return results.filter(result => result !== undefined);
};
const isGlobalIgnoreConfig = (config: XoConfigItem): boolean => {
const keys = Object.keys(config);
return Boolean(config.ignores && (keys.length === 1 || (keys.length === 2 && config.name)));
};
const expandIgnoreNegationForEslint = (pattern: string): string[] => {
const negatedPattern = pattern.slice(1);
const {base, isGlob} = micromatch.scan(negatedPattern, {parts: true});
const parentPath = isGlob ? base : path.posix.dirname(negatedPattern);
if (!parentPath || parentPath === '.') {
return [pattern];
}
const expandedPatterns = parentPath.split('/').map((_, index, segments) => `!${segments.slice(0, index + 1).join('/')}`);
expandedPatterns.push(pattern);
return expandedPatterns;
};
const expandIgnoreNegationsForEslint = (patterns: string[]): string[] => patterns.flatMap(pattern => pattern.startsWith('!') ? expandIgnoreNegationForEslint(pattern) : [pattern]);
const expandGlobalIgnoreConfigForEslint = (config: XoConfigItem): XoConfigItem => {
if (!isGlobalIgnoreConfig(config)) {
return config;
}
return {
...config,
ignores: expandIgnoreNegationsForEslint(arrify(config.ignores)),
};
};
const stripDefaultIgnoreConfigs = (configs: Linter.Config[]): Linter.Config[] => configs.map(configItem => {
const {ignores} = configItem;
const isDefaultIgnoreConfig = ignores?.length && ignores.every(pattern => defaultIgnores.includes(pattern));
if (!isDefaultIgnoreConfig) {
return configItem;
}
const {ignores: _ignored, ...configWithoutIgnores} = configItem;
return configWithoutIgnores;
});
const defaultIgnoreOverlapsReopenedPattern = (defaultIgnore: string, pattern: string): boolean => {
const {base, isGlob} = micromatch.scan(pattern, {parts: true});
const reopenedBase = isGlob ? base : path.posix.dirname(pattern) || pattern;
const {base: defaultBase, isGlob: isDefaultGlob} = micromatch.scan(defaultIgnore, {parts: true});
const ignoreBase = isDefaultGlob ? defaultBase : defaultIgnore;
return micromatch.isMatch(pattern, defaultIgnore, {dot: true})
|| micromatch.isMatch(defaultIgnore, pattern, {dot: true})
|| !ignoreBase
|| !reopenedBase
|| ignoreBase.startsWith(`${reopenedBase}/`)
|| reopenedBase.startsWith(`${ignoreBase}/`);
};
const getReopenedDefaultPatterns = (patterns: string[]): string[] => patterns
.filter(pattern => pattern.startsWith('!'))
.map(pattern => pattern.slice(1))
.filter(pattern => defaultIgnores.some(defaultIgnore => defaultIgnoreOverlapsReopenedPattern(defaultIgnore, pattern)));
/**
XO only compensates for negations that reopen its built-in default ignores.
User-provided positive ignores are still used only for pruning.
The flow is:
1. discover with `defaultIgnores + positive global ignores`
2. if a negation reopens a built-in default ignore, run one extra pass with only that default ignore relaxed
3. apply the real global ignore order in XO: default ignores, then config ignores, then CLI ignores
4. let ESLint make the final ignore decision
This keeps the common "lint files XO ignores by default" behavior without trying to fully reimplement ESLint's ignore engine during discovery.
*/
const discoverLintFiles = async ({cwd, globs, positiveGlobalIgnores, discoveryIgnores, reopenedDefaultPatterns}: {
cwd: string;
globs: string[];
positiveGlobalIgnores: string[];
discoveryIgnores: string[];
reopenedDefaultPatterns: string[];
}): Promise<string[]> => {
const discoveredFiles = await globby(globs, {
ignore: [...defaultIgnores, ...positiveGlobalIgnores],
onlyFiles: true,
gitignore: true,
globalGitignore: true,
absolute: true,
dot: true,
cwd,
});
const effectiveIgnores = [...defaultIgnores, ...discoveryIgnores];
if (reopenedDefaultPatterns.length === 0) {
return discoveredFiles.filter(filePath => !isIgnoredFile(cwd, filePath, effectiveIgnores));
}
const reopenedFiles = await globby(globs, {
ignore: [
...positiveGlobalIgnores,
...defaultIgnores.filter(defaultIgnore => !reopenedDefaultPatterns.some(pattern => defaultIgnoreOverlapsReopenedPattern(defaultIgnore, pattern))),
],
onlyFiles: true,
gitignore: true,
globalGitignore: true,
absolute: true,
dot: true,
cwd,
});
return [...new Set([...discoveredFiles, ...reopenedFiles])]
.filter(filePath => !isIgnoredFile(cwd, filePath, effectiveIgnores));
};
export class Xo {
/**
Static helper to convert an XO config to an ESLint config to be used in `eslint.config.js`.
*/
static xoToEslintConfig = xoToEslintConfig;
/**
Static helper for backwards compatibility and use in editor extensions and other tools.
*/
static async lintText(code: string, options: LintTextOptions & LinterOptions & XoConfigOptions) {
const xo = new Xo(
{
cwd: options.cwd,
fix: options.fix,
filePath: options.filePath,
quiet: options.quiet,
ts: options.ts ?? true,
configPath: options.configPath,
suppressionsLocation: options.suppressionsLocation,
},
{
react: options.react,
space: options.space,
semicolon: options.semicolon,
prettier: options.prettier,
ignores: options.ignores,
},
);
return xo.lintText(code, {
filePath: options.filePath,
warnIgnored: options.warnIgnored,
});
}
/**
Static helper for backwards compatibility and use in editor extensions and other tools.
*/
static async lintFiles(globs: string | undefined, options: LinterOptions & XoConfigOptions) {
const xo = new Xo(
{
cwd: options.cwd,
fix: options.fix,
filePath: options.filePath,
quiet: options.quiet,
ts: options.ts,
configPath: options.configPath,
suppressionsLocation: options.suppressionsLocation,
},
{
react: options.react,
space: options.space,
semicolon: options.semicolon,
prettier: options.prettier,
ignores: options.ignores,
},
);
return xo.lintFiles(globs);
}
/**
Write the fixes to disk.
*/
static async outputFixes(results: XoLintResult) {
await ESLint.outputFixes(results?.results ?? []);
}
/**
Required linter options: `cwd`, `fix`, and `filePath` (in case of `lintText`).
*/
_linterOptions: LinterOptions;
/**
File path to the ESLint cache.
*/
_cacheLocation: string;
/**
XO config derived from both the base config and the resolved flat config.
*/
_xoConfig?: XoConfigItem[];
/**
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.
*/
readonly #baseXoConfig: XoConfigOptions;
/**
A re-usable ESLint instance configured with options calculated from the XO config.
*/
#eslint?: ESLint;
/**
The ESLint config calculated from the resolved XO config.
*/
#eslintConfig?: Linter.Config[];
/**
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.
*/
readonly #tsFilesGlob: string[] = [tsFilesGlob];
/**
We use this to also add negative glob patterns in case a user overrides the parserOptions in their XO config.
*/
readonly #tsFilesIgnoresGlob: string[] = [];
/**
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).
*/
readonly #fileConfigs = new Map<string, XoConfigItem>();
/**
Track virtual/stdin files that share a single tsconfig.stdin.json.
These are handled differently from regular files.
*/
readonly #virtualFiles = new Set<string>();
constructor(_linterOptions: LinterOptions, _baseXoConfig: XoConfigOptions = {}) {
this._linterOptions = _linterOptions;
this.#baseXoConfig = _baseXoConfig;
// Fix relative cwd paths
if (!path.isAbsolute(this._linterOptions.cwd)) {
this._linterOptions.cwd = path.resolve(process.cwd(), this._linterOptions.cwd);
}
try {
this._linterOptions.cwd = syncFs.realpathSync.native(this._linterOptions.cwd);
} catch {
// Ignore invalid paths here; the caller will handle errors later.
}
const backupCacheLocation = path.join(os.tmpdir(), cacheDirName);
this._cacheLocation = findCacheDirectory({name: cacheDirName, cwd: this._linterOptions.cwd}) ?? backupCacheLocation;
}
/**
Sets the XO config on the XO instance.
*/
async setXoConfig() {
if (this._xoConfig) {
return;
}
const {flatOptions, flatConfigPath} = await resolveXoConfig({
...this._linterOptions,
});
const {config: xoConfig, tsFilesGlob: tsGlob, tsFilesIgnoresGlob} = preProcessXoConfig([
this.#baseXoConfig,
...flatOptions,
]);
this._xoConfig = xoConfig;
this.#tsFilesGlob.push(...tsGlob);
this.#tsFilesIgnoresGlob.push(...tsFilesIgnoresGlob);
this.#prettierConfig = await prettier.resolveConfig(flatConfigPath, {editorconfig: true}) ?? {};
}
setEslintConfig(cliIgnores: string[] = arrify(this.#baseXoConfig.ignores), stripDefaultIgnores = false) {
if (!this._xoConfig) {
throw new Error('"Xo.setEslintConfig" failed');
}
// Combine base config with per-file configs from Map
// Deduplicate configs since multiple files can share the same config object
const [baseConfig = {}, ...resolvedConfigs] = this._xoConfig;
const configWithoutCliIgnores = Object.fromEntries(Object.entries(baseConfig).filter(([key]) => key !== 'ignores'));
const expandedResolvedConfigs = resolvedConfigs.map(config => expandGlobalIgnoreConfigForEslint(config));
const uniqueFileConfigs = [...new Set(this.#fileConfigs.values())];
const cliIgnoreConfig = cliIgnores.length > 0 ? [{ignores: expandIgnoreNegationsForEslint(cliIgnores)}] : [];
const allConfigs = [configWithoutCliIgnores, ...expandedResolvedConfigs, ...cliIgnoreConfig, ...uniqueFileConfigs];
// Always regenerate to support instance reuse with new files
this.#eslintConfig = xoToEslintConfig(allConfigs, {prettierOptions: this.#prettierConfig});
if (stripDefaultIgnores) {
this.#eslintConfig = stripDefaultIgnoreConfigs(this.#eslintConfig);
}
}
/**
Ensures the cache directory exists. This needs to run once before both tsconfig handling and running ESLint occur.
*/
async ensureCacheDirectory() {
try {
const cacheStats = await fs.stat(this._cacheLocation);
// If file, re-create as directory
if (cacheStats.isFile()) {
await fs.rm(this._cacheLocation, {recursive: true, force: true});
await fs.mkdir(this._cacheLocation, {recursive: true});
}
} catch {
// If not exists, create the directory
await fs.mkdir(this._cacheLocation, {recursive: true});
}
}
/**
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.
*/
async handleUnincludedTsFiles(files?: string[]): Promise<void> {
if (!this._linterOptions.ts || !files || files.length === 0) {
return;
}
// Get ALL TypeScript files being linted (both new and previously handled)
const allTsFiles = matchFilesForTsConfig(this._linterOptions.cwd, files, this.#tsFilesGlob, this.#tsFilesIgnoresGlob);
if (allTsFiles.length === 0) {
this.#fileConfigs.clear();
if (this.#virtualFiles.size > 0) {
await this.addVirtualFilesToConfig([]);
}
return;
}
const {program, existingFiles, virtualFiles} = handleTsconfig({
files: allTsFiles,
cwd: this._linterOptions.cwd,
cacheLocation: this._cacheLocation,
});
this.#fileConfigs.clear();
if (existingFiles.length > 0) {
this.addExistingFilesToConfig(existingFiles, program);
}
await this.addVirtualFilesToConfig(virtualFiles);
}
/**
Initializes the ESLint instance on the XO instance.
*/
public async initEslint(files?: string[], cliIgnores: string[] = arrify(this.#baseXoConfig.ignores), stripDefaultIgnores = false) {
await this.setXoConfig();
await this.ensureCacheDirectory();
await this.handleUnincludedTsFiles(files);
this.setEslintConfig(cliIgnores, stripDefaultIgnores);
if (!this._xoConfig) {
throw new Error('"Xo.initEslint" failed');
}
const eslintOptions: ESLint.Options = {
cwd: this._linterOptions.cwd,
overrideConfig: this.#eslintConfig,
overrideConfigFile: true,
globInputPaths: false,
warnIgnored: false,
cache: true,
cacheLocation: this._cacheLocation,
cacheStrategy: 'content',
fix: this._linterOptions.fix,
applySuppressions: true,
suppressionsLocation: this._linterOptions.suppressionsLocation,
};
// Always create new instance to support reuse with updated config
// ESLint's file-based cache (cacheLocation) persists across instances
this.#eslint = new ESLint(eslintOptions);
}
/**
Lints the files on the XO instance.
@param globs - Glob pattern to pass to `globby`.
@throws Error
*/
async lintFiles(globs?: string | string[]): Promise<XoLintResult> {
if (!globs || (Array.isArray(globs) && globs.length === 0)) {
globs = `**/*.{${allExtensions.join(',')}}`;
}
globs = arrify(globs);
// If any explicitly provided pattern is non-dynamic (a literal file path), throw when no files are found.
// Dynamic glob patterns matching nothing is acceptable — the project may simply have no matching files yet.
// The default glob substitution above is always dynamic, so this is false when no globs were provided.
const hasExplicitFilePaths = globs.some(glob => !isDynamicPattern(glob));
await this.setXoConfig();
const cliIgnores = arrify(this.#baseXoConfig.ignores);
const configIgnores = (this._xoConfig ?? []).slice(1)
.filter(config => isGlobalIgnoreConfig(config))
.flatMap(config => arrify(config.ignores));
const globalIgnores = [...configIgnores, ...cliIgnores];
const positiveGlobalIgnores = globalIgnores.filter(pattern => !pattern.startsWith('!'));
const reopenedDefaultPatterns = getReopenedDefaultPatterns(globalIgnores);
const discoveryIgnores = [...configIgnores, ...cliIgnores];
const files = await discoverLintFiles({
cwd: this._linterOptions.cwd,
globs,
positiveGlobalIgnores,
discoveryIgnores,
reopenedDefaultPatterns,
});
if (this._linterOptions.suppressionsLocation) {
const suppressionsFilePath = path.resolve(this._linterOptions.cwd, this._linterOptions.suppressionsLocation);
if (!syncFs.existsSync(suppressionsFilePath)) {
throw createErrorWithExitCode(suppressionsFileMissingErrorMessage, 2);
}
}
await this.initEslint(files, cliIgnores, true);
if (!this.#eslint) {
throw new Error('Failed to initialize ESLint');
}
const eslint = this.#eslint;
const ignoredResults = await getIgnoredExplicitFileResults(this._linterOptions.cwd, globs, eslint, [...defaultIgnores, ...discoveryIgnores]);
if (files.length === 0) {
if (hasExplicitFilePaths && ignoredResults.length === 0) {
throw new Error(noFilesFoundErrorMessage);
}
return this.processReport(ignoredResults);
}
const results = await eslint.lintFiles(files);
const rulesMeta = eslint.getRulesMetaForResults(results);
// No overlap: `warnIgnored: false` makes ESLint silently drop ignored files from `results`.
return this.processReport([...results, ...ignoredResults], {rulesMeta});
}
/**
Lints the text on the XO instance.
*/
async lintText(
code: string,
lintTextOptions: LintTextOptions,
): Promise<XoLintResult> {
const {filePath, warnIgnored} = lintTextOptions;
if (this._linterOptions.suppressionsLocation) {
const suppressionsFilePath = path.resolve(this._linterOptions.cwd, this._linterOptions.suppressionsLocation);
if (!syncFs.existsSync(suppressionsFilePath)) {
throw createErrorWithExitCode(suppressionsFileMissingErrorMessage, 2);
}
}
await this.initEslint([filePath]);
if (!this.#eslint) {
throw new Error('Failed to initialize ESLint');
}
const results = await this.#eslint?.lintText(code, {
filePath,
warnIgnored,
});
const rulesMeta = this.#eslint.getRulesMetaForResults(results ?? []);
return this.processReport(results ?? [], {rulesMeta});
}
async calculateConfigForFile(filePath: string): Promise<Linter.Config> {
await this.initEslint([filePath]);
if (!this.#eslint) {
throw new Error('Failed to initialize ESLint');
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
return this.#eslint.calculateConfigForFile(filePath) as Promise<Linter.Config>;
}
async getFormatter(name: string) {
await this.initEslint();
if (!this.#eslint) {
throw new Error('Failed to initialize ESLint');
}
return this.#eslint.loadFormatter(name);
}
/**
Add virtual files to the config with a tsconfig approach.
*/
private async addVirtualFilesToConfig(files: string[]): Promise<void> {
if (!this._xoConfig) {
return;
}
try {
const nextVirtualFiles = new Set(files);
const tsconfigPath = path.join(this._cacheLocation, 'tsconfig.stdin.json');
const configIndex = this._xoConfig.findIndex(configItem => {
const {languageOptions} = configItem;
const parserOptionsCandidate = languageOptions?.['parserOptions'];
return isObjectRecord(parserOptionsCandidate) && parserOptionsCandidate['project'] === tsconfigPath;
});
if (nextVirtualFiles.size > 0) {
const filesArray = nextVirtualFiles.values().toArray();
const relativeFiles = filesArray.map(file => path.relative(this._linterOptions.cwd, file));
const tsconfigContent = {
compilerOptions: {
...tsconfigDefaults.compilerOptions,
module: 'ESNext',
moduleResolution: 'NodeNext',
esModuleInterop: true,
skipLibCheck: true,
},
files: filesArray,
};
await fs.writeFile(tsconfigPath, JSON.stringify(tsconfigContent, null, 2));
if (configIndex === -1) {
const parserOptions: TypeScriptParserOptions = {
projectService: false,
project: tsconfigPath,
tsconfigRootDir: this._linterOptions.cwd,
};
this._xoConfig.push({
files: relativeFiles,
languageOptions: {
parser: typescriptParser,
parserOptions,
},
});
} else {
const existingConfig = this._xoConfig[configIndex];
this._xoConfig[configIndex] = {
...existingConfig,
files: relativeFiles,
};
}
this.#virtualFiles.clear();
for (const file of nextVirtualFiles) {
this.#virtualFiles.add(file);
}
return;
}
if (configIndex >= 0) {
this._xoConfig.splice(configIndex, 1);
}
this.#virtualFiles.clear();
await fs.rm(tsconfigPath, {force: true});
} catch (error) {
console.warn('XO: Failed to create tsconfig for virtual files. Type-aware linting will be disabled for these files.', error instanceof Error ? error.message : String(error));
}
}
/**
Add existing files to the config with an in-memory TypeScript Program.
*/
private addExistingFilesToConfig(files: string[], program?: ts.Program): void {
if (!this._xoConfig || files.length === 0) {
return;
}
const parserOptions: TypeScriptParserOptions = {
project: false,
projectService: false,
};
if (program) {
parserOptions.programs = [program];
}
const config: XoConfigItem = {
files: files.map(file => path.relative(this._linterOptions.cwd, file)),
languageOptions: {
parser: typescriptParser,
parserOptions,
},
};
// IMPORTANT: All files intentionally share the same config object reference for memory efficiency.
// This prevents unbounded memory growth in long-running processes (e.g., language servers).
// The config is immutable after creation, so sharing is safe.
// Deduplication happens in setEslintConfig() via Set to avoid duplicate configs in the final array.
for (const file of files) {
this.#fileConfigs.set(file, config);
}
}
private processReport(
report: ESLint.LintResult[],
{rulesMeta = {}} = {},
): XoLintResult {
if (this._linterOptions.quiet) {
report = ESLint.getErrorResults(report);
}
const result = {
results: report,
rulesMeta,
...this.getReportStatistics(report),
};
defineLazyProperty(result, 'usedDeprecatedRules', () => {
const seenRules = new Set();
const rules = [];
for (const {usedDeprecatedRules} of report) {
for (const rule of usedDeprecatedRules) {
if (seenRules.has(rule.ruleId)) {
continue;
}
seenRules.add(rule.ruleId);
rules.push(rule);
}
}
return rules;
});
return result;
}
private getReportStatistics(results: ESLint.LintResult[]) {
const statistics = {
errorCount: 0,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
};
for (const result of results) {
statistics.errorCount += result.errorCount;
statistics.warningCount += result.warningCount;
statistics.fixableErrorCount += result.fixableErrorCount;
statistics.fixableWarningCount += result.fixableWarningCount;
}
return statistics;
}
}
export default Xo;