Skip to content

fix(eslint): generated import order are now sorted alphabetically #592

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

Merged
merged 1 commit into from
Jun 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slimy-wombats-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'sv': patch
---

fix(eslint): generated import order are now sorted alphabetically
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@
"esbuild"
]
}
}
}
4 changes: 2 additions & 2 deletions packages/addons/eslint/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { addEslintConfigPrettier } from '../common.ts';
import { defineAddon, log } from '@sveltejs/cli-core';
import {
array,
Expand All @@ -10,6 +9,7 @@ import {
type AstTypes
} from '@sveltejs/cli-core/js';
import { parseJson, parseScript } from '@sveltejs/cli-core/parsers';
import { addEslintConfigPrettier } from '../common.ts';

export default defineAddon({
id: 'eslint',
Expand Down Expand Up @@ -164,8 +164,8 @@ export default defineAddon({
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');
imports.addNamed(ast, '@eslint/compat', { includeIgnoreFile: 'includeIgnoreFile' });

return generateCode();
});
Expand Down
5 changes: 5 additions & 0 deletions packages/core/tests/js/imports/reverse-order/output.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as foo from 'p1';
import * as bar from './p2';
import { namedTwo } from 'p3';
import 'p4';
import MyPackage from 'p5';
16 changes: 16 additions & 0 deletions packages/core/tests/js/imports/reverse-order/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { imports, type AstTypes } from '@sveltejs/cli-core/js';

export function run(ast: AstTypes.Program): void {
// imports should be added HERE in the reverse order
// so that the imports are added from the top (unshift)
imports.addDefault(ast, 'p5', 'MyPackage');
imports.addEmpty(ast, 'p4');
imports.addNamed(ast, 'p3', { namedTwo: 'namedTwo' }, false);
imports.addNamespace(ast, './p2', 'bar');
imports.addNamespace(ast, 'p1', 'foo');

// adding the same import twice should not produce two imports
imports.addNamespace(ast, './p2', 'bar');
// adding the same import for the 3rd time. Only the first one should be kept.
imports.addNamespace(ast, './p2', 'bar');
}