如何在 eslint 中禁止空导入?

How to disallow empty imports in eslint?

我一直在尝试禁止这样的空导入:

import {} from 'module-foo';

有人知道怎么做吗? (我有一个 TypeScript Node.js 项目。)

虽然没有预定义的规则,但您可以使用 no-restricted-syntax rule and AST Selectors 来禁止这种特定语法:

// .eslintrc.js

module.exports = {
  // ... rest of the options
  rules: {
    // ... rest of the rules
    'no-restricted-syntax': [
      'error',
      {
        selector: 'ImportDeclaration[specifiers.length = 0]',
        message: 'Empty imports are not allowed',
      },
    ],
  }
}

检查 this AST Tree 以了解选择器如何定位空导入。