在可视化工作室代码中显示所有警告和错误

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

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

98401 次浏览

现在是不可能的,VSCode 团队有一个 请求的功能,他们正在工作,所以我们必须等待。

另一种方法是使用任务运行程序来对项目中的所有错误(比如在 这本指南中创建的错误)进行联结。

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 代码中的问题扫描程序,但是如果您使用 eslint,这是一个很好的替代方案。


老答案

以下是您如何在不到10秒的时间内看到所有问题的方法。

你耍了个小把戏。

打开替换文件 Ctrl + Shift + H中的所有内容。

;代替 ;

点击替换所有。就是这样。现在,检查问题。

这里的假设是所有文件至少有一个分号 更大的项目,如果你得到一个警告,要求你完善你的 搜索,只要输入一些在所有文件中都是常见的,但不是 展示了很多。

可视化代码有一个 ESLINT 扩展,用于将 lint 错误集成到 IDE 中。无论何时保存文件,它都会显示其 lint 错误。看: https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint

对于任何像我一样在谷歌搜索“ c # vs code show all error in all files”中多次遇到这个问题的人来说,我的问题是我有太多的文件与我的项目相关——当你的文件总数超过1000个时,C # 扩展默认开始检查 只有打开的文件错误。

转到 Settings = > Workspace = > Max Project File Count For DiagnosisAnalysis,并将值增加到您认为合理的任何位置。

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

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

非常重要: 确保检查 酷毙了! 但我不用 Eslint部分!这提供了一个全球性的解决方案!通过设置 任务!还有详细的解释!

注意: 如果您觉得文档被涂抹了!一定要浏览并且找到抓住你的标题!尽管每个部分都很重要!(TLDS (太长的脱脂))。

Javascript 和 Eslint

加上@Ajay Raghav 的回答!

这部分展示了如何运行任务的问题! 和执行的输出!

对于 javascript,Vscode 埃斯林特扩展提供了这样一个特性!如果您使用的是 Eslint (而不是 jshint) ,那么您已经安装了它!

如@Ajay Raghav 回答中所描述的用法! 在 埃斯林特扩展页面中进行了解释!

Https://marketplace.visualstudio.com/items?itemname=dbaeumer.vscode-eslint

eslint.lintTask.enable: 该扩展是否将一个 lint 任务贡献给 lint 整个工作区文件夹。

eslint.lintTask.options: 运行任务时应用的命令行选项,用于对整个工作区(https://eslint.org/docs/user-guide/command-line-interface)进行链接。指向自定义。Json 文件和一个自定义。埃斯林蒂诺是:

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

在运行 VS 代码的任务时使用扩展名

医生说:

扩展名仅在键入。如果你想 把整个工作空间都弄脏了设置 eslint.lintTask.enabletrue和扩展也将有助于 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做的那样! (问题匹配器: $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)设置任务!对于一个工作区或整个用户!(有些任务类型只需要为一个工作区设置!它们取决于工作空间的变量) !

此外,任务功能包含了很多选项和功能!而且是一大块!想了解更多,请查看文档!

回到我们的问题上来!

我们要 衬里整个项目! 还有 错误检测

我们需要有 错误名单问题标签! 最好也与 修正建议

所有这些都要通过 任务来完成。

设置任务! 和核心元素

通过 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 运行 lint)

这里最重要的东西!是决定类别、任务处理和启动设置的类型!定义执行内容的脚本!最后也是最重要的一点!

对于整个任务设置! 和选项,您可以检查的医生!

下面是 打印稿的另一个例子:

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

哟! 你刚刚展示了如何,一行以上! 是的,有一些重要的东西! 我们可以通过 编译过程看到 打印稿的问题!从 或者TSLINT!TSLINT 将 支持代码样式规则等等!但是 mmmm 不推荐使用 TSLINT埃斯林特开始了!简单来说就是 使用 埃斯林特!所以 编译过程0!这样就更有意思了!

从官方链接查看以下链接:

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

还有幽默的表情: 不要害怕这么做

enter image description here

已经找到了!

我应该把特林特迁移到埃斯林特吗

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

选择和反对的理由是:

在 TypeScript 2019路线图中,TypeScript 核心团队解释说,ESLint 的架构比 TSLint 的性能更好,而且他们只会在为 TypeScript 提供编辑器链接集成时关注 ESLint。

检查 给你如何在没有迁移工具的情况下安装. 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 文件。

一定要使用支持类型脚本的代码样式版本,比如 airbnb:

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

(下一部分是最重要的) !

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

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

VS 代码可以使用问题匹配器处理任务的输出。问题匹配器扫描任务输出文本,寻找已知的警告或错误字符串,并在编辑器和问题面板中内联地报告这些内容。VS 代码提供了几个问题匹配器“在盒子里”:

  • 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 Compiler : $mscompile假定文件名报告为绝对路径。
  • Lessc 编译器 : $lessc假定文件名报告为绝对路径。
  • Node Sass 编译器 : $node-sass假定文件名报告为绝对路径。

好吧,但是你说的是 JAVA,C/C + + ,PHP,Python..。 我们需要写我们自己的问题

C/C + + (海湾合作委员会)

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

Https://marketplace.visualstudio.com/items?itemname=ms-vscode.cpptools

扩展提供 $gcc问题匹配器!

任务如下:

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

好了,现在我们知道了,如何使问题匹配!我们还没喝咖啡呢!那我们就这么做吧!(等等,我刚刚谷歌和 给你有人这样做)

{
// 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
}
}
}
]
}

问题匹配器和自动修复

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

它不是! 语言支持或短程序扩展是什么 提供这样的功能! 通过使用 Vscode 行动应用程序接口

将军

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

因此很简单: problem Matcher 设置了如何在 problem 选项卡上解析和输出任务运行的输出! 语言支持扩展实现自动修复!或者短毛!(扩展)[我可以做一个游戏场地扩展,如果我想] !

也注意到 问题标签中的黄色灯泡!工程和允许自动修理!因为 问题匹配器为问题提供线索!这就是 映射到扩展修复建议范围的输出!在 CodeActionProvider上更精确!

快跑:

npx eslint --init

并根据需要进行配置