-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
import/order
fixer is not working properly?
#2154
Comments
I thought it was bad resolver... but after changing to |
It’s working fine. Imports without bindings have side effects, and those can’t be safely reordered across by the autofixer. Put the css import at the top or bottom, and the fixer should be able to handle the rest. |
I wonder, can I use something like
to move those on Sadly, it does not work :C |
No. Side effecting imports can never, under any circumstances, be safely auto fixed. |
Maybe it could be feature request? Maybe adding some flag Anyway, for now it looks like I need find all of those and fix them. # Since I use Windows (with Powershell) I made use of it to find it
ls -R *.tsx,*.ts | sls 'import [''"]' # to just list occurrences
# or
ls -R *.tsx,*.ts | sls 'import [''"]' | % { code $_.Path } # to open related files in VS Code But, yeah, moving my |
I don't think that's worth it. I think doing it manually is the best and safest approach. |
Experiencing the same issue. I think that safety should not take priority over usability. User could be provided with unsafe option, if that option can really help him. I have hundreds of components with such css imports and I don't have imports that can change program in any way because of import order changed. Program that depends on import order is a bad program, most likely. Eslint's fix function could have much higher value in my case and probably many other cases if rule could affect imports with side effects. |
Safety takes priority over everything, always, full stop. |
Well, imports with bindings also could have side effects, so their ordering is not safe either. We can also have imports with bindings and side effects and imports without bindings and without side effects. Existence of bindings does not tell us anything about side effects. Nothing can tell, therefore import ordering can't be safe at all. We make software to automate some work. It sounds ridiculous to me to do that work manually because it is not safe in cases that are nonsense. It is not safe in other nonsense cases though, so it is even more ridiculous. |
Yes, that is all true - we had to draw a line somewhere, and this is where it's been drawn. Having side effects in modules that export things is indeed nonsense - if you have this, your application is already irreparably broken. However, a module depending on side effects introduced in a previous import is both not nonsense, and happens all the time in codebases, so it's a perfectly reasonable thing to protect against. |
I still disagree with you and think that there are two good arguments to include imports without bindings in the rule:
|
I agree fully with sk1e. Meanwhile:
That's hardly valid an argument to be honest. Why not leave that possibility for possible feature that some people would find useful? It's not like it's super important thing, I guess it wouldn't be destructive for the plugin design if someone wrote PR for this. I include I guess this issue could be closed, as there is working workaround, at least to mine problem that I described in the first post. If someone want to have side effects import reordering enabled, one could create new issue as proper feature request - hopefully, after finding this conversation, because sk1e has included few good points in favour of the possible feature. |
I could add 3. point to the sk1e post above:
Of course, again, I don't mean that its important feature, just make it open up to contribution instead drawing hard line before it. |
I do understand that if you've been using nonstandard side-effecting CSS imports in a large codebase, and then you want to suddenly order your imports, that you'll be in a tough spot and need to do lots of manual work. I'm not sure how to square the safety concerns with the convenience angle. |
@AgainPsychoX I can suggest you prettier plugin for this job. It has some downsides, but works better for me. |
I can understand both sides / opinions. But in most cases the order of the css file does not matter as it is not used by the other imports (they are normally not dependent) as these often just contain the styles of the current component. Not sure why there is such a big resistance to not add an optional config setting (with a warning when used) to move these imports to the top or bottom. Supporting this use case should not be a big problem imho. And optionally there could be defined which extensions are safe to reorder (css vs js). The latter is generally a problem and should be skipped and printed as warning. We will also switch to the aforementioned prettier plugin in a big project. Thanks for the link @sk1e. |
@DanielRuf because the order not mattering is uniquely true for css imports - and it would be very brittle to only allow “.css” imports. |
The argument I'm seeing is that this option is not being considered because imports without bindings can have side effects, and these side-effects make re-ordering dangerous. However, as @sk1e pointed out, even imports WITH bindings can have side-effects, and @ljharb you acknowledged this yourself:
It's clear that there are a bunch of people in the community who support having this option and have listed a number of reasons for why it's a valid feature. I don't understand why a line has to be drawn here. I don't think it has to be. Safe defaults are a great idea and I commend your resolve, but allowing flexibility here would better support the needs of the community, and is a win-win for everybody. The defaults can remain safe, while others can manually adjust those settings to what works best in their organization's coding standards. Thanks @sk1e for suggesting the Prettier plugin, it worked great for me. |
At least something in the docs that having unbound imports will WILDLY BREAK |
I am trying to use
eslint
with the plugin in my project, and I was happy when I saw that there are fixers for import ordering, so I don't have to visit hundreds of files to satisfy the linter.(from https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/order.md)
Unfortunately, in quite a lot of cases it does not work.
I use both
plugin:import/recommended
andplugin:import/typescript
(project uses Typescript/React (.ts
/.tsx
)).Rules (only
import/*
listed):Settings (only
import/*
listed):Example
File (only imports):
Linter output:
Using
--fix
does nothing, despite I imagine removing empty lines should be easy (its obviously detected already, isn't it?).Reordering the lines also should be quite easy...
Is there issue with Typescript/React files maybe?
The text was updated successfully, but these errors were encountered: