Skip to content

Commit 209c791

Browse files
committed
Try a different approach for loading TS config files
- This is done for mainly 2 reasons:   1. We don't know how many runtime environments are going to support loading TS files natively in the future, so this saves us having to check for every single one.   2. This also ensures that we give the user the option of passing their own TS loader of choice through `NODE_OPTIONS` in CLI for example: `NODE_OPTIONS=--import=tsx/esm`, without ESLint getting in the way and potentially causing conflicts between multiple loaders.
1 parent b0e5f96 commit 209c791

1 file changed

Lines changed: 32 additions & 25 deletions

File tree

lib/eslint/eslint.js

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -291,20 +291,22 @@ function isFileTS(filePath) {
291291
return fileExtension.endsWith("ts");
292292
}
293293

294-
/**
295-
* Check if ESLint is running in Bun.
296-
* @returns {boolean} `true` if the ESLint is running Bun, `false` if it's not.
297-
*/
298-
function isRunningInBun() {
299-
return !!globalThis.Bun || !!globalThis.process?.versions?.bun;
300-
}
294+
const unableToLoadTSFile = Symbol("Unable to load TS file");
301295

302296
/**
303-
* Check if ESLint is running in Deno.
304-
* @returns {boolean} `true` if the ESLint is running in Deno, `false` if it's not.
297+
* Load the TypeScript config file.
298+
* @param {string} filePath The file path to check.
299+
* @returns {Promise<{ default?: any } | typeof unableToLoadTSFile>} The loaded TypeScript file or `false` if it's not loaded.
305300
*/
306-
function isRunningInDeno() {
307-
return !!globalThis.Deno;
301+
async function loadTSFile(filePath) {
302+
try {
303+
return await import(filePath);
304+
} catch (error) {
305+
306+
debug(`${error}`);
307+
308+
return unableToLoadTSFile;
309+
}
308310
}
309311

310312
/**
@@ -350,30 +352,35 @@ async function loadFlatConfigFile(filePath) {
350352
delete require.cache[filePath];
351353
}
352354

355+
let config;
356+
353357
const isTS = isFileTS(filePath);
354358

355-
const isBun = isRunningInBun();
359+
if (isTS) {
356360

357-
const isDeno = isRunningInDeno();
361+
const loadedTSFile = await loadTSFile(fileURL.href);
358362

359-
if (isTS && !isDeno && !isBun) {
363+
if (loadedTSFile === unableToLoadTSFile) {
364+
try {
365+
require.resolve("jiti");
366+
} catch (error) {
367+
debug(`${error}`);
360368

361-
try {
362-
require.resolve("jiti");
363-
} catch {
364-
throw new Error("'jiti' is required for loading TypeScript configuration files. Make sure to install it.");
365-
}
369+
throw new Error("'jiti' is required for loading TypeScript configuration files. Make sure to install it.");
370+
}
366371

367-
const jiti = (await import("jiti")).default(__filename, { interopDefault: true, esmResolve: true });
372+
const jiti = (await import("jiti")).default(__filename, { interopDefault: true, esmResolve: true });
368373

369-
const config = jiti(fileURL.href);
374+
config = jiti(fileURL.href);
370375

371-
importedConfigFileModificationTime.set(filePath, mtime);
376+
} else {
377+
config = loadedTSFile?.default ?? loadedTSFile;
378+
}
372379

373-
return config;
374-
}
380+
} else {
381+
config = (await import(fileURL.href)).default;
375382

376-
const config = (await import(fileURL.href)).default;
383+
}
377384

378385
importedConfigFileModificationTime.set(filePath, mtime);
379386

0 commit comments

Comments
 (0)