【问题标题】:How to remove unused imports with a command?如何使用命令删除未使用的导入?
【发布时间】:2021-08-19 09:37:22
【问题描述】:

我的 nx 项目中有文件,其中包含没有任何用途的导入声明,我想删除它们。

在stackoverflow中搜索后,我找到了在vscode中打开文件的答案,然后按alt+shift+o,当不使用声明时,它会删除并排序导入。

但我有 10,000 个文件。那么在所有这些文件中是否有执行此操作的命令?我查看eslint,但没有规定。

【问题讨论】:

标签: javascript angular typescript visual-studio-code eslint


【解决方案1】:

安装no-unused-imports plugin

将未使用的导入添加到 .eslintrc 文件的插件部分

{
  "plugins": ["...", "unused-imports"]
}

添加以下规则

"no-unused-vars": "off",
"unused-imports/no-unused-imports": "error",
"unused-imports/no-unused-vars": [
  "warn",
  { "vars": "all", "varsIgnorePattern": "^_", "args": "after-used", "argsIgnorePattern": "^_" }
    ],

然后将脚本添加到您的 package.json 文件中

"scripts": {
   ...
   "fix-lint-errors": "eslint nx --fix"
},

从命令行运行脚本

npm run fix-lint-errors

yarn fix-lint-errors

【讨论】:

    【解决方案2】:

    如果您是 的重度用户,那么您只需打开您的偏好设置,然后将以下内容添加到您的 settings.json 中:

        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
          "source.organizeImports": true
        }
    

    或者您可以创建一个独立的 文件,其中包含以下内容:

    {
        "extends": ["tslint-etc"],
        "rules": {
                "no-unsed-declaration: true"
    }}
    

    然后运行以下命令来修复导入:

    tslint --config tslint-imports.json --fix --project
    

    然后使用

    ng build
    

    ng build name_of_project --configuration=production 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-11
      • 2010-12-10
      • 2021-10-17
      • 1970-01-01
      相关资源
      最近更新 更多