| 1 | # import/newline-after-import
|
|---|
| 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 | Enforces having one or more empty lines after the last top-level import statement or require call.
|
|---|
| 8 |
|
|---|
| 9 | ## Rule Details
|
|---|
| 10 |
|
|---|
| 11 | This rule supports the following options:
|
|---|
| 12 |
|
|---|
| 13 | - `count` which sets the number of newlines that are enforced after the last top-level import statement or require call. This option defaults to `1`.
|
|---|
| 14 |
|
|---|
| 15 | - `exactCount` which enforce the exact numbers of newlines that is mentioned in `count`. This option defaults to `false`.
|
|---|
| 16 |
|
|---|
| 17 | - `considerComments` which enforces the rule on comments after the last import-statement as well when set to true. This option defaults to `false`.
|
|---|
| 18 |
|
|---|
| 19 | Valid:
|
|---|
| 20 |
|
|---|
| 21 | ```js
|
|---|
| 22 | import defaultExport from './foo';
|
|---|
| 23 |
|
|---|
| 24 | const FOO = 'BAR';
|
|---|
| 25 | ```
|
|---|
| 26 |
|
|---|
| 27 | ```js
|
|---|
| 28 | import defaultExport from './foo';
|
|---|
| 29 | import { bar } from 'bar-lib';
|
|---|
| 30 |
|
|---|
| 31 | const FOO = 'BAR';
|
|---|
| 32 | ```
|
|---|
| 33 |
|
|---|
| 34 | ```js
|
|---|
| 35 | const FOO = require('./foo');
|
|---|
| 36 | const BAR = require('./bar');
|
|---|
| 37 |
|
|---|
| 38 | const BAZ = 1;
|
|---|
| 39 | ```
|
|---|
| 40 |
|
|---|
| 41 | Invalid:
|
|---|
| 42 |
|
|---|
| 43 | ```js
|
|---|
| 44 | import * as foo from 'foo'
|
|---|
| 45 | const FOO = 'BAR';
|
|---|
| 46 | ```
|
|---|
| 47 |
|
|---|
| 48 | ```js
|
|---|
| 49 | import * as foo from 'foo';
|
|---|
| 50 | const FOO = 'BAR';
|
|---|
| 51 |
|
|---|
| 52 | import { bar } from 'bar-lib';
|
|---|
| 53 | ```
|
|---|
| 54 |
|
|---|
| 55 | ```js
|
|---|
| 56 | const FOO = require('./foo');
|
|---|
| 57 | const BAZ = 1;
|
|---|
| 58 | const BAR = require('./bar');
|
|---|
| 59 | ```
|
|---|
| 60 |
|
|---|
| 61 | With `count` set to `2` this will be considered valid:
|
|---|
| 62 |
|
|---|
| 63 | ```js
|
|---|
| 64 | import defaultExport from './foo';
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 | const FOO = 'BAR';
|
|---|
| 68 | ```
|
|---|
| 69 |
|
|---|
| 70 | ```js
|
|---|
| 71 | import defaultExport from './foo';
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | const FOO = 'BAR';
|
|---|
| 76 | ```
|
|---|
| 77 |
|
|---|
| 78 | With `count` set to `2` these will be considered invalid:
|
|---|
| 79 |
|
|---|
| 80 | ```js
|
|---|
| 81 | import defaultExport from './foo';
|
|---|
| 82 | const FOO = 'BAR';
|
|---|
| 83 | ```
|
|---|
| 84 |
|
|---|
| 85 | ```js
|
|---|
| 86 | import defaultExport from './foo';
|
|---|
| 87 |
|
|---|
| 88 | const FOO = 'BAR';
|
|---|
| 89 | ```
|
|---|
| 90 |
|
|---|
| 91 | With `count` set to `2` and `exactCount` set to `true` this will be considered valid:
|
|---|
| 92 |
|
|---|
| 93 | ```js
|
|---|
| 94 | import defaultExport from './foo';
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 | const FOO = 'BAR';
|
|---|
| 98 | ```
|
|---|
| 99 |
|
|---|
| 100 | With `count` set to `2` and `exactCount` set to `true` these will be considered invalid:
|
|---|
| 101 |
|
|---|
| 102 | ```js
|
|---|
| 103 | import defaultExport from './foo';
|
|---|
| 104 | const FOO = 'BAR';
|
|---|
| 105 | ```
|
|---|
| 106 |
|
|---|
| 107 | ```js
|
|---|
| 108 | import defaultExport from './foo';
|
|---|
| 109 |
|
|---|
| 110 | const FOO = 'BAR';
|
|---|
| 111 | ```
|
|---|
| 112 |
|
|---|
| 113 | ```js
|
|---|
| 114 | import defaultExport from './foo';
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 | const FOO = 'BAR';
|
|---|
| 119 | ```
|
|---|
| 120 |
|
|---|
| 121 | ```js
|
|---|
| 122 | import defaultExport from './foo';
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 | const FOO = 'BAR';
|
|---|
| 128 | ```
|
|---|
| 129 |
|
|---|
| 130 | With `considerComments` set to `false` this will be considered valid:
|
|---|
| 131 |
|
|---|
| 132 | ```js
|
|---|
| 133 | import defaultExport from './foo'
|
|---|
| 134 | // some comment here.
|
|---|
| 135 | const FOO = 'BAR'
|
|---|
| 136 | ```
|
|---|
| 137 |
|
|---|
| 138 | With `considerComments` set to `true` this will be considered valid:
|
|---|
| 139 |
|
|---|
| 140 | ```js
|
|---|
| 141 | import defaultExport from './foo'
|
|---|
| 142 |
|
|---|
| 143 | // some comment here.
|
|---|
| 144 | const FOO = 'BAR'
|
|---|
| 145 | ```
|
|---|
| 146 |
|
|---|
| 147 | With `considerComments` set to `true` this will be considered invalid:
|
|---|
| 148 |
|
|---|
| 149 | ```js
|
|---|
| 150 | import defaultExport from './foo'
|
|---|
| 151 | // some comment here.
|
|---|
| 152 | const FOO = 'BAR'
|
|---|
| 153 | ```
|
|---|
| 154 |
|
|---|
| 155 | ## Example options usage
|
|---|
| 156 |
|
|---|
| 157 | ```json
|
|---|
| 158 | {
|
|---|
| 159 | "rules": {
|
|---|
| 160 | "import/newline-after-import": ["error", { "count": 2 }]
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 | ```
|
|---|
| 164 |
|
|---|
| 165 | ## When Not To Use It
|
|---|
| 166 |
|
|---|
| 167 | If you like to visually group module imports with its usage, you don't want to use this rule.
|
|---|