|
2 | 2 |
|
3 | 3 | <!-- end auto-generated rule header --> |
4 | 4 |
|
5 | | -When there is only a single export from a module, prefer using default export over named export. |
| 5 | +In exporting files, this rule checks if there is default export or not. |
6 | 6 |
|
7 | 7 | ## Rule Details |
8 | 8 |
|
| 9 | +##### rule schema: |
| 10 | + |
| 11 | +```javascript |
| 12 | +"import/prefer-default-export": [ |
| 13 | + ( "off" | "warn" | "error" ), |
| 14 | + { "target": "single" | "any" } // default is "single" |
| 15 | +] |
| 16 | +``` |
| 17 | + |
| 18 | +### Config Options |
| 19 | + |
| 20 | +There are two options available: `single` and `any`. By default, if you do not specify the option, rule will assume it is `single`. |
| 21 | + |
| 22 | +#### single |
| 23 | + |
| 24 | +**Definition**: When there is only a single export from a module, prefer using default export over named export. |
| 25 | + |
| 26 | +How to setup config file for this rule: |
| 27 | + |
| 28 | +```javascript |
| 29 | +// you can manually specify it |
| 30 | +"rules": { |
| 31 | + "import/prefer-default-export": [ |
| 32 | + ( "off" | "warn" | "error" ), |
| 33 | + { "target": "single" } |
| 34 | + ] |
| 35 | +} |
| 36 | + |
| 37 | +// config setup below will also work |
| 38 | +"rules": { |
| 39 | + "import/prefer-default-export": "off" | "warn" | "error" |
| 40 | +} |
| 41 | +``` |
| 42 | + |
9 | 43 | The following patterns are considered warnings: |
10 | 44 |
|
11 | 45 | ```javascript |
@@ -58,3 +92,95 @@ export { foo as default } |
58 | 92 | // Any batch export will disable this rule. The remote module is not inspected. |
59 | 93 | export * from './other-module' |
60 | 94 | ``` |
| 95 | + |
| 96 | +#### any |
| 97 | + |
| 98 | +**Definition**: any exporting file must contain a default export. |
| 99 | + |
| 100 | +How to setup config file for this rule: |
| 101 | + |
| 102 | +```javascript |
| 103 | +// you have to manually specify it |
| 104 | +"rules": { |
| 105 | + "import/prefer-default-export": [ |
| 106 | + ( "off" | "warn" | "error" ), |
| 107 | + { "target": "any" } |
| 108 | + ] |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | + |
| 113 | +The following patterns are *not* considered warnings: |
| 114 | + |
| 115 | +```javascript |
| 116 | +// good1.js |
| 117 | + |
| 118 | +//has default export |
| 119 | +export default function bar() {}; |
| 120 | +``` |
| 121 | + |
| 122 | +```javascript |
| 123 | +// good2.js |
| 124 | + |
| 125 | +// has default export |
| 126 | +let foo; |
| 127 | +export { foo as default } |
| 128 | +``` |
| 129 | + |
| 130 | +```javascript |
| 131 | +// good3.js |
| 132 | + |
| 133 | +//contains multiple exports AND default export |
| 134 | +export const a = 5; |
| 135 | +export function bar(){}; |
| 136 | +let foo; |
| 137 | +export { foo as default } |
| 138 | +``` |
| 139 | + |
| 140 | +```javascript |
| 141 | +// good4.js |
| 142 | + |
| 143 | +// does not contain any exports => file is not checked by the rule |
| 144 | +import * as foo from './foo'; |
| 145 | +``` |
| 146 | + |
| 147 | +```javascript |
| 148 | +// export-star.js |
| 149 | + |
| 150 | +// Any batch export will disable this rule. The remote module is not inspected. |
| 151 | +export * from './other-module' |
| 152 | +``` |
| 153 | + |
| 154 | +The following patterns are considered warnings: |
| 155 | + |
| 156 | +```javascript |
| 157 | +// bad1.js |
| 158 | + |
| 159 | +//has 2 named exports, but no default export |
| 160 | +export const foo = 'foo'; |
| 161 | +export const bar = 'bar'; |
| 162 | +``` |
| 163 | + |
| 164 | +```javascript |
| 165 | +// bad2.js |
| 166 | + |
| 167 | +// does not have default export |
| 168 | +let foo, bar; |
| 169 | +export { foo, bar } |
| 170 | +``` |
| 171 | + |
| 172 | +```javascript |
| 173 | +// bad3.js |
| 174 | + |
| 175 | +// does not have default export |
| 176 | +export { a, b } from "foo.js" |
| 177 | +``` |
| 178 | + |
| 179 | +```javascript |
| 180 | +// bad4.js |
| 181 | + |
| 182 | +// does not have default export |
| 183 | +let item; |
| 184 | +export const foo = item; |
| 185 | +export { item }; |
| 186 | +``` |
0 commit comments