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: enforce consistent imports with import/order #26

Merged
merged 2 commits into from
Jul 23, 2024

Conversation

AriPerkkio
Copy link
Member

Example:

// Before
import utils from "./utils";
import { test } from "vitest"
import messages from "../util/messages"
import fs from "fs";
import json from "./json";
// After
import fs from 'fs';                     // Group: Built in module
import { test } from 'vitest';           // Group: 3rd party dependency
import messages from '../util/messages'; // Group: Relative files, sorted asc
import json from './json';               // Group: Relative files, sorted asc
import utils from './utils';             // Group: Relative files, sorted asc

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation if applicable
  • I have added tests for this change

@AriPerkkio
Copy link
Member Author

@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. import 'fs' -> import 'node:fs'.

@AriPerkkio AriPerkkio requested a review from d3lm July 18, 2024 10:52
@d3lm
Copy link
Collaborator

d3lm commented Jul 18, 2024

Yep, I'd be fine with adding prefer-node-protocol I think that's a good thing and makes it easier which modules are Node builtins vs 3rd party deps.

},
rules: {
// https://github.com/un-ts/eslint-plugin-import-x/blob/master/docs/rules/order.md
'import/order': ['error', { alphabetize: { order: 'asc' } }],
Copy link
Collaborator

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.

Copy link
Member Author

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

Copy link
Collaborator

Choose a reason for hiding this comment

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

Very cool!

Copy link
Collaborator

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?

Copy link
Member Author

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.

Copy link
Collaborator

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.

Copy link
Member Author

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. 😄

@d3lm
Copy link
Collaborator

d3lm commented Jul 22, 2024

There's conflicts here as well in the lock file. I suppose we can just re-genrate it.

Copy link
Collaborator

@d3lm d3lm left a 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! 🙏 🔥

@d3lm d3lm merged commit f24689d into stackblitz:main Jul 23, 2024
2 checks passed
@AriPerkkio AriPerkkio deleted the feat/import-order branch July 23, 2024 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants