You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/rules/order.md
+63-82
Original file line number
Diff line number
Diff line change
@@ -99,14 +99,14 @@ import 'format2.css' // OK
99
99
This rule supports the following options (none of which are required):
100
100
101
101
-[`groups`][18]
102
-
-[`pathGroups`][19]
103
-
-[`pathGroupsExcludedImportTypes`][15]
102
+
-[`pathGroups`][8]
103
+
-[`pathGroupsExcludedImportTypes`][9]
104
104
-[`distinctGroup`][32]
105
-
-[`newlines-between`][24]
105
+
-[`newlines-between`][20]
106
106
-[`alphabetize`][30]
107
107
-[`named`][33]
108
108
-[`warnOnUnassignedImports`][5]
109
-
-[`sortTypesAmongThemselves`][7]
109
+
-[`sortTypesGroup`][7]
110
110
-[`newlines-between-types`][27]
111
111
-[`consolidateIslands`][25]
112
112
@@ -162,7 +162,7 @@ Roughly speaking, the grouping algorithm is as follows:
162
162
163
163
1. If the import has no corresponding identifiers (e.g. `import './my/thing.js'`), is otherwise "unassigned," or is an unsupported use of `require()`, and [`warnOnUnassignedImports`][5] is disabled, it will be ignored entirely since the order of these imports may be important for their [side-effects][31]
164
164
2. If the import is part of an arcane TypeScript declaration (e.g. `import log = console.log`), it will be considered **object**. However, note that external module references (e.g. `import x = require('z')`) are treated as normal `require()`s and import-exports (e.g. `export import w = y;`) are ignored entirely
165
-
3. If the import is [type-only][6], `"type"` is in `groups`, and [`sortTypesAmongThemselves`][7] is disabled, it will be considered **type** (with additional implications if using [`pathGroups`][8] and `"type"` is in [`pathGroupsExcludedImportTypes`][9])
165
+
3. If the import is [type-only][6], `"type"` is in `groups`, and [`sortTypesGroup`][7] is disabled, it will be considered **type** (with additional implications if using [`pathGroups`][8] and `"type"` is in [`pathGroupsExcludedImportTypes`][9])
166
166
4. If the import's specifier matches [`import-x/internal-regex`][28], it will be considered **internal**
167
167
5. If the import's specifier is an absolute path, it will be considered **unknown**
168
168
6. If the import's specifier has the name of a Node.js core module (using [is-core-module][10]), it will be considered **builtin**
@@ -177,22 +177,20 @@ Roughly speaking, the grouping algorithm is as follows:
177
177
15. If the import's specifier has a name that starts with a word character, it will be considered **external**
178
178
16. If this point is reached, the import will be ignored entirely
179
179
180
-
At the end of the process, if they co-exist in the same file, all top-level `require()` statements that haven't been ignored are shifted (with respect to their order) below any ES6 `import` or similar declarations. Finally, any type-only declarations are potentially reorganized according to [`sortTypesAmongThemselves`][7].
180
+
At the end of the process, if they co-exist in the same file, all top-level `require()` statements that haven't been ignored are shifted (with respect to their order) below any ES6 `import` or similar declarations. Finally, any type-only declarations are potentially reorganized according to [`sortTypesGroup`][7].
181
181
182
182
### `pathGroups`
183
183
184
184
Valid values: `PathGroup[]`\
185
185
Default: `[]`
186
186
187
-
Sometimes [the predefined groups][18] are not fine-grained
188
-
enough, especially when using import aliases. `pathGroups` defines one or more
189
-
[`PathGroup`][13]s relative to a predefined group. Imports are associated with a
190
-
[`PathGroup`][13] based on path matching against the import specifier (using
191
-
[minimatch][14]).
187
+
Sometimes [the predefined groups][18] are not fine-grained enough, especially when using import aliases.
188
+
`pathGroups` defines one or more [`PathGroup`][13]s relative to a predefined group.
189
+
Imports are associated with a [`PathGroup`][13] based on path matching against the import specifier (using [minimatch][14]).
192
190
193
-
> \[!IMPORTANT]
191
+
> [!IMPORTANT]
194
192
>
195
-
> Note that, by default, imports grouped as `"builtin"`, `"external"`, or `"object"` will not be considered for further `pathGroups` matching unless they are removed from [`pathGroupsExcludedImportTypes`][15].
193
+
> Note that, by default, imports grouped as `"builtin"`, `"external"`, or `"object"` will not be considered for further `pathGroups` matching unless they are removed from [`pathGroupsExcludedImportTypes`][9].
196
194
197
195
#### `PathGroup`
198
196
@@ -230,11 +228,10 @@ enough, especially when using import aliases. `pathGroups` defines one or more
Determine the sort order of imports within each [predefined group][18] or [`PathGroup`][19] alphabetically based on specifier.
385
+
Determine the sort order of imports within each [predefined group][18] or [`PathGroup`][8] alphabetically based on specifier.
389
386
390
-
> \[!NOTE]
387
+
> [!NOTE]
391
388
>
392
389
> Imports will be alphabetized based on their _specifiers_, not by their
393
390
> identifiers. For example, `const a = require('z');` will come _after_`const z = require('a');` when `alphabetize` is set to `{ order: "asc" }`.
@@ -445,13 +442,10 @@ Default: `false`
445
442
446
443
Enforce ordering of names within imports and exports.
447
444
448
-
If set to `true` or `{ enabled: true }`, _all_ named imports must be ordered
449
-
according to [`alphabetize`][30]. If set to `false` or `{ enabled:
450
-
false }`, named imports can occur in any order.
445
+
If set to `true` or `{ enabled: true }`, _all_ named imports must be ordered according to [`alphabetize`][30].
446
+
If set to `false` or `{ enabled: false }`, named imports can occur in any order.
451
447
452
-
If set to `{ enabled: true, ... }`, and any of the properties `import`,
453
-
`export`, `require`, or `cjsExports` are set to `false`, named ordering is
454
-
disabled with respect to the following kind of expressions:
448
+
If set to `{ enabled: true, ... }`, and any of the properties `import`, `export`, `require`, or `cjsExports` are set to `false`, named ordering is disabled with respect to the following kind of expressions:
> This setting is only meaningful when [`sortTypesAmongThemselves`][7] is enabled.
631
+
> This setting is only meaningful when [`sortTypesGroup`][7] is enabled.
641
632
642
-
`newlines-between-types` is functionally identical to [`newlines-between`][24]
643
-
except it only enforces or forbids new lines between _[type-only][6] import
644
-
groups_, which exist only when [`sortTypesAmongThemselves`][7] is enabled.
633
+
`newlines-between-types` is functionally identical to [`newlines-between`][20] except it only enforces or forbids new lines between _[type-only][6] import groups_, which exist only when [`sortTypesGroup`][7] is enabled.
645
634
646
-
In addition, when determining if a new line is enforceable or forbidden between
647
-
the type-only imports and the normal imports, `newlines-between-types` takes
648
-
precedence over [`newlines-between`][24].
635
+
In addition, when determining if a new line is enforceable or forbidden between the type-only imports and the normal imports, `newlines-between-types` takes precedence over [`newlines-between`][20].
Note the new line after `import type E from './';` but before `import a from "fs";`. This new line separates the type-only imports from the normal imports. Its existence is governed by [`newlines-between-types`][27] and _not `newlines-between`_.
705
692
706
-
> \[!IMPORTANT]
693
+
> [!IMPORTANT]
707
694
>
708
695
> In certain situations, [`consolidateIslands: true`][25] will take precedence over `newlines-between-types: "never"`, if used, when it comes to the new line separating type-only imports from normal imports.
709
696
710
-
The next example will pass even though there's a new line preceding the normal import and [`newlines-between`][24] is set to `"never"`:
697
+
The next example will pass even though there's a new line preceding the normal import and [`newlines-between`][20] is set to `"never"`:
> This setting is only meaningful when [`newlines-between`][24] and/or [`newlines-between-types`][27] is set to `"always-and-inside-groups"`.
772
+
> This setting is only meaningful when [`newlines-between`][20] and/or [`newlines-between-types`][27] is set to `"always-and-inside-groups"`.
786
773
787
-
When set to `"inside-groups"`, this ensures imports spanning multiple lines are separated from other imports with a new line while single-line imports are grouped together (and the space between them consolidated) if they belong to the same [group][18] or \[`pathGroups`]\[26].
774
+
When set to `"inside-groups"`, this ensures imports spanning multiple lines are separated from other imports with a new line while single-line imports are grouped together (and the space between them consolidated) if they belong to the same [group][18] or [`pathGroups`][8].
788
775
789
-
> \[!IMPORTANT]
776
+
> [!IMPORTANT]
790
777
>
791
778
> When all of the following are true:
792
779
>
780
+
> -[`sortTypesGroup`][7] is set to `true`
793
781
> -`consolidateIslands` is set to `"inside-groups"`
794
-
> -[`newlines-between`][24] is set to `"always-and-inside-groups"`
795
-
> -[`newlines-between-types`][27] is set to `"never"`
796
-
> -[`sortTypesAmongThemselves`][7] is set to `true`
782
+
> -[`newlines-between`][20] is set to `"always-and-inside-groups"` when [`newlines-between-types`][27] is set to `"never"` (or vice-versa)
797
783
>
798
-
> Then [`newlines-between-types`][27] will yield to `consolidateIslands` and allow new lines to separate multi-line imports and a single new line to separate all [type-only imports][6] from all normal imports. Other than that, [`newlines-between-types: "never"`][27] functions as described.
784
+
> Then [`newlines-between`][20]/[`newlines-between-types`][27] will yield to
785
+
> `consolidateIslands` and allow new lines to separate multi-line imports
786
+
> regardless of the `"never"` setting.
799
787
>
800
-
> This configuration is useful to keep type-only imports stacked tightly
801
-
> together at the bottom of your import block to preserve space while still
802
-
> logically organizing normal imports for quick and pleasant reference.
788
+
> This configuration is useful, for instance, to keep single-line type-only
789
+
> imports stacked tightly together at the bottom of your import block to
790
+
> preserve space while still logically organizing normal imports for quick and
791
+
> pleasant reference.
803
792
804
793
#### Example
805
794
@@ -853,7 +842,7 @@ var sibling2 = require('./bar')
853
842
var sibling3 =require('./foobar')
854
843
```
855
844
856
-
Note the intragroup "islands" of grouped single-line imports, as well as multi-line imports, are surrounded by new lines. At the same time, note the typical new lines separating different groups are still maintained thanks to [`newlines-between`][24].
845
+
Note the intragroup "islands" of grouped single-line imports, as well as multi-line imports, are surrounded by new lines. At the same time, note the typical new lines separating different groups are still maintained thanks to [`newlines-between`][20].
857
846
858
847
The same holds true for the next example; when given the following settings:
859
848
@@ -883,25 +872,20 @@ The same holds true for the next example; when given the following settings:
883
872
"newlines-between":"always-and-inside-groups",
884
873
"newlines-between-types":"never",
885
874
"pathGroupsExcludedImportTypes": [],
886
-
"sortTypesAmongThemselves":true,
875
+
"sortTypesGroup":true,
887
876
"consolidateIslands":"inside-groups",
888
877
},
889
878
],
890
879
}
891
880
```
892
881
893
-
> \[!IMPORTANT]
882
+
> [!IMPORTANT]
894
883
>
895
-
> **Pay special attention to the value of
896
-
> [`pathGroupsExcludedImportTypes`](#pathgroupsexcludedimporttypes)** in this
897
-
> example's settings. Without it, the successful example below would fail. This is
898
-
> because the imports with specifiers starting with "dirA/", "dirB/", and
899
-
> "dirC/" are all [considered part of the `"external"`
900
-
> group](#how-imports-are-grouped), and imports in that group are
901
-
> excluded from [`pathGroups`](#pathgroups) matching by default.
884
+
> **Pay special attention to the value of [`pathGroupsExcludedImportTypes`][9]** in this example's settings.
885
+
> Without it, the successful example below would fail.
886
+
> This is because the imports with specifiers starting with "dirA/", "dirB/", and "dirC/" are all [considered part of the `"external"` group](#how-imports-are-grouped), and imports in that group are excluded from [`pathGroups`][8] matching by default.
902
887
>
903
-
> The fix is to remove `"external"` (and, in this example, the others) from
0 commit comments