useConsistentDefaultExportStyle (JavaScript)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useConsistentDefaultExportStyle - This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Inspired from
unicorn/default-export-style
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useConsistentDefaultExportStyle": "error" } } }}Description
Section titled “Description”Enforce a consistent style for default-exported functions and classes.
A function or class can be declared inside the export default statement,
or declared on its own and then exported by name with export default name;.
Using one style across a project makes default exports easier to find and read.
By default, the rule requires functions and classes to be declared inside the export default statement.
Use the functions and classes options to require the separate style instead, or to turn off the check.
The rule only checks a declaration that comes directly before export default with its name.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”function foo() {}export default foo;code-block.js:2:1 lint/nursery/useConsistentDefaultExportStyle FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This default-exported function is declared separately from its export default statement.
1 │ function foo() {}
> 2 │ export default foo;
│ ^^^^^^^^^^^^^^^^^^^
3 │
ℹ The function is declared here.
> 1 │ function foo() {}
│ ^^^
2 │ export default foo;
3 │
ℹ This project declares default-exported functions inside the export default statement, so that all default exports look the same.
ℹ Move the declaration into the export default statement.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Declare the function inside the export default statement.
1 │ - function·foo()·{}
2 │ - export·default·foo;
1 │ + export·default·function·foo()·{}
3 2 │
const foo = () => {};export default foo;code-block.js:2:1 lint/nursery/useConsistentDefaultExportStyle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This default-exported function is declared separately from its export default statement.
1 │ const foo = () => {};
> 2 │ export default foo;
│ ^^^^^^^^^^^^^^^^^^^
3 │
ℹ The function is declared here.
> 1 │ const foo = () => {};
│ ^^^
2 │ export default foo;
3 │
ℹ This project declares default-exported functions inside the export default statement, so that all default exports look the same.
ℹ Rewrite the arrow function as a function declaration inside the export default statement, like export default function foo() {}.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
class Foo {}export default Foo;code-block.js:2:1 lint/nursery/useConsistentDefaultExportStyle FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This default-exported class is declared separately from its export default statement.
1 │ class Foo {}
> 2 │ export default Foo;
│ ^^^^^^^^^^^^^^^^^^^
3 │
ℹ The class is declared here.
> 1 │ class Foo {}
│ ^^^
2 │ export default Foo;
3 │
ℹ This project declares default-exported classes inside the export default statement, so that all default exports look the same.
ℹ Move the declaration into the export default statement.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Declare the class inside the export default statement.
1 │ - class·Foo·{}
2 │ - export·default·Foo;
1 │ + export·default·class·Foo·{}
3 2 │
export default function foo() {}
const bar = () => {};export { bar };export default class Foo {}Options
Section titled “Options”functions
Section titled “functions”Type: "inline" | "separate" | "ignore"
Default: "inline"
"inline"requires functions to be declared inside theexport defaultstatement, likeexport default function foo() {}."separate"requires functions to be declared asconstarrow functions, then exported by name, likeconst foo = () => {};followed byexport default foo;. Generator functions (function*) are ignored, because an arrow function can’t be a generator."ignore"turns off the check for functions.
The following examples use "separate":
{ "linter": { "rules": { "nursery": { "useConsistentDefaultExportStyle": { "level": "on", "options": { "functions": "separate" } } } } }}Invalid
Section titled “Invalid”export default function foo() {}code-block.js:1:1 lint/nursery/useConsistentDefaultExportStyle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This function is declared inside its export default statement.
> 1 │ export default function foo() {}
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ This project declares default-exported functions separately, so that all default exports look the same.
ℹ Declare the function as a const arrow function, then export it with export default and its name.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
function foo() {}export default foo;code-block.js:2:1 lint/nursery/useConsistentDefaultExportStyle ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This default-exported function is declared with the function keyword.
1 │ function foo() {}
> 2 │ export default foo;
│ ^^^^^^^^^^^^^^^^^^^
3 │
ℹ The function is declared here.
> 1 │ function foo() {}
│ ^^^
2 │ export default foo;
3 │
ℹ This project declares default-exported functions as const arrow functions, so that all default exports look the same.
ℹ Rewrite the function as a const arrow function, like const foo = () => {};.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
const foo = () => {};export default foo;classes
Section titled “classes”Type: "inline" | "separate" | "ignore"
Default: "inline"
"inline"requires classes to be declared inside theexport defaultstatement, likeexport default class Foo {}."separate"requires classes to be declared on their own, then exported by name, likeclass Foo {}followed byexport default Foo;."ignore"turns off the check for classes.
The following examples use "separate":
{ "linter": { "rules": { "nursery": { "useConsistentDefaultExportStyle": { "level": "on", "options": { "classes": "separate" } } } } }}Invalid
Section titled “Invalid”export default class Foo {}code-block.js:1:1 lint/nursery/useConsistentDefaultExportStyle FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ This class is declared inside its export default statement.
> 1 │ export default class Foo {}
│ ^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ This project declares default-exported classes separately, so that all default exports look the same.
ℹ Declare the class first, then export it with export default and its name.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Declare the class separately.
1 │ - export·default·class·Foo·{}
1 │ + class·Foo·{}
2 │ + export·default·Foo;
2 3 │
class Foo {}export default Foo;See Also
Section titled “See Also”- If you want to disallow default exports entirely, see
noDefaultExport. - If you want to choose between function declarations and functions assigned to variables, see
useConsistentFunctionStyle.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.