【问题标题】:Show all warnings and errors in visual studio code在 Visual Studio 代码中显示所有警告和错误
【发布时间】:2022-03-22 21:03:38
【问题描述】:

我正在使用 Visual Studio Code 版本 1.6.0。 是否有可能显示当前根文件夹中所有文件的错误和警告?

目前它只显示打开文件的错误和警告。

【问题讨论】:

    标签: visual-studio-code


    【解决方案1】:

    2019 年更新

    ES-Lint 在 VS Code 中引入了一个新任务。您必须在工作区设置中启用它。

    "eslint.lintTask.enable": true
    

    只需进入终端菜单并选择运行任务,然后选择

    eslint: lint whole folder
    

    您还可以通过在终端中运行以下命令来自动修复大多数问题:

    .\node_modules\.bin\eslint.cmd --fix .
    

    参考:https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint

    虽然我们仍在等待 VS Code 中的问题扫描器,但如果您使用 eslint,这已经足够好。


    旧答案

    您可以通过以下方式在 10 秒内查看所有问题。

    你使用了一个小技巧。

    打开替换所有文件Ctrl + Shift + H

    ; 替换为;

    点击全部替换。而已。现在,检查问题。

    这里的假设是所有文件都至少有一个分号。为了 更大的项目,如果你收到一个警告,要求你改进你的 搜索,只需输入所有文件中通用但不通用的内容 展示了很多。

    【讨论】:

    • 不错的提示。我认为括号是这个的完美选择。
    • 每次打开vscode都需要做这个,有没有办法一直做这个?
    • 那,我的朋友,是黑帮!
    • 顺便说一下,这个确切的字符串现在已经被弃用了,它应该是"eslint.lintTask.enable": true,
    • 实际上在一个非常大的 repo 中,替换可能需要 10 多秒...
    【解决方案2】:

    目前不可能,VSCode 团队有一个 request 来表示他们正在开发的功能,所以我们必须等待。

    另一种方法是使用任务运行器来检查项目中的所有错误,就像在 this guide 中创建的错误一样。

    【讨论】:

    • 这已经一年多了!
    • 这是 C/C++ 的限制吗?它是 2018 年,没有添加。
    • 2019年了,这个功能还没有。
    • 对于 eslint 用户,该解决方案已添加到 VS Code 中。
    【解决方案3】:

    非常重要:请务必勾选超酷!但我不使用 eslint 部分!提供全球解决方案!通过设置任务!并详细说明!

    注意:如果您觉得文档被吸干了!确保浏览并找到吸引您的标题!尽管每个部分都可能很重要! (TLDS(略读太长))。

    Javascript 和 Eslint

    添加@Ajay Raghav 的答案!

    本节展示如何运行有问题的任务!以及执行的输出!

    对于 javascript,Vscode Eslint 扩展提供了这样的功能!如果您使用的是 Eslint(而不是 jshint),那么您已经安装了它!

    如@Ajay Raghav 回答中所述的用法!在 Eslint 扩展页面上有解释!

    https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint

    eslint.lintTask.enable: 扩展是否提供 lint 任务来 lint 整个工作区文件夹。

    eslint.lintTask.options:在运行任务时应用命令行选项以对整个工作区进行 linting (https://eslint.org/docs/user-guide/command-line-interface)。指向自定义 .eslintrc.json 文件和自定义 .eslintignore 的示例如下:

    {
      "eslint.lintTask.options": "-c C:/mydirectory/.eslintrc.json --ignore-path C:/mydirectory/.eslintignore ."
    }
    

    在运行 VS Code 任务的情况下使用扩展

    来自文档:

    扩展程序仅在键入时对单个文件进行 linting。如果你想 lint 整个工作区eslint.lintTask.enable 设置为 true 并且扩展程序也将贡献 eslint: lint whole folder 任务。不再需要在tasks.json 中定义自定义任务。

    如果你不熟悉任务!这里你如何使用上面的!

    • 转到命令托盘 (CTRL + P + SHIFT)
    • > tasks run
    • 点击Tasks: run Task
    • 你会发现eslint: lint whole folder
    • 点击就可以了

    如果 eslint 没有配置设置!你会得到以下错误!

    如果是这样,运行eslint --init

    并关注交互式终端!

    请注意,如果您没有全局可用的 eslint 命令!

    通过运行 npm i -g eslint! 全局安装它!

    运行结果

    首先任务在终端上运行!

    您可以查看详细报告!您可以在终端功能上使用click link (CTRL + CLICK)!直接打开有问题的文件!

    您也可以看到问题将自动列在问题标签中!这很酷!

    超级酷!但我不使用 eslint

    打字稿和 TSLINT

    如果您是打字稿用户并且使用 TSLINT! 那我们就需要走向全球之路!那就是创建一个任务! 就像eslint 所做的一样! (problemMatcher: $tsc) [你会知道那只是下面的一点]。

    (请务必查看TSLINT is deprecated 标题)!

    我使用一门完整的其他语言(c#、java、c++、php、python ...)

    是的,是的!我们需要一种全球化的方式!而全局方式是通过创建任务

    创建任务(全局方式)

    (支持所有语言(为每种语言配置))

    我们需要创建一个任务!

    vscode 文档对它的解释非常棒且详细!

    https://code.visualstudio.com/docs/editor/tasks

    检查文档!

    简而言之!我会在任务是什么中恢复:

    vscode 工具和功能!这允许我们基于工具和脚本设置任务并在 vscode 中运行它们!在 vscode 中分析那里的输出!并激活其他 vscode 功能并从中获利!这包括终端上的点击链接导航!并在问题选项卡上列出问题!自动修复和建议!与调试工具集成! ...ETC!这取决于任务类型!

    通过设置文件(task.json)设置任务!对于工作区或整个用户! (有些任务类型只需要为工作区设置!它们取决于工作区的变量)!

    任务功能还包含很多选项和功能!而且是一大块!有关更多信息,请查看文档!

    回到我们的问题!

    我们希望对整个项目进行linting!以及错误检测

    我们需要在问题标签错误列出!最好也有修复建议

    这一切都将通过一个任务来完成。

    设置任务!及核心要素

    通过 vscode 任务功能和集成!我们需要对其进行配置以允许良好的输出!并与问题选项卡集成!

    配置会这样:

    {
      // See https://go.microsoft.com/fwlink/?LinkId=733558
      // for the documentation about the tasks.json format
      "version": "2.0.0",
      "tasks": [
        {
          "type": "npm",
          "script": "lint",
          "problemMatcher": ["$eslint-stylish"]
        }
      ]
    }
    

    (这里是通过 npm run lint eslint)

    在这里看到的最重要的事情!是确定类别和任务处理和启动设置的类型!定义执行内容的脚本!最后也是最重要的problemMatcher

    为整个任务设置!以及您可以查看文档的选项!

    这里是 typescript 的另一个示例:

    {
      // See https://go.microsoft.com/fwlink/?LinkId=733558
      // for the documentation about the tasks.json format
      "version": "2.0.0",
      "tasks": [
        {
          "type": "typescript",
          "tsconfig": "tsconfig.json",
          "problemMatcher": ["$tsc"],
          "group": {
            "kind": "build",
            "isDefault": true
          }
        }
      ]
    }
    

    TSLINT 已弃用

    哟!你刚刚展示了如何,上面一行!是的,有重要的事情! 我们可以通过tsc compile process看到typescript问题! 或者通过TSLINT! TSLINT 将支持代码样式规则等等! 但是 mmmm TSLINT 已弃用ESLINT 正在迎头赶上!简而言之使用 Eslint!所以我们可以将 Eslint 用于 Typescript!而且可以更丰富!

    从官方链接查看下面的链接:

    https://code.visualstudio.com/api/advanced-topics/tslint-eslint-migration

    幽默的表情:不要害怕这样做

    已经做到了!

    Should i migrate Tslint to eslint

    另一个原因是:TSLint 是一个只能用于 TypeScript 的 linter,而 ESLint 支持 JavaScript 和 TypeScript。

    选择和弃用的原因是:

    在 TypeScript 2019 路线图中,TypeScript 核心团队解释说,ESLint 具有比 TSLint 更高性能的架构,并且在为 TypeScript 提供编辑器 linting 集成时,他们只会专注于 ESLint。

    查看here如何在没有迁移工具的情况下设置 .eslintrc.js

    https://www.npmjs.com/package/@typescript-eslint/eslint-plugin

    简而言之就是:

    module.exports = {
      "parser": "@typescript-eslint/parser", // set eslint parser
       "parserOptions": {
           "ecmaVersion": 12, // latest ecma script features
           "sourceType": "module" // Allows for the use of imports
       },
       "plugins": [
           "@typescript-eslint"
       ],
      extends: [
        "plugin:@typescript-eslint/recommended" // Uses the recommended rules from the @typescript-eslint/eslint-plugin
      ],
      rules: {
        // Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
        // e.g. "@typescript-eslint/explicit-function-return-type": "off",
      }
    };
    

    当然:npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin

    执行 eslint 时请务必使用eslint --ext .js,.ts!因为默认情况下 eslint 只会搜索 .js 文件。

    确保使用支持 typescript 的代码样式版本,例如 airbnb:

    https://www.npmjs.com/package/eslint-config-airbnb-typescript

    (下一节最重要)!

    使用问题匹配器处理任务输出

    https://code.visualstudio.com/docs/editor/tasks#_processing-task-output-with-problem-matchers

    VS Code 可以使用问题匹配器处理任务的输出。问题匹配器扫描任务输出文本中的已知警告或错误字符串,并在编辑器和“问题”面板中内联报告这些内容。 VS Code 附带了几个问题匹配器'in-the-box':

    • TypeScript$tsc 假定输出中的文件名与打开的文件夹相关。
    • TypeScript Watch$tsc-watch 匹配在监视模式下执行时从 tsc 编译器报告的问题。
    • JSHint$jshint 假定文件名报告为绝对路径。
    • JSHint Stylish$jshint-stylish 假定文件名报告为绝对路径。
    • ESLint Compact$eslint-compact 假定输出中的文件名与打开的文件夹相关。
    • ESLint Stylish$eslint-stylish 假定输出中的文件名与打开的文件夹相关。
    • Go$go 匹配 go 编译器报告的问题。假设文件名与打开的文件夹相关。
    • CSharp 和 VB 编译器$mscompile 假定文件名报告为绝对路径。
    • Lessc 编译器$lessc 假定文件名报告为绝对路径。
    • Node Sass 编译器$node-sass 假定文件名报告为绝对路径。

    好的,但你说的是 JAVA、C/C++、PHP、Python ... => 我们需要编写自己的问题匹配器

    C/C++ (GCC)

    通过官方ms扩展ms-vscode.cpptools添加vscode中的c/c++支持

    https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools

    扩展提供$gccproblemMatcher!

    任务将如下:

    {
      "version": "2.0.0",
      "tasks": [
        {
          "type": "shell",
          "label": "g++ build all",
          "command": "/usr/bin/g++",
          "args": ["${workspaceFolder}/src/*.cpp", "-o", "${workspaceFolder}/build"],
          "options": {
            "cwd": "/usr/bin"
          },
          "problemMatcher": ["$gcc"],
          "group": {
            "kind": "build",
            "isDefault": true
          }
        }
      ]
    }
    

    请注意,我刚刚编译了 src 中的文件(一级)

    可以使用cmake构建!

    可能想检查: https://code.visualstudio.com/docs/cpp/config-linux#_build-helloworldcpp

    定义问题匹配器

    您可以查看下面的文档部分:

    https://code.visualstudio.com/docs/editor/tasks#_defining-a-problem-matcher

    为 c/c++ 提供了 gcc 的示例! 编译结果如下:

    helloWorld.c:5:3: warning: implicit declaration of function ‘prinft’
    

    我们通过以下方式设置匹配器

    {
      // The problem is owned by the cpp language service.
      "owner": "cpp",
      // The file name for reported problems is relative to the opened folder.
      "fileLocation": ["relative", "${workspaceFolder}"],
      // The actual pattern to match problems in the output.
      "pattern": {
        // The regular expression. Example to match: helloWorld.c:5:3: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
        "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
        // The first match group matches the file name which is relative.
        "file": 1,
        // The second match group matches the line on which the problem occurred.
        "line": 2,
        // The third match group matches the column at which the problem occurred.
        "column": 3,
        // The fourth match group matches the problem's severity. Can be ignored. Then all problems are captured as errors.
        "severity": 4,
        // The fifth match group matches the message.
        "message": 5
      }
    }
    

    直接在任务配置中可以这样:

    {
      "version": "2.0.0",
      "tasks": [
        {
          "label": "build",
          "command": "g++",
          "args": ["${workspaceFolder}/src/*.cpp", "-o", "${workspaceFolder}/build"],
          "problemMatcher": {
            "owner": "cpp",
            "fileLocation": ["relative", "${workspaceFolder}"],
            "pattern": {
              "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
              "file": 1,
              "line": 2,
              "column": 3,
              "severity": 4,
              "message": 5
            }
          }
        }
      ]
    }
    

    你可以看到设置是多么的直截了当! 您可以查看上面的文档链接以获取更多详细信息!

    定义多行问题匹配器

    https://code.visualstudio.com/docs/editor/tasks#_defining-a-multiline-problem-matcher

    有些工具会将源文件中发现的问题分散到多行,尤其是在使用时尚的记者时。一个例子是 ESLint;在时尚模式下,它会产生如下输出:

    test.js
      1:0   error  Missing "use strict" statement                 strict
    ✖ 1 problems (1 errors, 0 warnings)
    

    我不会去详细检查文档!它解释得很好(也检查loop 属性!

    {
      "owner": "javascript",
      "fileLocation": ["relative", "${workspaceFolder}"],
      "pattern": [
        {
          "regexp": "^([^\\s].*)$",
          "file": 1
        },
        {
          "regexp": "^\\s+(\\d+):(\\d+)\\s+(error|warning|info)\\s+(.*)\\s\\s+(.*)$",
          "line": 1,
          "column": 2,
          "severity": 3,
          "message": 4,
          "code": 5,
          "loop": true
        }
      ]
    }
    

    修改现有问题匹配器

    https://code.visualstudio.com/docs/editor/tasks#_modifying-an-existing-problem-matcher

    只需检查文档!

    JAVA

    好的,现在我们知道了,如何制作问题匹配器!我们还没有做java!所以让我们为它做吧! (等一下,我刚刚在 Google 上搜索了一下,然后 here 有人这样做了)

    {
        // compiles all files in the folder of the currently opened file
        "taskName": "javac all",
        "args": ["$env:CLASSPATH += ';${fileDirname}'; javac ${fileDirname}\\*.java -Xlint"],
        "showOutput": "silent",
        "problemMatcher": {
            "owner": "java",
            "fileLocation": "absolute",
            "pattern": {
                "regexp": "^(.*):([0-9]+): (error|warning): (.*)$",
                "file": 1,
                "line": 2,
                "severity": 3,
                "message": 4
            }
        }
    }
    

    PHP

    这里也有一个使用代码嗅探的 php 任务!

    src(再次谷歌搜索):https://github.com/bmewburn/vscode-intelephense/issues/1102

    {
      "version": "2.0.0",
      "tasks": [
        {
          "label": "PHP: CodeSniff Workspace",
          "type": "shell",
          "command": "${config:phpcs.executablePath}",
          "args": [
            "--standard=${config:phpcs.standard}",
            "--extensions=module,inc,install,php,theme,test,js,css",
            "--report=csv",
            "--basepath=${workspaceFolder}",
            "web/modules/custom"
          ],
          "options": {
            "cwd": "${workspaceFolder}"
          },
          "problemMatcher": {
            "owner": "php",
            "fileLocation": [
              "relative",
              "${workspaceFolder}"
            ],
            "pattern": {
              "regexp": "^\"(.*?)\",(\\d+),(\\d+),(error|warning),\"(.*)\",.*$",
              "file": 1,
              "line": 2,
              "column": 3,
              "severity": 4,
              "message": 5
            }
          }
        }
      ]
    }
    

    问题匹配器和自动修复

    好的,问题匹配器如何提出自动修复建议?答案是不会!可以很清楚!或者你可能在第一次看到问题匹配器时说,嘿它可能有关系!

    不是! 语言支持或 linter 扩展正是提供了这样的功能!并且通过使用 vscode Action api

    检查

    https://code.visualstudio.com/api/references/vscode-api#CodeAction

    https://code.visualstudio.com/api/references/vscode-api#CodeActionKind

    https://code.visualstudio.com/api/references/vscode-api#CodeActionProvider%3CT%3E

    这么简单:problemMatcher 设置任务运行的输出如何被解析并输出到问题选项卡上! 语言支持扩展实现了自动修复!或短绒! (扩展)[如果我愿意,我可以做一个游乐场扩展]!

    还要注意问题标签中的黄色灯泡!工作并允许自动修复!因为问题匹配器为问题提供了路线!这将与扩展修复建议范围的输出映射!这在CodeActionProvider 上得到精确!

    【讨论】:

      【解决方案4】:

      可视化代码具有 ESLINT 扩展,可将 lint 错误集成到 IDE 中。每当您保存文件时,它都会显示其 lint 错误。看吧:https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint

      【讨论】:

        【解决方案5】:

        对于像我这样多次遇到这个问题的人,谷歌搜索“c# vs code show all errors in all files”的变体,我的问题是我的项目关联的文件太多——C#扩展默认情况下,当您的文件总数超过 1000 个时,仅打开文件开始错误检查。

        转到设置 => 工作区 => 诊断分析的最大项目文件计数,并将值增加到您认为合理的任何位置。

        等效地,将以下行添加到您现有的 settings.json 中,这会将文件计数截止值增加到 10,000。

        {
            // Rest of settings.json file here
            "csharp.maxProjectFileCountForDiagnosticAnalysis": 10000,
        }
        

        【讨论】:

          【解决方案6】:

          只要运行:

          npx eslint --init
          

          并根据需要进行配置

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-04-12
            • 2011-07-23
            • 2020-02-26
            • 2016-10-16
            • 2022-07-13
            相关资源
            最近更新 更多