简体   繁体   English

ESLint规则,每个文件仅允许默认或命名导出

[英]ESLint rule to allow only either default or named exports per file

I wish to have a following ESLint rule: 我希望遵循以下ESLint规则:

A js file can have either one default export, or as many named exports as possible. js文件可以具有一个默认导出,也可以具有尽可能多的命名导出。 But not both. 但不是两者。 So having one default export + named exports should result in an eslint warning. 因此,使用一个默认导出+命名导出应该会导致eslint警告。 Is this possible with eslint currently? eslint目前可能吗? If not, would it be easy to make a rule like that? 如果没有,制定这样的规则会容易吗?

Looking at the code for prefer-default-export , it looks like it may well be as simple as forking that rule and changing 看一下prefer-default-export的代码,看起来就像分叉规则并更改一样简单

'Program:exit': function() {
  if (specifierExportCount === 1 && !hasDefaultExport && !hasStarExport) {
    context.report(namedExportNode, 'Prefer default export.')
  }
},

to

'Program:exit': function() {
  if ((specifierExportCount >= 1 || hasStarExport) && hasDefaultExport) {
    context.report(namedExportNode, 'Do not use both named exports and a default export')
  }
},

specifierExportCount in that rule module counts the number of named exports. 该规则模块中的specifierExportCount计算命名导出的数量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM