| 1 | # import/no-anonymous-default-export
|
|---|
| 2 |
|
|---|
| 3 | <!-- end auto-generated rule header -->
|
|---|
| 4 |
|
|---|
| 5 | Reports if a module's default export is unnamed. This includes several types of unnamed data types; literals, object expressions, arrays, anonymous functions, arrow functions, and anonymous class declarations.
|
|---|
| 6 |
|
|---|
| 7 | Ensuring that default exports are named helps improve the grepability of the codebase by encouraging the re-use of the same identifier for the module's default export at its declaration site and at its import sites.
|
|---|
| 8 |
|
|---|
| 9 | ## Options
|
|---|
| 10 |
|
|---|
| 11 | By default, all types of anonymous default exports are forbidden, but any types can be selectively allowed by toggling them on in the options.
|
|---|
| 12 |
|
|---|
| 13 | The complete default configuration looks like this.
|
|---|
| 14 |
|
|---|
| 15 | ```js
|
|---|
| 16 | "import/no-anonymous-default-export": ["error", {
|
|---|
| 17 | "allowArray": false,
|
|---|
| 18 | "allowArrowFunction": false,
|
|---|
| 19 | "allowAnonymousClass": false,
|
|---|
| 20 | "allowAnonymousFunction": false,
|
|---|
| 21 | "allowCallExpression": true, // The true value here is for backward compatibility
|
|---|
| 22 | "allowNew": false,
|
|---|
| 23 | "allowLiteral": false,
|
|---|
| 24 | "allowObject": false
|
|---|
| 25 | }]
|
|---|
| 26 | ```
|
|---|
| 27 |
|
|---|
| 28 | ## Rule Details
|
|---|
| 29 |
|
|---|
| 30 | ### Fail
|
|---|
| 31 |
|
|---|
| 32 | ```js
|
|---|
| 33 | export default []
|
|---|
| 34 |
|
|---|
| 35 | export default () => {}
|
|---|
| 36 |
|
|---|
| 37 | export default class {}
|
|---|
| 38 |
|
|---|
| 39 | export default function () {}
|
|---|
| 40 |
|
|---|
| 41 | /* eslint import/no-anonymous-default-export: [2, {"allowCallExpression": false}] */
|
|---|
| 42 | export default foo(bar)
|
|---|
| 43 |
|
|---|
| 44 | export default 123
|
|---|
| 45 |
|
|---|
| 46 | export default {}
|
|---|
| 47 |
|
|---|
| 48 | export default new Foo()
|
|---|
| 49 | ```
|
|---|
| 50 |
|
|---|
| 51 | ### Pass
|
|---|
| 52 |
|
|---|
| 53 | ```js
|
|---|
| 54 | const foo = 123
|
|---|
| 55 | export default foo
|
|---|
| 56 |
|
|---|
| 57 | export default class MyClass() {}
|
|---|
| 58 |
|
|---|
| 59 | export default function foo() {}
|
|---|
| 60 |
|
|---|
| 61 | /* eslint import/no-anonymous-default-export: [2, {"allowArray": true}] */
|
|---|
| 62 | export default []
|
|---|
| 63 |
|
|---|
| 64 | /* eslint import/no-anonymous-default-export: [2, {"allowArrowFunction": true}] */
|
|---|
| 65 | export default () => {}
|
|---|
| 66 |
|
|---|
| 67 | /* eslint import/no-anonymous-default-export: [2, {"allowAnonymousClass": true}] */
|
|---|
| 68 | export default class {}
|
|---|
| 69 |
|
|---|
| 70 | /* eslint import/no-anonymous-default-export: [2, {"allowAnonymousFunction": true}] */
|
|---|
| 71 | export default function () {}
|
|---|
| 72 |
|
|---|
| 73 | export default foo(bar)
|
|---|
| 74 |
|
|---|
| 75 | /* eslint import/no-anonymous-default-export: [2, {"allowLiteral": true}] */
|
|---|
| 76 | export default 123
|
|---|
| 77 |
|
|---|
| 78 | /* eslint import/no-anonymous-default-export: [2, {"allowObject": true}] */
|
|---|
| 79 | export default {}
|
|---|
| 80 |
|
|---|
| 81 | /* eslint import/no-anonymous-default-export: [2, {"allowNew": true}] */
|
|---|
| 82 | export default new Foo()
|
|---|
| 83 | ```
|
|---|