-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
Change Request: Flat Config does not support FileEnumerator #18087
Comments
If I'm reading this correctly, your use case is basically to answer the question "is this file ignored by ESLint?" So perhaps a method like |
Sort of - that would require us to enumerate every file on disk and ask eslint about each one, because we also don't know where the eslint "root" is. We specifically need a list of lintable files. |
Okay, then I feel like there's not enough information in the original description to really understand how you're using And when you say "root", what is it you're referring to? (Note that |
By What we need is a list or iterator of lintable files (so we can crawl them all, parse their imports and exports, and figure out which are unused). |
Okay, I'm going to try asking this again: can you please explain exactly the approach/algorithm that you're using right now to do this? We need to know:
There isn't going to be an exact replacement for |
oh sure, sorry if i misunderstood :-) The code that uses FileEnumerator is here: https://github.com/import-js/eslint-plugin-import/blob/7a21f7e10f18c04473faadca94928af6b8e28009/src/rules/no-unused-modules.js#L19-L64 and https://github.com/import-js/eslint-plugin-import/blob/7a21f7e10f18c04473faadca94928af6b8e28009/src/rules/no-unused-modules.js#L173-L187 The rule's config takes a list of globs, and a list of exclusion patterns, which we apply to narrow down the files provided by the Enumerator. The rule requires the globs all be relative to the cwd, so any root determination beyond that is up to eslint via the Enumerator. We're parsing all lintable files; the user defines which extensions in their plugin settings in eslint config. Files are all parsed "on demand" in the sense that it's when the rule is run, but "up front" in that it's the first thing the rule does (if we don't cache it for all future in-process rule runs after that, then oops, we should). |
That's helpful, thanks. So am I understanding you correctly in that if I do something like Looking through the code, it seems like perhaps you could replace It doesn't appear that you need the location of the ESLint config file for any reason (what we called |
yes, that's right - unfortunately i don't have an example config available (they're in previous employers' codebases). using a generic glob utility would be "fine" but there'd be no guarantee it matches the glob semantics eslint uses, now and in the future. (when i said "root" i meant the |
Per 2024-02-08 |
I think you can get pretty close. Internally, we have eslint/lib/eslint/eslint-helpers.js Lines 285 to 333 in 15c143f
The rest of the logic around filtering there is solely for the benefit of being able to output useful errors on the CLI, which is clearly something you wouldn't need to do. So, I think if we exposed |
I agree, that would work, thank you. The only remaining downside is that I'd basically have to copy-paste that logic, and if eslint ever changed it, it'd be out of sync. Is there no way to get direct access to eslint's glob results in the first place, even if unfiltered? |
That is a risk, for sure, but given that we use the same
Unfiltered would be disastrous because you'd end up getting everything in every The glob results of the current run probably won't work for you because that won't give you all of the files in the project. For example, if someone runs As I said, this isn't really functionality we want to encourage in rules, so we're trying to make the surface area to get this working for you as small as possible. (At some point, I'd like to look at a higher level at what it might look like for ESLint to understand the dependency graph of a project.) |
True, basically what we'd want is "the list of paths to be linted as if someone ran |
So, it sounds like, unless eslint can somehow expose a function that takes a path (like |
Let me put together a quick POC that you can try out. |
Ah, thanks, that simplifies it a bit. Look forward to seeing what you come up with :-) |
Here's the branch: The way I have this set up is as a Let me know how that works. |
So, if someone does |
No, the list of files/dirs/globs to be linted does not affect this. On the other hand, there are CLI options that can affect this. Those are: |
ok - so is there any way for a rule to know that those options were provided? (It feels more and more like this is something eslint should be abstracting away, rather than a plugin having to reconstruct eslint's logic) |
So |
ok awesome! |
@ljharb have you had a chance to try this out yet? |
no, not just yet - I'll try to make some time today or tomorrow! |
ping @ljharb -- we really need your feedback on this approach to help with release planning |
I'm trying to look into it, but since I don't use flat config anywhere yet, it's not something I can quickly test :-/ However, I feel pretty confident the approach will work, it's just unfortunate that the globbing logic has to be replicated. |
Is there a reason you can't just switch one of your existing repos to use flat config and try it out? We are blocked on your feedback for this, and we are running out of time to get this into v9, so we'd appreciate you trying this out soon. |
Mainly because almost all of my repos are stuck on eslint v8.8.0 due to #15595, and I no longer have access to a corporate repo, so I'm not actually using the no-unused-modules rule. I'm not really sure how I can get someone to try it out prior to it being published, unfortunately. |
I had some time to look into this. Mainly good news and a bit of bad news. Good news: I was able to get a working Bad news: the specific |
@ljharb Unfortunately, we're not going to be able to ship anything until you've verified that this approach works. The ball is in your court now and it's up to you as to whether or not this makes it in for the final v9.0.0. @JoshuaKGoldberg My hunch is that it's in the implementation, because we use eslint/lib/eslint/eslint-helpers.js Line 19 in 15c143f
|
This might help as an example, @ljharb . I was updating the template I have for typescript-based projects In the commit Let me know if I can help simplify this any further. alexrecuenco/typescript-eslint-prettier-jest-example@20bfe75 |
ESLint version
v8
What problem do you want to solve?
eslint-plugin-import uses FileEnumerator in its
no-unused-modules
rule, for the purpose of gathering a list of files are not eslintignored, or ignored by the user's rule config, for checking if any exports or modules are unused.It seems that in flat config, this capability does not exist.
What do you think is the correct solution?
Something that may work nicely is a new method on
context
provided to rules, that can achieve the same goal, but I have no idea if this makes sense for eslint or not.Participation
Additional comments
This is the sole remaining blocker (afaik) to eslint-plugin-import supporting Flat Config, after which I plan to do a breaking change to drop older eslint versions, which is something quite a lot of users have been asking for.
The text was updated successfully, but these errors were encountered: