Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for TS config files #18134

Draft
wants to merge 40 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
eac9360
Initial working implementation of TS config files
aryaemami59 Feb 20, 2024
3833b5b
Simplify implementation
aryaemami59 Feb 20, 2024
840378c
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Feb 23, 2024
150d567
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Feb 23, 2024
f196b6b
Use typescript's `transpile()` instead of `tsx` to avoid side effects
aryaemami59 Feb 27, 2024
9b630ea
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Feb 28, 2024
ac08d46
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Feb 29, 2024
c5a8862
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Mar 2, 2024
a016dff
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Mar 5, 2024
00af268
Remove unnecessary nullish coalescing
aryaemami59 Mar 6, 2024
5ee0c4c
Use `jiti` instead of TypeScript
aryaemami59 Mar 6, 2024
d58270e
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Mar 7, 2024
c69224c
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Mar 9, 2024
421587c
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Mar 11, 2024
5eae061
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Mar 13, 2024
40c77d4
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Mar 15, 2024
e004292
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Mar 17, 2024
7540572
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Mar 19, 2024
0f8dc8a
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Mar 21, 2024
6eb0f88
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Mar 26, 2024
6ccf10f
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Mar 28, 2024
7b00b03
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Apr 2, 2024
c341646
Add missing section related to `importedConfigFileModificationTime`
aryaemami59 Apr 2, 2024
8614fd3
Enable `esmResolve` for `jiti`
aryaemami59 Apr 2, 2024
8556132
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Apr 3, 2024
5ebf9f9
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Apr 6, 2024
68ab0af
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Apr 9, 2024
1081889
Merge branch 'eslint.config.ts' of https://github.com/aryaemami59/esl…
aryaemami59 Apr 12, 2024
e7eb303
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Apr 17, 2024
424ce93
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Apr 22, 2024
ab5ef8f
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Apr 23, 2024
599c6a5
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Apr 29, 2024
dad7b0e
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 Apr 30, 2024
2bfe8b5
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 May 6, 2024
10d147e
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 May 9, 2024
9f09770
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 May 11, 2024
360cf38
Merge branch 'main' of https://github.com/eslint/eslint into eslint.c…
aryaemami59 May 14, 2024
b162744
Make `jiti` an optional peer dependency
aryaemami59 May 14, 2024
8aca3eb
Throw an error if `jiti` is not installed
aryaemami59 May 14, 2024
b532667
Add `jiti` to `ignoreDependencies` in `knip.jsonc`
aryaemami59 May 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 42 additions & 1 deletion lib/eslint/eslint.js
Expand Up @@ -94,6 +94,9 @@ const LintResultCache = require("../cli-engine/lint-result-cache");
//------------------------------------------------------------------------------

const FLAT_CONFIG_FILENAMES = [
"eslint.config.ts",
"eslint.config.mts",
"eslint.config.cts",
"eslint.config.js",
"eslint.config.mjs",
"eslint.config.cjs"
Expand Down Expand Up @@ -269,6 +272,17 @@ function findFlatConfigFile(cwd) {
);
}

/**
* Check if the file is a TypeScript file.
* @param {string} filePath The file path to check.
* @returns {boolean} `true` if the file is a TypeScript file, `false` if it's not.
*/
function isFileTS(filePath) {
const fileExtension = path.extname(filePath);

return fileExtension.endsWith("ts");
}

/**
* Load the config array from the given filename.
* @param {string} filePath The filename to load from.
Expand Down Expand Up @@ -312,7 +326,34 @@ async function loadFlatConfigFile(filePath) {
delete require.cache[filePath];
}

const config = (await import(fileURL)).default;
const isTS = isFileTS(filePath);

if (isTS) {
const ts = (await import("typescript")).default;

const tsFileContent = await fs.readFile(filePath, "utf-8");

const tempFileName = `eslint.config.temp${path.extname(path.basename(filePath)).replace("t", "j")}`;

const outputPath = path.join(path.dirname(filePath), tempFileName);

const outputText = ts.transpile(tsFileContent,
{ module: ts.ModuleKind.ESNext, moduleResolution: ts.ModuleResolutionKind.Node10, target: ts.ScriptTarget.ESNext });

try {
await fs.writeFile(outputPath, outputText, "utf-8");

const outputFileURL = pathToFileURL(outputPath);

const config = (await import(outputFileURL.href)).default;

return config;
} finally {
await fs.unlink(outputPath);
}
}
aryaemami59 marked this conversation as resolved.
Show resolved Hide resolved

const config = (await import(fileURL.href)).default?.default ?? (await import(fileURL.href)).default;
aryaemami59 marked this conversation as resolved.
Show resolved Hide resolved

importedConfigFileModificationTime.set(filePath, mtime);

Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -96,7 +96,8 @@
"natural-compare": "^1.4.0",
"optionator": "^0.9.3",
"strip-ansi": "^6.0.1",
"text-table": "^0.2.0"
"text-table": "^0.2.0",
"typescript": "^5.3.3"
},
"devDependencies": {
"@babel/core": "^7.4.3",
Expand Down