【问题标题】:eslint sort-imports mixing single and multipleeslint sort-imports 混合单个和多个
【发布时间】:2021-03-31 22:38:26
【问题描述】:

我用 eslint 安装了一个 CRA 应用程序,我使用带有 sort-imports 扩展名的 vscode。除了 sort-imports 规则之外,所有 linting 似乎都按我的意愿工作。它似乎允许混合使用 singlemultiple 导入。

这是我的 .eslintrc sort-imports 规则:

"sort-imports": [
  "error",
  {
    "memberSyntaxSortOrder": [
      "all",
      "single",
      "multiple",
      "none"
    ],
    "allowSeparatedGroups": true
  }
],

还有一个错误的例子:

import { Readonly } from "components/Form/Readonly";
import { Summary } from "components/Heading/Summary";
import axios from "axios";
import { config } from "system/config";
import qs from "qs";
import { useLoader } from "components/Loading";

sort-imports 喜欢这个,而 eslint 也不抱怨。我错过了什么,或者 eslint 应该抱怨这个吗?显然是在将singlemultiple 混合在一起,对吗?

如果我把它改成我想要的样子:

import axios from "axios";
import qs from "qs";

import { Readonly } from "components/Form/Readonly";
import { Summary } from "components/Heading/Summary";
import { config } from "system/config";
import { useLoader } from "components/Loading";

eslint 似乎没有抱怨,但 sort-imports 会将它恢复到上面的样子。

所以我想我的问题在于 sort-imports 扩展......但我仍然认为 eslint 应该抱怨我混合这些导入。

【问题讨论】:

    标签: reactjs typescript sorting eslint typescript-eslint


    【解决方案1】:

    虽然文档并不完全清楚 - 从我可以看出规则的来源 (https://github.com/eslint/eslint/blob/896273f9d75c973ce2c7cc25580ae667a10ec6f9/lib/rules/sort-imports.js#L77-L99),import { foo } from 'module' 被视为“单一”导入,因为它导入一件事 - 与 @987654323 相同@ 也是一个“单一”导入。

    import { foo, bar } from 'module' 是一个“多重”,因为它导入多个东西。

    【讨论】:

      猜你喜欢
      • 2020-09-02
      • 1970-01-01
      • 2016-07-21
      • 2013-07-20
      • 1970-01-01
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      相关资源
      最近更新 更多