Skip to content
← Back to rules

unicorn/no-anonymous-default-export Restriction

🚧 An auto-fix is planned for this rule, but not implemented at this time.

它的作用

禁止将匿名函数和类作为默认导出。

为什么这不好?

为默认导出命名可以提高可搜索性,并确保模块默认导出的标识符在声明和导入时保持一致。

示例

以下是此规则的错误代码示例:

javascript
export default class {}
export default function () {}
export default () => {};
module.exports = class {};
module.exports = function () {};
module.exports = () => {};

以下是此规则的正确代码示例:

javascript
export default class Foo {}
export default function foo () {}

const foo = () => {};
export default foo;

module.exports = class Foo {};
module.exports = function foo () {};

const foo = () => {};
module.exports = foo;

如何使用

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

json
{
  "rules": {
    "unicorn/no-anonymous-default-export": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "unicorn/no-anonymous-default-export": "error",
  },
});
bash
oxlint --deny unicorn/no-anonymous-default-export

版本

此规则是在 v0.3.3 中添加的。

参考资料