如何配置 ESLint 使其不允许默认导出

How to configure ESLint so that it disallows default exports

我已经在网络和 Whosebug 上搜索了很长时间,但没有成功。

我正在尝试让 ESLint 将以下内容标记为错误:

export default ...;

其中 default 是这里的关键。到目前为止,我得到的最好的是对 eslint-plugin-import plugin and some of its rules that can make me closer to the target, namely the no-anonymous-default-export 规则的引用。但即使使用此规则,以下默认导出也是有效的:

const foo = 123
export default foo

export default class MyClass() {}

export default function foo() {}

如何配置 ESLint 使这四个也被视为错误?

您可以使用 no-restricted-syntax rule. Try pasting this in the demo 来尝试一下(您需要先在选项中将 "Source Type" 更改为 "module"):

/* eslint "no-restricted-syntax": ["error", {
    "selector": "ExportDefaultDeclaration",
    "message": "Prefer named exports"
  }] */
export default class Foo { } // 5:1 - Prefer named exports (no-restricted-syntax)

如果您已经在使用 eslint-plugin-import,则可以使用 no-default-export 规则(于 2018 年 2 月左右添加)。