source: frontend/node_modules/eslint-plugin-import/docs/rules/first.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 2.2 KB
Line 
1# import/first
2
3🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
4
5<!-- end auto-generated rule header -->
6
7This rule reports any imports that come after non-import
8statements.
9
10## Rule Details
11
12```js
13import foo from './foo'
14
15// some module-level initializer
16initWith(foo)
17
18import bar from './bar' // <- reported
19```
20
21Providing `absolute-first` as an option will report any absolute imports (i.e.
22packages) that come after any relative imports:
23
24```js
25import foo from 'foo'
26import bar from './bar'
27
28import * as _ from 'lodash' // <- reported
29```
30
31If you really want import type ordering, check out [`import/order`].
32
33Notably, `import`s are hoisted, which means the imported modules will be evaluated
34before any of the statements interspersed between them. Keeping all `import`s together
35at the top of the file may prevent surprises resulting from this part of the spec.
36
37### On directives
38
39Directives are allowed as long as they occur strictly before any `import` declarations,
40as follows:
41
42```js
43'use super-mega-strict'
44
45import { suchFoo } from 'lame-fake-module-name' // no report here
46```
47
48A directive in this case is assumed to be a single statement that contains only
49a literal string-valued expression.
50
51`'use strict'` would be a good example, except that [modules are always in strict
52mode](https://262.ecma-international.org/6.0/#sec-strict-mode-code) so it would be surprising to see a `'use strict'` sharing a file with `import`s and
53`export`s.
54
55Given that, see [#255] for the reasoning.
56
57### With Fixer
58
59This rule contains a fixer to reorder in-body import to top, the following criteria applied:
60
611. Never re-order relative to each other, even if `absolute-first` is set.
622. If an import creates an identifier, and that identifier is referenced at module level *before* the import itself, that won't be re-ordered.
63
64## When Not To Use It
65
66If you don't mind imports being sprinkled throughout, you may not want to
67enable this rule.
68
69## Further Reading
70
71 - [`import/order`]: a major step up from `absolute-first`
72 - Issue [#255]
73
74[`import/order`]: ./order.md
75[#255]: https://github.com/import-js/eslint-plugin-import/issues/255
Note: See TracBrowser for help on using the repository browser.