运行 autofix 对这些导入进行排序!关于 TypeScript 中的 eslint simple-import-sort/imports 插件

Lan*_*ard 14 typescript eslint visual-studio-code

到目前为止,Eslint 正在自动修复和清理我的代码。但现在我添加了这两个 eslint 插件:

"eslint-import-resolver-typescript": "^3.5.2",
"eslint-plugin-import": "^2.26.0",
Run Code Online (Sandbox Code Playgroud)

我在.eslintrc.json以下rules部分中使用了它们:

"simple-import-sort/imports": [
  "error",
  {
    "groups": [
      [
        "^(assert|buffer|child_process|cluster|console|constants|crypto|dgram|dns|domain|events|fs|http|https|module|net|os|path|punycode|querystring|readline|repl|stream|string_decoder|sys|timers|tls|tty|url|util|vm|zlib|freelist|v8|process|async_hooks|http2|perf_hooks)(/.*|$)"
      ],
      ["^\\w"],
      ["^@"],
      ["^\\u0000"],
      ["^\\.\\.(?!/?$)", "^\\.\\./?$"],
      ["^\\./(?=.*/)(?!/?$)", "^\\.(?!/?$)", "^\\./?$"]
    ]
  }
],
"simple-import-sort/exports": [
  "error",
  {
    "groups": [
      [
        "^(assert|buffer|child_process|cluster|console|constants|crypto|dgram|dns|domain|events|fs|http|https|module|net|os|path|punycode|querystring|readline|repl|stream|string_decoder|sys|timers|tls|tty|url|util|vm|zlib|freelist|v8|process|async_hooks|http2|perf_hooks)(/.*|$)"
      ],
      ["^\\w"],
      ["^@"],
      ["^\\u0000"],
      ["^\\.\\.(?!/?$)", "^\\.\\./?$"],
      ["^\\./(?=.*/)(?!/?$)", "^\\.(?!/?$)", "^\\./?$"]
    ]
  }
],
"import/first": "error",
"import/newline-after-import": "error",
"import/no-duplicates": "error"
Run Code Online (Sandbox Code Playgroud)

我也有:

"plugins": [
  "@typescript-eslint",
  "import",
  "simple-import-sort"
],
Run Code Online (Sandbox Code Playgroud)

为什么它不在保存时自动修复导入?

在此输入图像描述

我的.vscode/settings.json是这样的,它允许修复保存:

{
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.formatOnSave": true,
  "eslint.format.enable": true,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "eslint.validate": ["javascript", "typescript"],
  "eslint.nodePath": "./node_modules/eslint",
  "eslint.packageManager": "npm"
}
Run Code Online (Sandbox Code Playgroud)

Sha*_*ath 8

如果您希望 esLint 在一次提交中运行您的代码,请调用npx eslint . --fix.

这将运行 eslint 并尝试修复所有可能的问题(例如导入排序)。