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

Sort imports alphabetically (JS, TS) #504

Open
hyunbinseo opened this issue Mar 24, 2025 · 3 comments
Open

Sort imports alphabetically (JS, TS) #504

hyunbinseo opened this issue Mar 24, 2025 · 3 comments
Labels
enhancement New feature or request pkg:add sv add

Comments

@hyunbinseo
Copy link
Contributor

Things I do after scaffolding a project include manually saving *.js and *.ts files. With this setting enabled on VS Code:

{ "editor.codeActionsOnSave": { "source.organizeImports": "explicit" } }

ESLint, Vite configs and other files get their import sorted. For example:

// eslint.config.js (generated)
import prettier from 'eslint-config-prettier';
import js from '@eslint/js';
import { includeIgnoreFile } from '@eslint/compat';
// eslint.config.js (organized with VS Code)
import { includeIgnoreFile } from '@eslint/compat';
import js from '@eslint/js';
import prettier from 'eslint-config-prettier';

I believe prettier-plugin-sort-imports uses the same order.

@manuel3108 manuel3108 added enhancement New feature or request pkg:add sv add labels Mar 24, 2025
@manuel3108
Copy link
Member

Not sure if or how this would properly be doable. One thought I had was to add this sorting to esrap, but in case sv is adding to an existing file, this might completely destroy the user provided order.

A simpler idea would just be to order the import calls in our source code here:

if (typescript) imports.addDefault(ast, 'typescript-eslint', 'ts');
imports.addNamed(ast, 'node:url', { fileURLToPath: 'fileURLToPath' });
imports.addDefault(ast, 'globals', 'globals');
imports.addDefault(ast, 'eslint-plugin-svelte', 'svelte');
imports.addNamed(ast, '@eslint/compat', { includeIgnoreFile: 'includeIgnoreFile' });
imports.addDefault(ast, '@eslint/js', 'js');

I think the second idea seems way more reasonable long term

@AdrianGonz97
Copy link
Member

What happens if we're just modifying an existing file and the imports are not already ordered alphabetically? Would we just indiscriminately rearrange their imports (which would be a bit rude)? We'd also have to consider the possible side-effects of those existing imports.

I just don't think this feature would be worth the effort.

@manuel3108
Copy link
Member

Agree, that's why I suggest just re-ordering the imports in our source code, that should already help a lot i think

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request pkg:add sv add
Projects
None yet
Development

No branches or pull requests

3 participants