1 # Import Sorting (sort-imports)
3 The import statement is used to import members (functions, objects or primitives) that have been exported from an external module. Using a specific member syntax:
6 // single - Import single member.
7 import myMember from "my-module.js";
9 // multiple - Import multiple members.
10 import {foo, bar} from "my-module.js";
12 // all - Import all members, where myModule contains all the exported bindings.
13 import * as myModule from "my-module.js";
16 The import statement can also import a module without exported bindings. Used when the module does not export anything, but runs it own code or changes the global context object.
19 // none - Import module without exported bindings.
23 When declaring multiple imports, a sorted list of import declarations make it easier for developers to read the code and find necessary imports later. This rule is purely a matter of style.
27 This rule checks all import declarations and verifies that all imports are first sorted by the used member syntax and then alphabetically by the first member or alias name.
29 The `--fix` option on the command line automatically fixes some problems reported by this rule: multiple members on a single line are automatically sorted (e.g. `import { b, a } from 'foo.js'` is corrected to `import { a, b } from 'foo.js'`), but multiple lines are not reordered.
33 This rule accepts an object with its properties as
35 * `ignoreCase` (default: `false`)
36 * `ignoreDeclarationSort` (default: `false`)
37 * `ignoreMemberSort` (default: `false`)
38 * `memberSyntaxSortOrder` (default: `["none", "all", "multiple", "single"]`); all 4 items must be present in the array, but you can change the order:
39 * `none` = import module without exported bindings.
40 * `all` = import all members provided by exported bindings.
41 * `multiple` = import multiple members.
42 * `single` = import single member.
43 * `allowSeparatedGroups` (default: `false`)
45 Default option settings are:
49 "sort-imports": ["error", {
51 "ignoreDeclarationSort": false,
52 "ignoreMemberSort": false,
53 "memberSyntaxSortOrder": ["none", "all", "multiple", "single"],
54 "allowSeparatedGroups": false
63 Examples of **correct** code for this rule when using default options:
66 /*eslint sort-imports: "error"*/
67 import 'module-without-export.js';
68 import * as bar from 'bar.js';
69 import * as foo from 'foo.js';
70 import {alpha, beta} from 'alpha.js';
71 import {delta, gamma} from 'delta.js';
72 import a from 'baz.js';
73 import b from 'qux.js';
75 /*eslint sort-imports: "error"*/
76 import a from 'foo.js';
77 import b from 'bar.js';
78 import c from 'baz.js';
80 /*eslint sort-imports: "error"*/
82 import * as bar from 'bar.js';
83 import {a, b} from 'baz.js';
84 import c from 'qux.js';
86 /*eslint sort-imports: "error"*/
87 import {a, b, c} from 'foo.js'
90 Examples of **incorrect** code for this rule when using default options:
93 /*eslint sort-imports: "error"*/
94 import b from 'foo.js';
95 import a from 'bar.js';
97 /*eslint sort-imports: "error"*/
98 import a from 'foo.js';
99 import A from 'bar.js';
101 /*eslint sort-imports: "error"*/
102 import {b, c} from 'foo.js';
103 import {a, b} from 'bar.js';
105 /*eslint sort-imports: "error"*/
106 import a from 'foo.js';
107 import {b, c} from 'bar.js';
109 /*eslint sort-imports: "error"*/
110 import a from 'foo.js';
111 import * as b from 'bar.js';
113 /*eslint sort-imports: "error"*/
114 import {b, a, c} from 'foo.js'
119 When `true` the rule ignores the case-sensitivity of the imports local name.
121 Examples of **incorrect** code for this rule with the `{ "ignoreCase": true }` option:
124 /*eslint sort-imports: ["error", { "ignoreCase": true }]*/
126 import B from 'foo.js';
127 import a from 'bar.js';
130 Examples of **correct** code for this rule with the `{ "ignoreCase": true }` option:
133 /*eslint sort-imports: ["error", { "ignoreCase": true }]*/
135 import a from 'foo.js';
136 import B from 'bar.js';
137 import c from 'baz.js';
142 ### `ignoreDeclarationSort`
144 Ignores the sorting of import declaration statements.
146 Examples of **incorrect** code for this rule with the default `{ "ignoreDeclarationSort": false }` option:
149 /*eslint sort-imports: ["error", { "ignoreDeclarationSort": false }]*/
150 import b from 'foo.js'
151 import a from 'bar.js'
154 Examples of **correct** code for this rule with the `{ "ignoreDeclarationSort": true }` option:
157 /*eslint sort-imports: ["error", { "ignoreDeclarationSort": true }]*/
158 import a from 'foo.js'
159 import b from 'bar.js'
163 /*eslint sort-imports: ["error", { "ignoreDeclarationSort": true }]*/
164 import b from 'foo.js'
165 import a from 'bar.js'
170 ### `ignoreMemberSort`
172 Ignores the member sorting within a `multiple` member import declaration.
174 Examples of **incorrect** code for this rule with the default `{ "ignoreMemberSort": false }` option:
177 /*eslint sort-imports: ["error", { "ignoreMemberSort": false }]*/
178 import {b, a, c} from 'foo.js'
181 Examples of **correct** code for this rule with the `{ "ignoreMemberSort": true }` option:
184 /*eslint sort-imports: ["error", { "ignoreMemberSort": true }]*/
185 import {b, a, c} from 'foo.js'
190 ### `memberSyntaxSortOrder`
192 There are four different styles and the default member syntax sort order is:
194 * `none` - import module without exported bindings.
195 * `all` - import all members provided by exported bindings.
196 * `multiple` - import multiple members.
197 * `single` - import single member.
199 All four options must be specified in the array, but you can customize their order.
201 Examples of **incorrect** code for this rule with the default `{ "memberSyntaxSortOrder": ["none", "all", "multiple", "single"] }` option:
204 /*eslint sort-imports: "error"*/
205 import a from 'foo.js';
206 import * as b from 'bar.js';
209 Examples of **correct** code for this rule with the `{ "memberSyntaxSortOrder": ['single', 'all', 'multiple', 'none'] }` option:
212 /*eslint sort-imports: ["error", { "memberSyntaxSortOrder": ['single', 'all', 'multiple', 'none'] }]*/
214 import a from 'foo.js';
215 import * as b from 'bar.js';
218 Examples of **correct** code for this rule with the `{ "memberSyntaxSortOrder": ['all', 'single', 'multiple', 'none'] }` option:
221 /*eslint sort-imports: ["error", { "memberSyntaxSortOrder": ['all', 'single', 'multiple', 'none'] }]*/
223 import * as foo from 'foo.js';
224 import z from 'zoo.js';
225 import {a, b} from 'foo.js';
228 Default is `["none", "all", "multiple", "single"]`.
230 ### `allowSeparatedGroups`
232 When `true` the rule checks the sorting of import declaration statements only for those that appear on consecutive lines.
234 In other words, a blank line or a comment line or line with any other statement after an import declaration statement will reset the sorting of import declaration statements.
236 Examples of **incorrect** code for this rule with the `{ "allowSeparatedGroups": true }` option:
239 /*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/
241 import b from 'foo.js';
242 import c from 'bar.js';
243 import a from 'baz.js';
246 Examples of **correct** code for this rule with the `{ "allowSeparatedGroups": true }` option:
249 /*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/
251 import b from 'foo.js';
252 import c from 'bar.js';
254 import a from 'baz.js';
258 /*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/
260 import b from 'foo.js';
261 import c from 'bar.js';
263 import a from 'baz.js';
267 /*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/
269 import b from 'foo.js';
270 import c from 'bar.js';
272 import a from 'baz.js';
277 ## When Not To Use It
279 This rule is a formatting preference and not following it won't negatively affect the quality of your code. If alphabetizing imports isn't a part of your coding standards, then you can leave this rule disabled.
283 * [sort-keys](sort-keys.md)
284 * [sort-vars](sort-vars.md)