显示visual studio代码中的所有警告和错误

时间:2016-10-12 11:04:36

标签: visual-studio-code

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

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

5 个答案:

答案 0 :(得分:10)

现在不可能,VSCode团队有一个request用于他们正在使用的功能,所以我们必须等待。

另一种方法是使用任务运行器来隐藏项目中的所有错误,例如在this guide中创建的错误。

答案 1 :(得分:10)

UPDATE 2019

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

"eslint.provideLintTask": 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中的所有文件。

;替换为;

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

这里的假设是所有文件都至少有一个分号。对于较大的项目,如果收到警告,要求您缩小搜索范围,则只需输入所有文件中常见但很少出现的内容即可。

答案 2 :(得分: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
  • 点击,就是这样

enter image description here

enter image description here

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

enter image description here

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

并关注交互式终端!

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

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

运行结果

首先在终端上运行任务!

enter image description here

enter image description here

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

enter image description here

您还可以看到问题会自动列在问题标签中!哪个很酷!

超级酷!但我不使用 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 功能并从中获利!这包括在终端上单击链接导航!并在问题选项卡上列出问题!自动修复和建议!与调试工具集成! ...等等!这取决于任务类型!

enter image description here

通过设置文件(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编译过程看到typescript问题! 通过TSLINT! TSLINT 将支持代码样式规则等等! 但是 mmmm 不推荐使用 TSLINTESLINT 正在兴起!简单来说使用 Eslint!所以我们可以将 Eslint 用于 Typescript!那可以更丰富!

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

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

还有幽默的脸:不要害怕这样做

enter image description here

已经做到了!

Should i migrate Tslint to eslint

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

选择和弃用的原因是:

<块引用>

在 TypeScript 2019 Roadmap 中,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 附带了几个“内置”问题匹配器:

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

好吧,但你说的是 JAVA、C/C++、PHP、Python…… => 我们需要编写自己的problemMatcher

C/C++ (GCC)

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

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

扩展程序提供 $gcc problemMatcher!

任务将如下:

{
  "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

gcc 的例子是为 c/c++ 给出的! 编译结果如下:

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呢!所以让我们为它做这件事吧! (等等,我刚刚用谷歌搜索了一下,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
        }
      }
    }
  ]
}

问题匹配器和自动修复

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

不是! 语言支持或linters 扩展提供了这样的功能!并且通过使用 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 上得到精确!

答案 3 :(得分:0)

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

答案 4 :(得分:0)

对于像我这样的人,在Google搜索中多次遇到此问题(“ c#vs代码显示所有文件中的所有错误”)时,我的问题是我的项目关联了太多文件– C#扩展名默认情况下,当总数超过1000个文件时,就会开始仅打开文件错误检查。

转到“设置” =>“工作区” =>“用于诊断分析的最大项目文件数”,然后将该值增加到您认为合理的位置。

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

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