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 20 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 d7a3132
🔍 Latest deploy log https://app.netlify.com/sites/docs-eslint/deploys/665b4ffa0611c20008da51c4

Co-authored-by: aryaemami59 <aryaemami59@users.noreply.github.com>
@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.

Choose a reason for hiding this comment

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

Oh my gosh, thank you so much for figuring this one out, I was banging my head on the wall for quite some time trying to figure this one out.

@antfu
Copy link
Contributor Author

antfu commented May 29, 2024

PR is still blocking due to a tsx issue of loading cts file - waiting for upstream fixed to proceed this PR.

@aryaemami59
Copy link

@antfu It's a little hacky, but could this possibly work?

"eslint.config.cjs",
"eslint.config.ts",
"eslint.config.mts",
"eslint.config.cts"
Copy link
Contributor

Choose a reason for hiding this comment

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

What about eslint.config.tsx? It's a legitimate file extension that some frontend-area projects enforce all files adhere to for consistency.

Copy link
Member

Choose a reason for hiding this comment

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

Can you give some examples?

Copy link
Contributor Author

@antfu antfu Jun 1, 2024

Choose a reason for hiding this comment

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

I am not sure about it personally. To me, TSX is a DSL specific to some frontend frameworks. And if we going to have .tsx, one could argue to have .jsx - which is not supported by Node.js without transpiling. I am not sure what's the use case of having TSX in the config file, and such a convension does not seem to widely supported by other tools.

@@ -918,6 +918,112 @@ describe("ESLint", () => {
assert.strictEqual(results[0].messages[0].ruleId, "no-undef");
});
});

describe("TypeScript config files", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

[Testing] What do you think about including a test file that includes the not-very-friendly-to-transpilation TypeScript features?

  • const enum
  • namespace
  • @ experimentalDecorators

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting, I personally would never use them because they are not transpilation friendly 😇 But good points - I will try to do a matrix tests in importx first to see how each solution works with them

Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
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

6 participants