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: support TypeScript config using importx #18440

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

antfu
Copy link
Contributor

@antfu antfu commented May 10, 2024

Prerequisites checklist

What is the purpose of this pull request? (put an "X" next to an item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[x] Add something to the core
[ ] Other, please explain:

What changes did you make? (Give an overview)

Support loading eslint.config.ts eslint.config.mts eslint.config.cts files powered by importx. The support is opt-in by users, where they need to install importx explicitly (we include it as optional peer deps instead of dependencies)

Based on my previous experiments with eslint-ts-patch(which supports swapping different loaders like jiti tsx bundle-require, feel free to try them out). importx is a package trying to ease out the differences between them and the complexity of the pros/cons hidden from ESLint.

The only downside is that tsx uses Node's API, which has only been available since v20.8.0 and v18.19.0. But considering this is an opt-in feature, it should be fine, and the ecosystem should catch up soon. importx ease out that will auto fallback to jiti on unsupported node version.

Is there anything you'd like reviewers to focus on?

@antfu antfu requested a review from a team as a code owner May 10, 2024 21:23
@eslint-github-bot eslint-github-bot bot added the feature This change adds a new feature to ESLint label May 10, 2024
@github-actions github-actions bot added cli Relates to ESLint's command-line interface core Relates to ESLint's core APIs and features labels May 10, 2024
Copy link

netlify bot commented May 10, 2024

Deploy Preview for docs-eslint canceled.

Name Link
🔨 Latest commit d2660d6
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/6655b6e53d4fb5000853d915

@fasttime fasttime added the needs design Important details about this change need to be discussed label May 11, 2024
@nzakas nzakas marked this pull request as draft May 13, 2024 14:39
@nzakas
Copy link
Member

nzakas commented May 13, 2024

Marking as a draft to avoid accidental merging, as there is an RFC being discussed.

Copy link

Hi everyone, it looks like we lost track of this pull request. Please review and see what the next steps are. This pull request will auto-close in 7 days without an update.

@antfu antfu changed the title feat: support TypeScript config using tsx feat: support TypeScript config using importx May 24, 2024
@fasttime fasttime added accepted There is consensus among the team that this change meets the criteria for inclusion and removed needs design Important details about this change need to be discussed labels May 27, 2024
@fasttime
Copy link
Member

Thanks for this PR, @antfu! In order to move forward with this pull request, we will need some unit tests to verify that TypeScript config files are being loaded as expected.

My suggestion would be to start looking at these tests and add similar tests to check the behavior with packages that have config files with .ts, .cts and .mts extension in place of .js, .cjs and .mjs respectively. It would be also useful to have a test that checks loading a config file whose path is specified in overrideConfigFile like this one. When you are done, just mark the PR as ready for review. And of course, just ping me if you need help!

@fasttime
Copy link
Member

Oh, and can you have a look at the lint problems? You can run npm run lint to verify the files locally.

"./hash": hashStub
});
const newLintResultCache = new NewLintResultCache(cacheFileLocation, "metadata");
const versionStub = sandbox.stub(process, "version").value(version);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wasn't correctly restored, causing process.version to always be the mocked value for sub-sequence tests.

const config = (await import(fileURL)).default;
let config;

if (isFileTS(filePath)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because Node.js is the only runtime that can't load TypeScript natively, maybe we can also check that globalThis.Deno and globalThis.Bun are undefined before loading importx?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With register/module-loader like ts-node or tsx (the cli), or testing frameworks like Vitest, Node.js can load TypeScript - the goal for importx is to cover those details, where ESLint or other users can be agnostic to runtime like Deno and Bun, while also could automatically work if we have yet another runtime in the future.

importx already doing lazying loading internally - so importing importx itself should be fairly lightweight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion cli Relates to ESLint's command-line interface core Relates to ESLint's core APIs and features feature This change adds a new feature to ESLint
Projects
Status: Implementing
Development

Successfully merging this pull request may close these issues.

None yet

4 participants