-
Notifications
You must be signed in to change notification settings - Fork 17
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
Allow unassigned imports #37
base: main
Are you sure you want to change the base?
Conversation
test/rules/order-imports.js
Outdated
output: ` | ||
import './an-unassigned-relative'; | ||
import path from 'path'; | ||
import _ from './relative'; | ||
import 'an-unassigned-module'; | ||
`, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does eslint
compare this output after running '--fix' against this plugin. If so, shouldn't this be re-ordered because when running locally against a file, the fixer does re-order as per the errors mentioned below.
Probably I'm missing something here?
@Tibfib Would you please review it whenever you can find the time? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @PezCoder!
Thanks so much for this contribution. You're right about that test being off. I pulled down the PR and looks like you've got it working for reporting errors but not for fixing the unassigned imports. I think this is where you'll need to tweak it: https://github.com/Tibfib/eslint-plugin-import-helpers/blob/master/src/rules/order-imports.ts#L193
The "canReorderItems" is reporting false for the unassigned/bare imports. You'll want to pass down the "allow unassignedImports" config setting and have that return true, or something to that effect.
Thanks for your patience with this review. Hopefully you're still interested in getting this in 🙂
@Tibfib Thank you for the review & the instructions. I'll try & take up the suggestions later in the day & push them. |
@Tibfib Pushed the changes suggested & the test is working fine now. While doing this I also realized that I'll need checks to make this work for 'require' type imports as well, somewhere here & here I'm not sure how can we detect if a I tried an experiment of running it over this to know the difference:
Diff: https://www.diffchecker.com/au7ZJqaI Unassigned import node, seem to have the type
|
The function |
@Tibfib Understood, so the sole purpose of that function is to find unassigned imports. My question was just around to understand, how can I make sure that this new option that I've introduced also takes care of |
function canCrossNodeWhileReorder(node: NodeOrToken): boolean { | ||
return isPlainRequireModule(node) || isPlainImportModule(node); | ||
function canCrossNodeWhileReorder(node: NodeOrToken, context): boolean { | ||
return isPlainRequireModule(node) || isAllowedImportModule(node, context); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest leaving the isPlainImportModule
function alone and instead, on this line, add in a check for
I think it would be:
return getOptions(context).unassignedImports === 'allow'` || isPlainRequireModule(node) || isAllowedImportModule(node)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not 100% sure on this, I think this would also make it sort on variables or other types of nodes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tibfib Yeah, I was thinking that too but wanted to make sure I don't let any other node which may not be an import pass through.
After re-reviewing the code, I don't think this is correct. The intention of the function is to find import statements and then to make sure those import statements are assigned.
Yeah, okay, I'm following a bit more now. I'd have to run this code myself to test for sure, but I think you're on the right track for bare "requires". Make sure you've got good test cases and that the existing test cases don't break while using the (Sorry about this! I didn't write all of this code so it takes a bit of diving in for me to remember what it does) |
Any news? |
@hyoretsu I got a little occupied with my work, I'll probably be able to resume this sometime by the end of this week. |
Spent some time on this today. Somewhere I'm having a hard time figuring out is: As far as I can understand we use the former when reading the Whereas In the case of What I need to work here is to make sure: |
I would also love to see this land |
News? 👀😬 |
Any chance we might get this implemented? |
What?
Add a way to allow sorting for unassigned imports. Please check the updated order-imports.md for documentation.
Resolves issues: #35 #24
My Usecase
A very common use-case is to have style imports
import 'some-style.scss'
sorted at the end of the file when building react components. Even after defining a separate group for these files/\.scss$/
, the sorting didn't work because we ignore unassigned imports. This change lets the user enable this option if they'd like.