Skip to content

noDefaultExport

诊断类别:lint/style/noDefaultExport

¥Diagnostic Category: lint/style/noDefaultExport

自从:v1.4.0 来源:

¥Since: v1.4.0 Sources:

禁止默认导出。

¥Disallow default exports.

在编辑器中无法轻易发现默认导出:当用户尝试导入名称时,编辑器无法建议它们。

¥Default exports cannot be easily discovered inside an editor: They cannot be suggested by the editor when the user tries to import a name.

此外,默认导出不鼓励代码库的一致性:导入默认导出的模块必须选择一个名称。不同的模块可能使用不同的名称。

¥Also, default exports don’t encourage consistency over a code base: the module that imports the default export must choose a name. It is likely that different modules use different names.

此外,默认导出鼓励导出充当命名空间的对象。这是一种用于模仿 CommonJS 模块的遗留模式。

¥Moreover, default exports encourage exporting an object that acts as a namespace. This is a legacy pattern used to mimic CommonJS modules.

出于所有这些原因,团队可能希望禁止默认导出。

¥For all these reasons, a team may want to disallow default exports.

请注意,此规则仅禁止 EcmaScript 模块中的默认导出。它忽略 CommonJS 默认导出。

¥Note that this rule disallows only default exports in EcmaScript Module. It ignores CommonJS default exports.

¥Examples

¥Invalid

export default function f() {};
code-block.js:1:8 lint/style/noDefaultExport ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid default exports.

> 1 │ export default function f() {};
^^^^^^^
2 │

Default exports cannot be easily discovered inside an editor and don’t encourage the use of consistent names through a code base.

Use a named export instead.

export default class C {};
code-block.js:1:8 lint/style/noDefaultExport ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid default exports.

> 1 │ export default class C {};
^^^^^^^
2 │

Default exports cannot be easily discovered inside an editor and don’t encourage the use of consistent names through a code base.

Use a named export instead.

export default {
f() {},
g() {},
};
code-block.js:1:8 lint/style/noDefaultExport ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid default exports.

> 1 │ export default {
^^^^^^^
2 │ f() {},
3 │ g() {},

Default exports cannot be easily discovered inside an editor and don’t encourage the use of consistent names through a code base.

Use a named export instead.

export { X as default };
code-block.js:1:15 lint/style/noDefaultExport ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Avoid default exports.

> 1 │ export { X as default };
^^^^^^^
2 │

Default exports cannot be easily discovered inside an editor and don’t encourage the use of consistent names through a code base.

Use a named export instead.

¥Valid

export function f () {};
export class C {};
export { default as X } from "mod";
module.exports = class {};

¥Related links