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.
28 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.
30 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.
34 This rule accepts an object with its properties as
36 * `ignoreCase` (default: `false`)
37 * `ignoreDeclarationSort` (default: `false`)
38 * `ignoreMemberSort` (default: `false`)
39 * `memberSyntaxSortOrder` (default: `["none", "all", "multiple", "single"]`); all 4 items must be present in the array, but you can change the order:
40 * `none` = import module without exported bindings.
41 * `all` = import all members provided by exported bindings.
42 * `multiple` = import multiple members.
43 * `single` = import single member.
45 Default option settings are:
49 "sort-imports": ["error", {
51 "ignoreDeclarationSort": false,
52 "ignoreMemberSort": false,
53 "memberSyntaxSortOrder": ["none", "all", "multiple", "single"]
62 Examples of **correct** code for this rule when using default options:
65 /*eslint sort-imports: "error"*/
66 import 'module-without-export.js';
67 import * as bar from 'bar.js';
68 import * as foo from 'foo.js';
69 import {alpha, beta} from 'alpha.js';
70 import {delta, gamma} from 'delta.js';
71 import a from 'baz.js';
72 import b from 'qux.js';
74 /*eslint sort-imports: "error"*/
75 import a from 'foo.js';
76 import b from 'bar.js';
77 import c from 'baz.js';
79 /*eslint sort-imports: "error"*/
81 import * as bar from 'bar.js';
82 import {a, b} from 'baz.js';
83 import c from 'qux.js';
85 /*eslint sort-imports: "error"*/
86 import {a, b, c} from 'foo.js'
89 Examples of **incorrect** code for this rule when using default options:
92 /*eslint sort-imports: "error"*/
93 import b from 'foo.js';
94 import a from 'bar.js';
96 /*eslint sort-imports: "error"*/
97 import a from 'foo.js';
98 import A from 'bar.js';
100 /*eslint sort-imports: "error"*/
101 import {b, c} from 'foo.js';
102 import {a, b} from 'bar.js';
104 /*eslint sort-imports: "error"*/
105 import a from 'foo.js';
106 import {b, c} from 'bar.js';
108 /*eslint sort-imports: "error"*/
109 import a from 'foo.js';
110 import * as b from 'bar.js';
112 /*eslint sort-imports: "error"*/
113 import {b, a, c} from 'foo.js'
118 When `true` the rule ignores the case-sensitivity of the imports local name.
120 Examples of **incorrect** code for this rule with the `{ "ignoreCase": true }` option:
123 /*eslint sort-imports: ["error", { "ignoreCase": true }]*/
125 import B from 'foo.js';
126 import a from 'bar.js';
129 Examples of **correct** code for this rule with the `{ "ignoreCase": true }` option:
132 /*eslint sort-imports: ["error", { "ignoreCase": true }]*/
134 import a from 'foo.js';
135 import B from 'bar.js';
136 import c from 'baz.js';
141 ### `ignoreDeclarationSort`
143 Ignores the sorting of import declaration statements.
145 Examples of **incorrect** code for this rule with the default `{ "ignoreDeclarationSort": false }` option:
148 /*eslint sort-imports: ["error", { "ignoreDeclarationSort": false }]*/
149 import b from 'foo.js'
150 import a from 'bar.js'
153 Examples of **correct** code for this rule with the `{ "ignoreDeclarationSort": true }` option:
156 /*eslint sort-imports: ["error", { "ignoreDeclarationSort": true }]*/
157 import a from 'foo.js'
158 import b from 'bar.js'
162 /*eslint sort-imports: ["error", { "ignoreDeclarationSort": true }]*/
163 import b from 'foo.js'
164 import a from 'bar.js'
169 ### `ignoreMemberSort`
171 Ignores the member sorting within a `multiple` member import declaration.
173 Examples of **incorrect** code for this rule with the default `{ "ignoreMemberSort": false }` option:
176 /*eslint sort-imports: ["error", { "ignoreMemberSort": false }]*/
177 import {b, a, c} from 'foo.js'
180 Examples of **correct** code for this rule with the `{ "ignoreMemberSort": true }` option:
183 /*eslint sort-imports: ["error", { "ignoreMemberSort": true }]*/
184 import {b, a, c} from 'foo.js'
189 ### `memberSyntaxSortOrder`
191 There are four different styles and the default member syntax sort order is:
193 * `none` - import module without exported bindings.
194 * `all` - import all members provided by exported bindings.
195 * `multiple` - import multiple members.
196 * `single` - import single member.
198 All four options must be specified in the array, but you can customize their order.
200 Examples of **incorrect** code for this rule with the default `{ "memberSyntaxSortOrder": ["none", "all", "multiple", "single"] }` option:
203 /*eslint sort-imports: "error"*/
204 import a from 'foo.js';
205 import * as b from 'bar.js';
208 Examples of **correct** code for this rule with the `{ "memberSyntaxSortOrder": ['single', 'all', 'multiple', 'none'] }` option:
211 /*eslint sort-imports: ["error", { "memberSyntaxSortOrder": ['single', 'all', 'multiple', 'none'] }]*/
213 import a from 'foo.js';
214 import * as b from 'bar.js';
217 Examples of **correct** code for this rule with the `{ "memberSyntaxSortOrder": ['all', 'single', 'multiple', 'none'] }` option:
220 /*eslint sort-imports: ["error", { "memberSyntaxSortOrder": ['all', 'single', 'multiple', 'none'] }]*/
222 import * as foo from 'foo.js';
223 import z from 'zoo.js';
224 import {a, b} from 'foo.js';
227 Default is `["none", "all", "multiple", "single"]`.
229 ## When Not To Use It
231 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.
235 * [sort-keys](sort-keys.md)
236 * [sort-vars](sort-vars.md)