【问题标题】:How to disallow empty imports in eslint?如何在 eslint 中禁止空导入?
【发布时间】:2022-04-07 00:36:22
【问题描述】:

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

import {} from 'module-foo';

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

【问题讨论】:

  • 我想这应该属于npmjs.com/package/eslint-plugin-import,但它现在看起来没有那个规则。
  • @jonrsharpe 谢谢,我已经在使用这个插件了,但我也没有找到这个规则。

标签: eslint typescript-eslint


【解决方案1】:

虽然没有预定义的规则,但您可以使用no-restricted-syntax ruleAST 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 以了解选择器如何针对空导入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-02
    • 1970-01-01
    • 2022-12-16
    • 2016-12-09
    • 2011-01-11
    • 2019-03-22
    • 1970-01-01
    相关资源
    最近更新 更多