Skip to content

Commit 2790318

Browse files
committed
chore: enhance
1 parent 7b42ee0 commit 2790318

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

.changeset/clear-bikes-jog.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
"eslint-plugin-import-x": patch
2+
"eslint-plugin-import-x": minor
33
---
44

5-
fix: align with TypeScript Organize Imports for [mapped imports](https://nodejs.org/api/packages.html#imports)
5+
feat: add option `followTsOrganizeImports` for `order` rule

docs/rules/order.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ This rule supports the following options (none of which are required):
109109
- [`sortTypesGroup`][7]
110110
- [`newlines-between-types`][27]
111111
- [`consolidateIslands`][25]
112+
- [`followTsOrganizeImports`][26]
112113

113114
---
114115

@@ -958,6 +959,17 @@ import type { G } from './aaa.js'
958959
import type { H } from './bbb'
959960
```
960961

962+
### `followTsOrganizeImports`
963+
964+
Valid values: `boolean` \
965+
Default: `false`
966+
967+
> [!CAUTION]
968+
>
969+
> Currently, `followTsOrganizeImports` defaults to `false`. However, in a later update, the default might change to `true`.
970+
971+
When set to `true`, this option will align the behavior with [TypeScript's LSP Organize Imports][34] feature. This only has an effect if no manual `groups` are defined.
972+
961973
## Related
962974

963975
- [`import-x/external-module-folders`][29]
@@ -984,10 +996,12 @@ import type { H } from './bbb'
984996
[22]: https://prettier.io
985997
[23]: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#type-modifiers-on-import-names
986998
[25]: #consolidateislands
999+
[26]: #followtsorganizeimports
9871000
[27]: #newlines-between-types
9881001
[28]: ../../README.md#importinternal-regex
9891002
[29]: ../../README.md#importexternal-module-folders
9901003
[30]: #alphabetize
9911004
[31]: https://webpack.js.org/guides/tree-shaking#mark-the-file-as-side-effect-free
9921005
[32]: #distinctgroup
9931006
[33]: #named
1007+
[34]: https://code.visualstudio.com/docs/typescript/typescript-refactoring#_organize-imports

src/rules/order.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ const defaultGroups = [
5353
'index',
5454
] as const
5555

56-
const defaultGroupsOfficialOrganizeImports = [
56+
const defaultGroupsTsOrganizeImports = [
5757
'private-import',
5858
'external',
5959
'builtin',
6060
'parent',
61-
'sibling',
6261
'index',
62+
'sibling',
6363
] as const
6464

6565
// REPORTING AND FIXING
@@ -1159,7 +1159,7 @@ export interface Options {
11591159
pathGroups?: PathGroup[]
11601160
sortTypesGroup?: boolean
11611161
warnOnUnassignedImports?: boolean
1162-
privateImportsFeatureFlag?: boolean
1162+
followTsOrganizeImports?: boolean
11631163
}
11641164

11651165
type MessageId =
@@ -1282,8 +1282,9 @@ export default createRule<[Options?], MessageId>({
12821282
type: 'boolean',
12831283
default: false,
12841284
},
1285-
privateImportsFeatureFlag: {
1285+
followTsOrganizeImports: {
12861286
type: 'boolean',
1287+
// TODO: switch default to true in next major
12871288
default: false,
12881289
},
12891290
},
@@ -1405,8 +1406,8 @@ export default createRule<[Options?], MessageId>({
14051406
)
14061407
const { groups, omittedTypes } = convertGroupsToRanks(
14071408
options.groups ||
1408-
(options.privateImportsFeatureFlag
1409-
? defaultGroupsOfficialOrganizeImports
1409+
(options.followTsOrganizeImports
1410+
? defaultGroupsTsOrganizeImports
14101411
: defaultGroups),
14111412
)
14121413
ranks = {

test/rules/order.spec.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4813,14 +4813,18 @@ describe('TypeScript', () => {
48134813
tValid({
48144814
code: `import { internA } from "#a";
48154815
import { scopeA } from "@a/a";
4816+
import a from 'a';
4817+
import 'format.css';
48164818
import fs from 'node:fs';
48174819
import path from "path";
4820+
import index from './';
48184821
import { localA } from "./a";
4822+
import sibling from './foo';
48194823
`,
48204824
...parserConfig,
48214825
options: [
48224826
{
4823-
privateImportsFeatureFlag: true,
4827+
followTsOrganizeImports: true,
48244828
},
48254829
],
48264830
}),
@@ -5224,7 +5228,7 @@ import { localA } from "./a";
52245228
...parserConfig,
52255229
options: [
52265230
{
5227-
privateImportsFeatureFlag: true,
5231+
followTsOrganizeImports: true,
52285232
},
52295233
],
52305234
errors: [

0 commit comments

Comments
 (0)