eslint 9.11.1 → 9.12.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,694 @@
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
+ const createJiti = await import("jiti").then(jitiModule => (typeof jitiModule?.createJiti === "function" ? jitiModule.createJiti : jitiModule.default), () => {
177
+ throw new Error("The 'jiti' library is required for loading TypeScript configuration files. Make sure to install it.");
178
+ });
179
+
180
+ /*
181
+ * Disabling `moduleCache` allows us to reload a
182
+ * config file when the last modified timestamp changes.
183
+ */
184
+
185
+ const jiti = createJiti(__filename, { moduleCache: false, interopDefault: false });
186
+
187
+ if (typeof jiti?.import !== "function") {
188
+ 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.");
189
+ }
190
+
191
+ const config = await jiti.import(fileURL.href);
192
+
193
+ importedConfigFileModificationTime.set(filePath, mtime);
194
+
195
+ return config?.default ?? config;
196
+ }
197
+
198
+
199
+ // fallback to normal runtime behavior
200
+
201
+ const config = (await import(fileURL)).default;
202
+
203
+ importedConfigFileModificationTime.set(filePath, mtime);
204
+
205
+ return config;
206
+ }
207
+
208
+ /**
209
+ * Calculates the config array for this run based on inputs.
210
+ * @param {string} configFilePath The absolute path to the config file to use if not overridden.
211
+ * @param {string} basePath The base path to use for relative paths in the config file.
212
+ * @param {ConfigLoaderOptions} options The options to use when loading configuration files.
213
+ * @returns {Promise<FlatConfigArray>} The config array for `eslint`.
214
+ */
215
+ async function calculateConfigArray(configFilePath, basePath, options) {
216
+
217
+ const {
218
+ cwd,
219
+ baseConfig,
220
+ ignoreEnabled,
221
+ ignorePatterns,
222
+ overrideConfig,
223
+ defaultConfigs = [],
224
+ allowTS
225
+ } = options;
226
+
227
+ debug(`Calculating config array from config file ${configFilePath} and base path ${basePath}`);
228
+
229
+ const configs = new FlatConfigArray(baseConfig || [], { basePath, shouldIgnore: ignoreEnabled });
230
+
231
+ // load config file
232
+ if (configFilePath) {
233
+
234
+ debug(`Loading config file ${configFilePath}`);
235
+ const fileConfig = await loadConfigFile(configFilePath, allowTS);
236
+
237
+ if (Array.isArray(fileConfig)) {
238
+ configs.push(...fileConfig);
239
+ } else {
240
+ configs.push(fileConfig);
241
+ }
242
+ }
243
+
244
+ // add in any configured defaults
245
+ configs.push(...defaultConfigs);
246
+
247
+ // append command line ignore patterns
248
+ if (ignorePatterns && ignorePatterns.length > 0) {
249
+
250
+ let relativeIgnorePatterns;
251
+
252
+ /*
253
+ * If the config file basePath is different than the cwd, then
254
+ * the ignore patterns won't work correctly. Here, we adjust the
255
+ * ignore pattern to include the correct relative path. Patterns
256
+ * passed as `ignorePatterns` are relative to the cwd, whereas
257
+ * the config file basePath can be an ancestor of the cwd.
258
+ */
259
+ if (basePath === cwd) {
260
+ relativeIgnorePatterns = ignorePatterns;
261
+ } else {
262
+
263
+ // relative path must only have Unix-style separators
264
+ const relativeIgnorePath = path.relative(basePath, cwd).replace(/\\/gu, "/");
265
+
266
+ relativeIgnorePatterns = ignorePatterns.map(pattern => {
267
+ const negated = pattern.startsWith("!");
268
+ const basePattern = negated ? pattern.slice(1) : pattern;
269
+
270
+ return (negated ? "!" : "") +
271
+ path.posix.join(relativeIgnorePath, basePattern);
272
+ });
273
+ }
274
+
275
+ /*
276
+ * Ignore patterns are added to the end of the config array
277
+ * so they can override default ignores.
278
+ */
279
+ configs.push({
280
+ ignores: relativeIgnorePatterns
281
+ });
282
+ }
283
+
284
+ if (overrideConfig) {
285
+ if (Array.isArray(overrideConfig)) {
286
+ configs.push(...overrideConfig);
287
+ } else {
288
+ configs.push(overrideConfig);
289
+ }
290
+ }
291
+
292
+ await configs.normalize();
293
+
294
+ return configs;
295
+ }
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
+
522
+ /**
523
+ * Encapsulates the loading and caching of configuration files when looking up
524
+ * from the current working directory.
525
+ */
526
+ class LegacyConfigLoader extends ConfigLoader {
527
+
528
+ /**
529
+ * The options to use when loading configuration files.
530
+ * @type {ConfigLoaderOptions}
531
+ */
532
+ #options;
533
+
534
+ /**
535
+ * The cached config file path for this instance.
536
+ * @type {{configFilePath:string,basePath:string}|undefined}
537
+ */
538
+ #configFilePath;
539
+
540
+ /**
541
+ * The cached config array for this instance.
542
+ * @type {FlatConfigArray}
543
+ */
544
+ #configArray;
545
+
546
+ /**
547
+ * Creates a new instance.
548
+ * @param {ConfigLoaderOptions} options The options to use when loading configuration files.
549
+ */
550
+ constructor(options) {
551
+ super(options);
552
+ this.#options = options;
553
+ }
554
+
555
+ /**
556
+ * Determines which config file to use. This is determined by seeing if an
557
+ * override config file was specified, and if so, using it; otherwise, as long
558
+ * as override config file is not explicitly set to `false`, it will search
559
+ * upwards from the cwd for a file named `eslint.config.js`.
560
+ * @returns {Promise<{configFilePath:string|undefined,basePath:string}>} Location information for
561
+ * the config file.
562
+ */
563
+ async #locateConfigFileToUse() {
564
+
565
+ // check cache first
566
+ if (this.#configFilePath) {
567
+ return this.#configFilePath;
568
+ }
569
+
570
+ const configFilenames = this.#options.allowTS
571
+ ? [...FLAT_CONFIG_FILENAMES, ...TS_FLAT_CONFIG_FILENAMES]
572
+ : FLAT_CONFIG_FILENAMES;
573
+
574
+ // determine where to load config file from
575
+ let configFilePath;
576
+ const {
577
+ cwd,
578
+ configFile: useConfigFile
579
+ } = this.#options;
580
+ let basePath = cwd;
581
+
582
+ if (typeof useConfigFile === "string") {
583
+ debug(`[Legacy]: Override config file path is ${useConfigFile}`);
584
+ configFilePath = path.resolve(cwd, useConfigFile);
585
+ basePath = cwd;
586
+ } else if (useConfigFile !== false) {
587
+ debug("[Legacy]: Searching for eslint.config.js");
588
+ configFilePath = await findUp(
589
+ configFilenames,
590
+ { cwd }
591
+ );
592
+
593
+ if (configFilePath) {
594
+ basePath = path.dirname(configFilePath);
595
+ }
596
+
597
+ }
598
+
599
+ // cache the result
600
+ this.#configFilePath = { configFilePath, basePath };
601
+
602
+ return {
603
+ configFilePath,
604
+ basePath
605
+ };
606
+
607
+ }
608
+
609
+ /**
610
+ * Calculates the config array for this run based on inputs.
611
+ * @param {string} configFilePath The absolute path to the config file to use if not overridden.
612
+ * @param {string} basePath The base path to use for relative paths in the config file.
613
+ * @returns {Promise<FlatConfigArray>} The config array for `eslint`.
614
+ */
615
+ async #calculateConfigArray(configFilePath, basePath) {
616
+
617
+ // check for cached version first
618
+ if (this.#configArray) {
619
+ return this.#configArray;
620
+ }
621
+
622
+ const configs = await calculateConfigArray(configFilePath, basePath, this.#options);
623
+
624
+ // cache the config array for this instance
625
+ this.#configArray = configs;
626
+
627
+ return configs;
628
+ }
629
+
630
+
631
+ /**
632
+ * Returns the config file path for the given directory. This will either use
633
+ * the override config file that was specified in the constructor options or
634
+ * search for a config file from the directory of the file being linted.
635
+ * @param {string} dirPath The directory path to get the config file path for.
636
+ * @returns {Promise<string|undefined>} The config file path or `undefined` if not found.
637
+ * @throws {Error} If `fileOrDirPath` is not a non-empty string.
638
+ * @throws {Error} If `fileOrDirPath` is not an absolute path.
639
+ */
640
+ async findConfigFileForPath(dirPath) {
641
+
642
+ assertValidFilePath(dirPath);
643
+
644
+ const { configFilePath } = await this.#locateConfigFileToUse();
645
+
646
+ return configFilePath;
647
+ }
648
+
649
+ /**
650
+ * Returns a configuration object for the given file based on the CLI options.
651
+ * This is the same logic used by the ESLint CLI executable to determine
652
+ * configuration for each file it processes.
653
+ * @param {string} dirPath The path of the directory to retrieve config for.
654
+ * @returns {Promise<ConfigData|undefined>} A configuration object for the file
655
+ * or `undefined` if there is no configuration data for the file.
656
+ */
657
+ async loadConfigArrayForDirectory(dirPath) {
658
+
659
+ assertValidFilePath(dirPath);
660
+
661
+ debug(`[Legacy]: Calculating config for ${dirPath}`);
662
+
663
+ const { configFilePath, basePath } = await this.#locateConfigFileToUse();
664
+
665
+ debug(`[Legacy]: Using config file ${configFilePath} and base path ${basePath}`);
666
+ return this.#calculateConfigArray(configFilePath, basePath);
667
+ }
668
+
669
+ /**
670
+ * Returns a configuration array for the given directory based on the CLI options.
671
+ * This is a synchronous operation and does not read any files from disk. It's
672
+ * intended to be used in locations where we know the config file has already
673
+ * been loaded and we just need to get the configuration for a file.
674
+ * @param {string} dirPath The path of the directory to retrieve a config object for.
675
+ * @returns {ConfigData|undefined} A configuration object for the file
676
+ * or `undefined` if there is no configuration data for the file.
677
+ * @throws {Error} If `dirPath` is not a non-empty string.
678
+ * @throws {Error} If `dirPath` is not an absolute path.
679
+ * @throws {Error} If the config file was not already loaded.
680
+ */
681
+ getCachedConfigArrayForPath(dirPath) {
682
+ assertValidFilePath(dirPath);
683
+
684
+ debug(`[Legacy]: Looking up cached config for ${dirPath}`);
685
+
686
+ if (!this.#configArray) {
687
+ throw new Error(`Could not find config file for ${dirPath}`);
688
+ }
689
+
690
+ return this.#configArray;
691
+ }
692
+ }
693
+
694
+ module.exports = { ConfigLoader, LegacyConfigLoader };