-
Notifications
You must be signed in to change notification settings - Fork 7
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: enforce consistent imports with import/order
#26
Conversation
@d3lm any thought about adding https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/prefer-node-protocol.md? It would enforce consistent Node built-in imports, e.g. |
Yep, I'd be fine with adding |
}, | ||
rules: { | ||
// https://github.com/un-ts/eslint-plugin-import-x/blob/master/docs/rules/order.md | ||
'import/order': ['error', { alphabetize: { order: 'asc' } }], |
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.
Can we double check if it works correctly that it doesn't re-order imports that are separated by newlines? So that each block of imports is independently sorted and not all imports are sorted / grouped? There is often the use case to have say .css
imports in a separate block to group them a bit by JS and styles for instance.
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.
Yep, this is allowed:
import fs from 'fs';
import { test } from 'vitest';
import messages from '../util/messages';
import json from './json';
import utils from './utils';
import './side-effects';
import './some.css';
Looks like this is nicely configurable: https://github.com/un-ts/eslint-plugin-import-x/blob/master/docs/rules/order.md#newlines-between-ignorealwaysalways-and-inside-groupsnever
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.
Very cool!
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.
The only thing would be that I can prolly no longer use the "organize imports" in VSC since it doesn't match the ordering of this plugin. That's very unfortunate cause it's so easy but I guess the plugin provides a fixer?
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.
Yup, there's autofix! Just run ESLint autofixes on save and it provides similar experience.
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.
Okay, I guess I can re-map maybe that shortcut to run eslint or something. I have to look into it unless you know if that's possible.
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've used just ESLint and prettier plugins for sorting the imports. IDE's modifying code is not consistent across all developers so I try to avoid those. 😄
There's conflicts here as well in the lock file. I suppose we can just re-genrate it. |
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.
If we resolve the conflict in the lock file this is good to go! 🙏 🔥
ee0b878
to
50f31df
Compare
import/order
: https://github.com/un-ts/eslint-plugin-import-x/blob/master/docs/rules/order.mdExample:
Checklist