| 1 | # import/group-exports
|
|---|
| 2 |
|
|---|
| 3 | <!-- end auto-generated rule header -->
|
|---|
| 4 |
|
|---|
| 5 | Reports when named exports are not grouped together in a single `export` declaration or when multiple assignments to CommonJS `module.exports` or `exports` object are present in a single file.
|
|---|
| 6 |
|
|---|
| 7 | **Rationale:** An `export` declaration or `module.exports` assignment can appear anywhere in the code. By requiring a single export declaration all your exports will remain at one place, making it easier to see what exports a module provides.
|
|---|
| 8 |
|
|---|
| 9 | ## Rule Details
|
|---|
| 10 |
|
|---|
| 11 | This rule warns whenever a single file contains multiple named export declarations or multiple assignments to `module.exports` (or `exports`).
|
|---|
| 12 |
|
|---|
| 13 | ### Valid
|
|---|
| 14 |
|
|---|
| 15 | ```js
|
|---|
| 16 | // A single named export declaration -> ok
|
|---|
| 17 | export const valid = true
|
|---|
| 18 | ```
|
|---|
| 19 |
|
|---|
| 20 | ```js
|
|---|
| 21 | const first = true
|
|---|
| 22 | const second = true
|
|---|
| 23 |
|
|---|
| 24 | // A single named export declaration -> ok
|
|---|
| 25 | export {
|
|---|
| 26 | first,
|
|---|
| 27 | second,
|
|---|
| 28 | }
|
|---|
| 29 | ```
|
|---|
| 30 |
|
|---|
| 31 | ```js
|
|---|
| 32 | // Aggregating exports -> ok
|
|---|
| 33 | export { default as module1 } from 'module-1'
|
|---|
| 34 | export { default as module2 } from 'module-2'
|
|---|
| 35 | ```
|
|---|
| 36 |
|
|---|
| 37 | ```js
|
|---|
| 38 | // A single exports assignment -> ok
|
|---|
| 39 | module.exports = {
|
|---|
| 40 | first: true,
|
|---|
| 41 | second: true
|
|---|
| 42 | }
|
|---|
| 43 | ```
|
|---|
| 44 |
|
|---|
| 45 | ```js
|
|---|
| 46 | const first = true
|
|---|
| 47 | const second = true
|
|---|
| 48 |
|
|---|
| 49 | // A single exports assignment -> ok
|
|---|
| 50 | module.exports = {
|
|---|
| 51 | first,
|
|---|
| 52 | second,
|
|---|
| 53 | }
|
|---|
| 54 | ```
|
|---|
| 55 |
|
|---|
| 56 | ```js
|
|---|
| 57 | function test() {}
|
|---|
| 58 | test.property = true
|
|---|
| 59 | test.another = true
|
|---|
| 60 |
|
|---|
| 61 | // A single exports assignment -> ok
|
|---|
| 62 | module.exports = test
|
|---|
| 63 | ```
|
|---|
| 64 |
|
|---|
| 65 | ```ts
|
|---|
| 66 | const first = true;
|
|---|
| 67 | type firstType = boolean
|
|---|
| 68 |
|
|---|
| 69 | // A single named export declaration (type exports handled separately) -> ok
|
|---|
| 70 | export {first}
|
|---|
| 71 | export type {firstType}
|
|---|
| 72 | ```
|
|---|
| 73 |
|
|---|
| 74 | ### Invalid
|
|---|
| 75 |
|
|---|
| 76 | ```js
|
|---|
| 77 | // Multiple named export statements -> not ok!
|
|---|
| 78 | export const first = true
|
|---|
| 79 | export const second = true
|
|---|
| 80 | ```
|
|---|
| 81 |
|
|---|
| 82 | ```js
|
|---|
| 83 | // Aggregating exports from the same module -> not ok!
|
|---|
| 84 | export { module1 } from 'module-1'
|
|---|
| 85 | export { module2 } from 'module-1'
|
|---|
| 86 | ```
|
|---|
| 87 |
|
|---|
| 88 | ```js
|
|---|
| 89 | // Multiple exports assignments -> not ok!
|
|---|
| 90 | exports.first = true
|
|---|
| 91 | exports.second = true
|
|---|
| 92 | ```
|
|---|
| 93 |
|
|---|
| 94 | ```js
|
|---|
| 95 | // Multiple exports assignments -> not ok!
|
|---|
| 96 | module.exports = {}
|
|---|
| 97 | module.exports.first = true
|
|---|
| 98 | ```
|
|---|
| 99 |
|
|---|
| 100 | ```js
|
|---|
| 101 | // Multiple exports assignments -> not ok!
|
|---|
| 102 | module.exports = () => {}
|
|---|
| 103 | module.exports.first = true
|
|---|
| 104 | module.exports.second = true
|
|---|
| 105 | ```
|
|---|
| 106 |
|
|---|
| 107 | ```ts
|
|---|
| 108 | type firstType = boolean
|
|---|
| 109 | type secondType = any
|
|---|
| 110 |
|
|---|
| 111 | // Multiple named type export statements -> not ok!
|
|---|
| 112 | export type {firstType}
|
|---|
| 113 | export type {secondType}
|
|---|
| 114 | ```
|
|---|
| 115 |
|
|---|
| 116 | ## When Not To Use It
|
|---|
| 117 |
|
|---|
| 118 | If you do not mind having your exports spread across the file, you can safely turn this rule off.
|
|---|