Skip to content

import/no-anonymous-default-export Style

What it does

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.

Why is this bad?

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.

Examples

Examples of incorrect code for this rule:

js
export default [];
export default () => {};
export default class {};
export default function() {};
export default foo(bar);
export default 123;
export default {};
export default new Foo();
export default `foo`;
export default /^123/;

Examples of correct code for this rule:

js
const foo = 123;
export default foo;
export default function foo() {};
export default class MyClass {};
export default function foo() {};
export default foo(bar);
/* eslint import/no-anonymous-default-export: ['error', {"allowLiteral": true}] */
export default 123;
/* eslint import/no-anonymous-default-export: ['error, {"allowArray": true}] */
export default []
/* eslint import/no-anonymous-default-export: ['error, {"allowArrowFunction": true}] */
export default () => {};
/* eslint import/no-anonymous-default-export: ['error, {"allowAnonymousClass": true}] */
export default class {};
/* eslint import/no-anonymous-default-export: ['error, {"allowAnonymousFunction": true}] */
export default function() {};
/* eslint import/no-anonymous-default-export: ['error, {"allowObject": true}] */
export default {};
/* eslint import/no-anonymous-default-export: ['error, {"allowNew": true}] */
export default new Foo();
/* eslint import/no-anonymous-default-export: ['error, {"allowCallExpression": true}] */
export default foo(bar);

By default, all types of anonymous default exports are forbidden, but any types can be selectively allowed by toggling them on in the options.

Configuration

This rule accepts a configuration object with the following properties:

allowAnonymousClass

type: boolean

default: false

Allow anonymous class as default export.

allowAnonymousFunction

type: boolean

default: false

Allow anonymous function as default export.

allowArray

type: boolean

default: false

Allow anonymous array as default export.

allowArrowFunction

type: boolean

default: false

Allow anonymous arrow function as default export.

allowCallExpression

type: boolean

default: true

Allow anonymous call expression as default export.

allowLiteral

type: boolean

default: false

Allow anonymous literal as default export.

allowNew

type: boolean

default: false

Allow anonymous new expression as default export.

allowObject

type: boolean

default: false

Allow anonymous object as default export.

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["import"],
  "rules": {
    "import/no-anonymous-default-export": "error"
  }
}
bash
oxlint --deny import/no-anonymous-default-export --import-plugin

References