| [9af201e] | 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 |
|
|---|
| 7 | This rule reports any imports that come after non-import
|
|---|
| 8 | statements.
|
|---|
| 9 |
|
|---|
| 10 | ## Rule Details
|
|---|
| 11 |
|
|---|
| 12 | ```js
|
|---|
| 13 | import foo from './foo'
|
|---|
| 14 |
|
|---|
| 15 | // some module-level initializer
|
|---|
| 16 | initWith(foo)
|
|---|
| 17 |
|
|---|
| 18 | import bar from './bar' // <- reported
|
|---|
| 19 | ```
|
|---|
| 20 |
|
|---|
| 21 | Providing `absolute-first` as an option will report any absolute imports (i.e.
|
|---|
| 22 | packages) that come after any relative imports:
|
|---|
| 23 |
|
|---|
| 24 | ```js
|
|---|
| 25 | import foo from 'foo'
|
|---|
| 26 | import bar from './bar'
|
|---|
| 27 |
|
|---|
| 28 | import * as _ from 'lodash' // <- reported
|
|---|
| 29 | ```
|
|---|
| 30 |
|
|---|
| 31 | If you really want import type ordering, check out [`import/order`].
|
|---|
| 32 |
|
|---|
| 33 | Notably, `import`s are hoisted, which means the imported modules will be evaluated
|
|---|
| 34 | before any of the statements interspersed between them. Keeping all `import`s together
|
|---|
| 35 | at the top of the file may prevent surprises resulting from this part of the spec.
|
|---|
| 36 |
|
|---|
| 37 | ### On directives
|
|---|
| 38 |
|
|---|
| 39 | Directives are allowed as long as they occur strictly before any `import` declarations,
|
|---|
| 40 | as follows:
|
|---|
| 41 |
|
|---|
| 42 | ```js
|
|---|
| 43 | 'use super-mega-strict'
|
|---|
| 44 |
|
|---|
| 45 | import { suchFoo } from 'lame-fake-module-name' // no report here
|
|---|
| 46 | ```
|
|---|
| 47 |
|
|---|
| 48 | A directive in this case is assumed to be a single statement that contains only
|
|---|
| 49 | a literal string-valued expression.
|
|---|
| 50 |
|
|---|
| 51 | `'use strict'` would be a good example, except that [modules are always in strict
|
|---|
| 52 | mode](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 |
|
|---|
| 55 | Given that, see [#255] for the reasoning.
|
|---|
| 56 |
|
|---|
| 57 | ### With Fixer
|
|---|
| 58 |
|
|---|
| 59 | This rule contains a fixer to reorder in-body import to top, the following criteria applied:
|
|---|
| 60 |
|
|---|
| 61 | 1. Never re-order relative to each other, even if `absolute-first` is set.
|
|---|
| 62 | 2. 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 |
|
|---|
| 66 | If you don't mind imports being sprinkled throughout, you may not want to
|
|---|
| 67 | enable 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
|
|---|