eslint 9.11.1 → 9.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,703 @@
1
+ /**
2
+ * @fileoverview Utility to load config files
3
+ * @author Nicholas C. Zakas
4
+ */
5
+
6
+ "use strict";
7
+
8
+ //------------------------------------------------------------------------------
9
+ // Requirements
10
+ //------------------------------------------------------------------------------
11
+
12
+ const path = require("node:path");
13
+ const fs = require("node:fs/promises");
14
+ const findUp = require("find-up");
15
+ const { pathToFileURL } = require("node:url");
16
+ const debug = require("debug")("eslint:config-loader");
17
+ const { FlatConfigArray } = require("../config/flat-config-array");
18
+
19
+ //-----------------------------------------------------------------------------
20
+ // Types
21
+ //-----------------------------------------------------------------------------
22
+
23
+ /**
24
+ * @typedef {import("../shared/types").FlatConfigObject} FlatConfigObject
25
+ * @typedef {import("../shared/types").FlatConfigArray} FlatConfigArray
26
+ * @typedef {Object} ConfigLoaderOptions
27
+ * @property {string|false|undefined} configFile The path to the config file to use.
28
+ * @property {string} cwd The current working directory.
29
+ * @property {boolean} ignoreEnabled Indicates if ignore patterns should be honored.
30
+ * @property {FlatConfigArray} [baseConfig] The base config to use.
31
+ * @property {Array<FlatConfigObject>} [defaultConfigs] The default configs to use.
32
+ * @property {Array<string>} [ignorePatterns] The ignore patterns to use.
33
+ * @property {FlatConfigObject|Array<FlatConfigObject>} overrideConfig The override config to use.
34
+ * @property {boolean} allowTS Indicates if TypeScript configuration files are allowed.
35
+ */
36
+
37
+ //------------------------------------------------------------------------------
38
+ // Helpers
39
+ //------------------------------------------------------------------------------
40
+
41
+ const FLAT_CONFIG_FILENAMES = [
42
+ "eslint.config.js",
43
+ "eslint.config.mjs",
44
+ "eslint.config.cjs"
45
+ ];
46
+
47
+ const TS_FLAT_CONFIG_FILENAMES = [
48
+ "eslint.config.ts",
49
+ "eslint.config.mts",
50
+ "eslint.config.cts"
51
+ ];
52
+
53
+ const importedConfigFileModificationTime = new Map();
54
+
55
+ /**
56
+ * Asserts that the given file path is valid.
57
+ * @param {string} filePath The file path to check.
58
+ * @returns {void}
59
+ * @throws {Error} If `filePath` is not a non-empty string.
60
+ */
61
+ function assertValidFilePath(filePath) {
62
+ if (!filePath || typeof filePath !== "string") {
63
+ throw new Error("'filePath' must be a non-empty string");
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Asserts that a configuration exists. A configuration exists if any
69
+ * of the following are true:
70
+ * - `configFilePath` is defined.
71
+ * - `useConfigFile` is `false`.
72
+ * @param {string|undefined} configFilePath The path to the config file.
73
+ * @param {ConfigLoaderOptions} loaderOptions The options to use when loading configuration files.
74
+ * @returns {void}
75
+ * @throws {Error} If no configuration exists.
76
+ */
77
+ function assertConfigurationExists(configFilePath, loaderOptions) {
78
+
79
+ const {
80
+ configFile: useConfigFile
81
+ } = loaderOptions;
82
+
83
+ if (!configFilePath && useConfigFile !== false) {
84
+ const error = new Error("Could not find config file.");
85
+
86
+ error.messageTemplate = "config-file-missing";
87
+ throw error;
88
+ }
89
+
90
+ }
91
+
92
+ /**
93
+ * Check if the file is a TypeScript file.
94
+ * @param {string} filePath The file path to check.
95
+ * @returns {boolean} `true` if the file is a TypeScript file, `false` if it's not.
96
+ */
97
+ function isFileTS(filePath) {
98
+ const fileExtension = path.extname(filePath);
99
+
100
+ return /^\.[mc]?ts$/u.test(fileExtension);
101
+ }
102
+
103
+ /**
104
+ * Check if ESLint is running in Bun.
105
+ * @returns {boolean} `true` if the ESLint is running Bun, `false` if it's not.
106
+ */
107
+ function isRunningInBun() {
108
+ return !!globalThis.Bun;
109
+ }
110
+
111
+ /**
112
+ * Check if ESLint is running in Deno.
113
+ * @returns {boolean} `true` if the ESLint is running in Deno, `false` if it's not.
114
+ */
115
+ function isRunningInDeno() {
116
+ return !!globalThis.Deno;
117
+ }
118
+
119
+ /**
120
+ * Load the config array from the given filename.
121
+ * @param {string} filePath The filename to load from.
122
+ * @param {boolean} allowTS Indicates if TypeScript configuration files are allowed.
123
+ * @returns {Promise<any>} The config loaded from the config file.
124
+ */
125
+ async function loadConfigFile(filePath, allowTS) {
126
+
127
+ debug(`Loading config from ${filePath}`);
128
+
129
+ const fileURL = pathToFileURL(filePath);
130
+
131
+ debug(`Config file URL is ${fileURL}`);
132
+
133
+ const mtime = (await fs.stat(filePath)).mtime.getTime();
134
+
135
+ /*
136
+ * Append a query with the config file's modification time (`mtime`) in order
137
+ * to import the current version of the config file. Without the query, `import()` would
138
+ * cache the config file module by the pathname only, and then always return
139
+ * the same version (the one that was actual when the module was imported for the first time).
140
+ *
141
+ * This ensures that the config file module is loaded and executed again
142
+ * if it has been changed since the last time it was imported.
143
+ * If it hasn't been changed, `import()` will just return the cached version.
144
+ *
145
+ * Note that we should not overuse queries (e.g., by appending the current time
146
+ * to always reload the config file module) as that could cause memory leaks
147
+ * because entries are never removed from the import cache.
148
+ */
149
+ fileURL.searchParams.append("mtime", mtime);
150
+
151
+ /*
152
+ * With queries, we can bypass the import cache. However, when import-ing a CJS module,
153
+ * Node.js uses the require infrastructure under the hood. That includes the require cache,
154
+ * which caches the config file module by its file path (queries have no effect).
155
+ * Therefore, we also need to clear the require cache before importing the config file module.
156
+ * In order to get the same behavior with ESM and CJS config files, in particular - to reload
157
+ * the config file only if it has been changed, we track file modification times and clear
158
+ * the require cache only if the file has been changed.
159
+ */
160
+ if (importedConfigFileModificationTime.get(filePath) !== mtime) {
161
+ delete require.cache[filePath];
162
+ }
163
+
164
+ const isTS = isFileTS(filePath);
165
+ const isBun = isRunningInBun();
166
+ const isDeno = isRunningInDeno();
167
+
168
+ /*
169
+ * If we are dealing with a TypeScript file, then we need to use `jiti` to load it
170
+ * in Node.js. Deno and Bun both allow native importing of TypeScript files.
171
+ *
172
+ * When Node.js supports native TypeScript imports, we can remove this check.
173
+ */
174
+ if (allowTS && isTS && !isDeno && !isBun) {
175
+
176
+ // eslint-disable-next-line no-use-before-define -- `ConfigLoader.loadJiti` can be overwritten for testing
177
+ const { createJiti } = await ConfigLoader.loadJiti().catch(() => {
178
+ throw new Error("The 'jiti' library is required for loading TypeScript configuration files. Make sure to install it.");
179
+ });
180
+
181
+ // `createJiti` was added in jiti v2.
182
+ if (typeof createJiti !== "function") {
183
+ throw new Error("You are using an outdated version of the 'jiti' library. Please update to the latest version of 'jiti' to ensure compatibility and access to the latest features.");
184
+ }
185
+
186
+ /*
187
+ * Disabling `moduleCache` allows us to reload a
188
+ * config file when the last modified timestamp changes.
189
+ */
190
+
191
+ const jiti = createJiti(__filename, { moduleCache: false, interopDefault: false });
192
+ const config = await jiti.import(fileURL.href);
193
+
194
+ importedConfigFileModificationTime.set(filePath, mtime);
195
+
196
+ return config?.default ?? config;
197
+ }
198
+
199
+
200
+ // fallback to normal runtime behavior
201
+
202
+ const config = (await import(fileURL)).default;
203
+
204
+ importedConfigFileModificationTime.set(filePath, mtime);
205
+
206
+ return config;
207
+ }
208
+
209
+ /**
210
+ * Calculates the config array for this run based on inputs.
211
+ * @param {string} configFilePath The absolute path to the config file to use if not overridden.
212
+ * @param {string} basePath The base path to use for relative paths in the config file.
213
+ * @param {ConfigLoaderOptions} options The options to use when loading configuration files.
214
+ * @returns {Promise<FlatConfigArray>} The config array for `eslint`.
215
+ */
216
+ async function calculateConfigArray(configFilePath, basePath, options) {
217
+
218
+ const {
219
+ cwd,
220
+ baseConfig,
221
+ ignoreEnabled,
222
+ ignorePatterns,
223
+ overrideConfig,
224
+ defaultConfigs = [],
225
+ allowTS
226
+ } = options;
227
+
228
+ debug(`Calculating config array from config file ${configFilePath} and base path ${basePath}`);
229
+
230
+ const configs = new FlatConfigArray(baseConfig || [], { basePath, shouldIgnore: ignoreEnabled });
231
+
232
+ // load config file
233
+ if (configFilePath) {
234
+
235
+ debug(`Loading config file ${configFilePath}`);
236
+ const fileConfig = await loadConfigFile(configFilePath, allowTS);
237
+
238
+ if (Array.isArray(fileConfig)) {
239
+ configs.push(...fileConfig);
240
+ } else {
241
+ configs.push(fileConfig);
242
+ }
243
+ }
244
+
245
+ // add in any configured defaults
246
+ configs.push(...defaultConfigs);
247
+
248
+ // append command line ignore patterns
249
+ if (ignorePatterns && ignorePatterns.length > 0) {
250
+
251
+ let relativeIgnorePatterns;
252
+
253
+ /*
254
+ * If the config file basePath is different than the cwd, then
255
+ * the ignore patterns won't work correctly. Here, we adjust the
256
+ * ignore pattern to include the correct relative path. Patterns
257
+ * passed as `ignorePatterns` are relative to the cwd, whereas
258
+ * the config file basePath can be an ancestor of the cwd.
259
+ */
260
+ if (basePath === cwd) {
261
+ relativeIgnorePatterns = ignorePatterns;
262
+ } else {
263
+
264
+ // relative path must only have Unix-style separators
265
+ const relativeIgnorePath = path.relative(basePath, cwd).replace(/\\/gu, "/");
266
+
267
+ relativeIgnorePatterns = ignorePatterns.map(pattern => {
268
+ const negated = pattern.startsWith("!");
269
+ const basePattern = negated ? pattern.slice(1) : pattern;
270
+
271
+ return (negated ? "!" : "") +
272
+ path.posix.join(relativeIgnorePath, basePattern);
273
+ });
274
+ }
275
+
276
+ /*
277
+ * Ignore patterns are added to the end of the config array
278
+ * so they can override default ignores.
279
+ */
280
+ configs.push({
281
+ ignores: relativeIgnorePatterns
282
+ });
283
+ }
284
+
285
+ if (overrideConfig) {
286
+ if (Array.isArray(overrideConfig)) {
287
+ configs.push(...overrideConfig);
288
+ } else {
289
+ configs.push(overrideConfig);
290
+ }
291
+ }
292
+
293
+ await configs.normalize();
294
+
295
+ return configs;
296
+ }
297
+
298
+ //-----------------------------------------------------------------------------
299
+ // Exports
300
+ //-----------------------------------------------------------------------------
301
+
302
+ /**
303
+ * Encapsulates the loading and caching of configuration files when looking up
304
+ * from the file being linted.
305
+ */
306
+ class ConfigLoader {
307
+
308
+ /**
309
+ * Map of config file paths to the config arrays for those directories.
310
+ * @type {Map<string, FlatConfigArray>}
311
+ */
312
+ #configArrays = new Map();
313
+
314
+ /**
315
+ * Map of absolute directory names to the config file paths for those directories.
316
+ * @type {Map<string, {configFilePath:string,basePath:string}>}
317
+ */
318
+ #configFilePaths = new Map();
319
+
320
+ /**
321
+ * The options to use when loading configuration files.
322
+ * @type {ConfigLoaderOptions}
323
+ */
324
+ #options;
325
+
326
+ /**
327
+ * Creates a new instance.
328
+ * @param {ConfigLoaderOptions} options The options to use when loading configuration files.
329
+ */
330
+ constructor(options) {
331
+ this.#options = options;
332
+ }
333
+
334
+ /**
335
+ * Determines which config file to use. This is determined by seeing if an
336
+ * override config file was specified, and if so, using it; otherwise, as long
337
+ * as override config file is not explicitly set to `false`, it will search
338
+ * upwards from `fromDirectory` for a file named `eslint.config.js`.
339
+ * @param {string} fromDirectory The directory from which to start searching.
340
+ * @returns {Promise<{configFilePath:string|undefined,basePath:string}>} Location information for
341
+ * the config file.
342
+ */
343
+ async #locateConfigFileToUse(fromDirectory) {
344
+
345
+ // check cache first
346
+ if (this.#configFilePaths.has(fromDirectory)) {
347
+ return this.#configFilePaths.get(fromDirectory);
348
+ }
349
+
350
+ const configFilenames = this.#options.allowTS
351
+ ? [...FLAT_CONFIG_FILENAMES, ...TS_FLAT_CONFIG_FILENAMES]
352
+ : FLAT_CONFIG_FILENAMES;
353
+
354
+ // determine where to load config file from
355
+ let configFilePath;
356
+ const {
357
+ cwd,
358
+ configFile: useConfigFile
359
+ } = this.#options;
360
+ let basePath = cwd;
361
+
362
+ if (typeof useConfigFile === "string") {
363
+ debug(`Override config file path is ${useConfigFile}`);
364
+ configFilePath = path.resolve(cwd, useConfigFile);
365
+ basePath = cwd;
366
+ } else if (useConfigFile !== false) {
367
+ debug("Searching for eslint.config.js");
368
+ configFilePath = await findUp(
369
+ configFilenames,
370
+ { cwd: fromDirectory }
371
+ );
372
+
373
+ if (configFilePath) {
374
+ basePath = path.dirname(configFilePath);
375
+ }
376
+
377
+ }
378
+
379
+ // cache the result
380
+ this.#configFilePaths.set(fromDirectory, { configFilePath, basePath });
381
+
382
+ return {
383
+ configFilePath,
384
+ basePath
385
+ };
386
+
387
+ }
388
+
389
+ /**
390
+ * Calculates the config array for this run based on inputs.
391
+ * @param {string} configFilePath The absolute path to the config file to use if not overridden.
392
+ * @param {string} basePath The base path to use for relative paths in the config file.
393
+ * @returns {Promise<FlatConfigArray>} The config array for `eslint`.
394
+ */
395
+ async #calculateConfigArray(configFilePath, basePath) {
396
+
397
+ // check for cached version first
398
+ if (this.#configArrays.has(configFilePath)) {
399
+ return this.#configArrays.get(configFilePath);
400
+ }
401
+
402
+ const configs = await calculateConfigArray(configFilePath, basePath, this.#options);
403
+
404
+ // cache the config array for this instance
405
+ this.#configArrays.set(configFilePath, configs);
406
+
407
+ return configs;
408
+ }
409
+
410
+ /**
411
+ * Returns the config file path for the given directory or file. This will either use
412
+ * the override config file that was specified in the constructor options or
413
+ * search for a config file from the directory.
414
+ * @param {string} fileOrDirPath The file or directory path to get the config file path for.
415
+ * @returns {Promise<string|undefined>} The config file path or `undefined` if not found.
416
+ * @throws {Error} If `fileOrDirPath` is not a non-empty string.
417
+ * @throws {Error} If `fileOrDirPath` is not an absolute path.
418
+ */
419
+ async findConfigFileForPath(fileOrDirPath) {
420
+
421
+ assertValidFilePath(fileOrDirPath);
422
+
423
+ const absoluteDirPath = path.resolve(this.#options.cwd, path.dirname(fileOrDirPath));
424
+ const { configFilePath } = await this.#locateConfigFileToUse(absoluteDirPath);
425
+
426
+ return configFilePath;
427
+ }
428
+
429
+ /**
430
+ * Returns a configuration object for the given file based on the CLI options.
431
+ * This is the same logic used by the ESLint CLI executable to determine
432
+ * configuration for each file it processes.
433
+ * @param {string} filePath The path of the file or directory to retrieve config for.
434
+ * @returns {Promise<ConfigData|undefined>} A configuration object for the file
435
+ * or `undefined` if there is no configuration data for the file.
436
+ * @throws {Error} If no configuration for `filePath` exists.
437
+ */
438
+ async loadConfigArrayForFile(filePath) {
439
+
440
+ assertValidFilePath(filePath);
441
+
442
+ debug(`Calculating config for file ${filePath}`);
443
+
444
+ const configFilePath = await this.findConfigFileForPath(filePath);
445
+
446
+ assertConfigurationExists(configFilePath, this.#options);
447
+
448
+ return this.loadConfigArrayForDirectory(filePath);
449
+ }
450
+
451
+ /**
452
+ * Returns a configuration object for the given directory based on the CLI options.
453
+ * This is the same logic used by the ESLint CLI executable to determine
454
+ * configuration for each file it processes.
455
+ * @param {string} dirPath The path of the directory to retrieve config for.
456
+ * @returns {Promise<ConfigData|undefined>} A configuration object for the directory
457
+ * or `undefined` if there is no configuration data for the directory.
458
+ */
459
+ async loadConfigArrayForDirectory(dirPath) {
460
+
461
+ assertValidFilePath(dirPath);
462
+
463
+ debug(`Calculating config for directory ${dirPath}`);
464
+
465
+ const absoluteDirPath = path.resolve(this.#options.cwd, path.dirname(dirPath));
466
+ const { configFilePath, basePath } = await this.#locateConfigFileToUse(absoluteDirPath);
467
+
468
+ debug(`Using config file ${configFilePath} and base path ${basePath}`);
469
+ return this.#calculateConfigArray(configFilePath, basePath);
470
+ }
471
+
472
+ /**
473
+ * Returns a configuration array for the given file based on the CLI options.
474
+ * This is a synchronous operation and does not read any files from disk. It's
475
+ * intended to be used in locations where we know the config file has already
476
+ * been loaded and we just need to get the configuration for a file.
477
+ * @param {string} filePath The path of the file to retrieve a config object for.
478
+ * @returns {ConfigData|undefined} A configuration object for the file
479
+ * or `undefined` if there is no configuration data for the file.
480
+ * @throws {Error} If `filePath` is not a non-empty string.
481
+ * @throws {Error} If `filePath` is not an absolute path.
482
+ * @throws {Error} If the config file was not already loaded.
483
+ */
484
+ getCachedConfigArrayForFile(filePath) {
485
+ assertValidFilePath(filePath);
486
+
487
+ debug(`Looking up cached config for ${filePath}`);
488
+
489
+ return this.getCachedConfigArrayForPath(path.dirname(filePath));
490
+ }
491
+
492
+ /**
493
+ * Returns a configuration array for the given directory based on the CLI options.
494
+ * This is a synchronous operation and does not read any files from disk. It's
495
+ * intended to be used in locations where we know the config file has already
496
+ * been loaded and we just need to get the configuration for a file.
497
+ * @param {string} fileOrDirPath The path of the directory to retrieve a config object for.
498
+ * @returns {ConfigData|undefined} A configuration object for the directory
499
+ * or `undefined` if there is no configuration data for the directory.
500
+ * @throws {Error} If `dirPath` is not a non-empty string.
501
+ * @throws {Error} If `dirPath` is not an absolute path.
502
+ * @throws {Error} If the config file was not already loaded.
503
+ */
504
+ getCachedConfigArrayForPath(fileOrDirPath) {
505
+ assertValidFilePath(fileOrDirPath);
506
+
507
+ debug(`Looking up cached config for ${fileOrDirPath}`);
508
+
509
+ const absoluteDirPath = path.resolve(this.#options.cwd, fileOrDirPath);
510
+
511
+ if (!this.#configFilePaths.has(absoluteDirPath)) {
512
+ throw new Error(`Could not find config file for ${fileOrDirPath}`);
513
+ }
514
+
515
+ const { configFilePath } = this.#configFilePaths.get(absoluteDirPath);
516
+
517
+ return this.#configArrays.get(configFilePath);
518
+ }
519
+
520
+ /**
521
+ * Used to import the jiti dependency. This method is exposed internally for testing purposes.
522
+ * @returns {Promise<Record<string, unknown>>} A promise that fulfills with a module object
523
+ * or rejects with an error if jiti is not found.
524
+ */
525
+ static loadJiti() {
526
+ return import("jiti");
527
+ }
528
+
529
+ }
530
+
531
+ /**
532
+ * Encapsulates the loading and caching of configuration files when looking up
533
+ * from the current working directory.
534
+ */
535
+ class LegacyConfigLoader extends ConfigLoader {
536
+
537
+ /**
538
+ * The options to use when loading configuration files.
539
+ * @type {ConfigLoaderOptions}
540
+ */
541
+ #options;
542
+
543
+ /**
544
+ * The cached config file path for this instance.
545
+ * @type {{configFilePath:string,basePath:string}|undefined}
546
+ */
547
+ #configFilePath;
548
+
549
+ /**
550
+ * The cached config array for this instance.
551
+ * @type {FlatConfigArray}
552
+ */
553
+ #configArray;
554
+
555
+ /**
556
+ * Creates a new instance.
557
+ * @param {ConfigLoaderOptions} options The options to use when loading configuration files.
558
+ */
559
+ constructor(options) {
560
+ super(options);
561
+ this.#options = options;
562
+ }
563
+
564
+ /**
565
+ * Determines which config file to use. This is determined by seeing if an
566
+ * override config file was specified, and if so, using it; otherwise, as long
567
+ * as override config file is not explicitly set to `false`, it will search
568
+ * upwards from the cwd for a file named `eslint.config.js`.
569
+ * @returns {Promise<{configFilePath:string|undefined,basePath:string}>} Location information for
570
+ * the config file.
571
+ */
572
+ async #locateConfigFileToUse() {
573
+
574
+ // check cache first
575
+ if (this.#configFilePath) {
576
+ return this.#configFilePath;
577
+ }
578
+
579
+ const configFilenames = this.#options.allowTS
580
+ ? [...FLAT_CONFIG_FILENAMES, ...TS_FLAT_CONFIG_FILENAMES]
581
+ : FLAT_CONFIG_FILENAMES;
582
+
583
+ // determine where to load config file from
584
+ let configFilePath;
585
+ const {
586
+ cwd,
587
+ configFile: useConfigFile
588
+ } = this.#options;
589
+ let basePath = cwd;
590
+
591
+ if (typeof useConfigFile === "string") {
592
+ debug(`[Legacy]: Override config file path is ${useConfigFile}`);
593
+ configFilePath = path.resolve(cwd, useConfigFile);
594
+ basePath = cwd;
595
+ } else if (useConfigFile !== false) {
596
+ debug("[Legacy]: Searching for eslint.config.js");
597
+ configFilePath = await findUp(
598
+ configFilenames,
599
+ { cwd }
600
+ );
601
+
602
+ if (configFilePath) {
603
+ basePath = path.dirname(configFilePath);
604
+ }
605
+
606
+ }
607
+
608
+ // cache the result
609
+ this.#configFilePath = { configFilePath, basePath };
610
+
611
+ return {
612
+ configFilePath,
613
+ basePath
614
+ };
615
+
616
+ }
617
+
618
+ /**
619
+ * Calculates the config array for this run based on inputs.
620
+ * @param {string} configFilePath The absolute path to the config file to use if not overridden.
621
+ * @param {string} basePath The base path to use for relative paths in the config file.
622
+ * @returns {Promise<FlatConfigArray>} The config array for `eslint`.
623
+ */
624
+ async #calculateConfigArray(configFilePath, basePath) {
625
+
626
+ // check for cached version first
627
+ if (this.#configArray) {
628
+ return this.#configArray;
629
+ }
630
+
631
+ const configs = await calculateConfigArray(configFilePath, basePath, this.#options);
632
+
633
+ // cache the config array for this instance
634
+ this.#configArray = configs;
635
+
636
+ return configs;
637
+ }
638
+
639
+
640
+ /**
641
+ * Returns the config file path for the given directory. This will either use
642
+ * the override config file that was specified in the constructor options or
643
+ * search for a config file from the directory of the file being linted.
644
+ * @param {string} dirPath The directory path to get the config file path for.
645
+ * @returns {Promise<string|undefined>} The config file path or `undefined` if not found.
646
+ * @throws {Error} If `fileOrDirPath` is not a non-empty string.
647
+ * @throws {Error} If `fileOrDirPath` is not an absolute path.
648
+ */
649
+ async findConfigFileForPath(dirPath) {
650
+
651
+ assertValidFilePath(dirPath);
652
+
653
+ const { configFilePath } = await this.#locateConfigFileToUse();
654
+
655
+ return configFilePath;
656
+ }
657
+
658
+ /**
659
+ * Returns a configuration object for the given file based on the CLI options.
660
+ * This is the same logic used by the ESLint CLI executable to determine
661
+ * configuration for each file it processes.
662
+ * @param {string} dirPath The path of the directory to retrieve config for.
663
+ * @returns {Promise<ConfigData|undefined>} A configuration object for the file
664
+ * or `undefined` if there is no configuration data for the file.
665
+ */
666
+ async loadConfigArrayForDirectory(dirPath) {
667
+
668
+ assertValidFilePath(dirPath);
669
+
670
+ debug(`[Legacy]: Calculating config for ${dirPath}`);
671
+
672
+ const { configFilePath, basePath } = await this.#locateConfigFileToUse();
673
+
674
+ debug(`[Legacy]: Using config file ${configFilePath} and base path ${basePath}`);
675
+ return this.#calculateConfigArray(configFilePath, basePath);
676
+ }
677
+
678
+ /**
679
+ * Returns a configuration array for the given directory based on the CLI options.
680
+ * This is a synchronous operation and does not read any files from disk. It's
681
+ * intended to be used in locations where we know the config file has already
682
+ * been loaded and we just need to get the configuration for a file.
683
+ * @param {string} dirPath The path of the directory to retrieve a config object for.
684
+ * @returns {ConfigData|undefined} A configuration object for the file
685
+ * or `undefined` if there is no configuration data for the file.
686
+ * @throws {Error} If `dirPath` is not a non-empty string.
687
+ * @throws {Error} If `dirPath` is not an absolute path.
688
+ * @throws {Error} If the config file was not already loaded.
689
+ */
690
+ getCachedConfigArrayForPath(dirPath) {
691
+ assertValidFilePath(dirPath);
692
+
693
+ debug(`[Legacy]: Looking up cached config for ${dirPath}`);
694
+
695
+ if (!this.#configArray) {
696
+ throw new Error(`Could not find config file for ${dirPath}`);
697
+ }
698
+
699
+ return this.#configArray;
700
+ }
701
+ }
702
+
703
+ module.exports = { ConfigLoader, LegacyConfigLoader };