mar*_*ill 22 visual-studio-code
我使用的是Visual Studio Code Version 1.6.0.是否有可能显示当前根文件夹中所有文件的错误和警告?
目前它只显示打开文件的错误和警告.
Moh*_*lal 21
非常重要:一定要勾选超酷!但我不使用 eslint部分。我提供的是全球解决方案!通过设置任务并进行详细说明。
\n注意:如果您觉得文档臃肿,请务必浏览并找到吸引您的标题!尽管每个部分都可能很重要。(TLDS(太长,请勿浏览))。
\n添加@Ajay Raghav 的答案!
\n本节展示如何运行有问题的任务!以及执行的输出!
\n对于javascript,Vscode Eslint扩展提供了这样的功能!如果您使用的是 Eslint(而不是 jshint),那么您已经安装了它!
\n用法如@Ajay Raghav 答案中所述!在Eslint扩展页面上有解释!
\nhttps://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint
\n\n\n\n
eslint.lintTask.enable:扩展是否提供 lint 任务来对整个工作区文件夹进行 lint 检测。
\n\n\n
eslint.lintTask.options:运行对整个工作区进行 linting 的任务时应用命令行选项 ( https://eslint.org/docs/user-guide/command-line-interface )。指向自定义 .eslintrc.json 文件和自定义 .eslintignore 的示例是:
{\n "eslint.lintTask.options": "-c C:/mydirectory/.eslintrc.json --ignore-path C:/mydirectory/.eslintignore ."\n}\nRun Code Online (Sandbox Code Playgroud)\n来自文档:
\n\n\n该扩展仅在键入时对单个文件进行 linting。如果您想对整个工作区设置lint ,并且扩展也将贡献该任务。不再需要在 中定义自定义任务。
\neslint.lintTask.enabletrueeslint: lint whole foldertasks.json
如果您不熟悉任务!下面就来教大家如何使用上面的方法吧!
\n> tasks runTasks: run Taskeslint: lint whole folder如果 eslint 没有配置设置!您将收到以下错误!
\n\n如果是这样,运行eslint --init
并关注互动终端!
\n请注意,如果您没有eslint全局可用的命令!
通过运行来全局安装它 npm i -g eslint!
首先任务在终端上运行!
\n\n\n你可以看到详细的报告!您可以使用click link终端功能 ( CTRL + CLICK)!直接打开有问题的文件!
您还可以看到问题将自动列在问题选项卡中!这很酷!
\n如果你是 typescript 用户并且使用 TSLINT!\n那么我们需要采用全局方式!这就是通过创建一个任务!\n就像eslint做的那样!(problemMatcher: $tsc) [你会知道这是什么]。
(一定要检查TSLINT is deprecated标题)!
是的,是的!我们需要一种全球性的方式!全局方式是通过创建任务!
\n(支持所有语言(需针对每种语言进行配置))
\n我们需要创建一个任务!
\nvscode 文档对它的解释非常精彩且详细!
\nhttps://code.visualstudio.com/docs/editor/tasks
\n检查文档!
\n简而言之!我会恢复任务的内容:
\n一个 vscode 工具和功能!这使我们能够基于工具和脚本设置任务并在 vscode 中运行它们!在 vscode 中分析输出!并激活其他 vscode 功能并从中获利!这包括终端上的点击链接导航!问题选项卡上列出了问题!自动修复和建议!与调试工具集成!...ETC!这取决于任务类型!
\n\n任务通过设置文件(task.json)设置!对于工作空间或整个用户!(有些任务类型只需为工作空间设置!它们取决于工作空间的变量)!
\n此外,任务功能包含很多选项和功能!而且是一大块!有关更多信息,请查看文档!
\n回到我们的问题!
\n我们想要整个项目的linting!和错误检测!
\n我们需要在问题选项卡上列出错误 !最好也提供修复建议!
\n所有这一切都将通过任务来完成。
\n通过vscode任务功能和集成!我们需要对其进行配置以允许良好的输出!并与问题选项卡集成!
\n配置将如下所示:
\n{\n "eslint.lintTask.options": "-c C:/mydirectory/.eslintrc.json --ignore-path C:/mydirectory/.eslintignore ."\n}\nRun Code Online (Sandbox Code Playgroud)\n(这里eslint通过npm run lint)
\n最重要的是看这里!是确定类别以及任务处理和启动设置的类型!定义执行内容的脚本!最后也是重要的问题匹配器!
\n对于整个任务设置!和选项你可以检查文档!
\n这是打字稿的另一个例子:
\n{\n // See https://go.microsoft.com/fwlink/?LinkId=733558\n // for the documentation about the tasks.json format\n "version": "2.0.0",\n "tasks": [\n {\n "type": "npm",\n "script": "lint",\n "problemMatcher": ["$eslint-stylish"]\n }\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\n哟!您刚刚展示了如何操作,上面有一行!是的,有一些重要的事情!\n我们可以通过tsc 编译过程看到打字稿问题!或者通过TSLINT!TSLINT将支持代码风格规则等等!但是mmmm TSLINT 已被弃用!ESLINT正在发挥作用!简而言之,使用Eslint!所以我们可以将 Eslint 用于 Typescript!那可以更丰富!
\n从官方链接查看以下链接:
\nhttps://code.visualstudio.com/api/advanced-topics/tslint-eslint-migration
\n幽默的表情:不要害怕这样做
\n\n已经做了!
\n\n另一个原因是:TSLint 是一个只能用于 TypeScript 的 linter,而 ESLint 同时支持 JavaScript 和 TypeScript。
\n选择和弃用的原因是:
\n\n\n在 TypeScript 2019 路线图中,TypeScript 核心团队解释说,ESLint 具有比 TSLint 更高性能的架构,并且在为 TypeScript 提供编辑器 linting 集成时,他们只会关注 ESLint。
\n
在这里查看以及如何在不使用迁移工具的情况下设置 .eslintrc.js!
\n或https://www.npmjs.com/package/@typescript-eslint/eslint-plugin
\n简而言之就是:
\n{\n // See https://go.microsoft.com/fwlink/?LinkId=733558\n // for the documentation about the tasks.json format\n "version": "2.0.0",\n "tasks": [\n {\n "type": "typescript",\n "tsconfig": "tsconfig.json",\n "problemMatcher": ["$tsc"],\n "group": {\n "kind": "build",\n "isDefault": true\n }\n }\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\n当然:npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin
\n\n\n
eslint --ext .js,.ts执行eslint时一定要使用!因为默认情况下 eslint 只会搜索 .js 文件。
确保使用支持打字稿的代码样式版本,例如爱彼迎:
\nhttps://www.npmjs.com/package/eslint-config-airbnb-typescript
\n(下一节是最重要的)!
\nhttps://code.visualstudio.com/docs/editor/tasks#_processing-task-output-with-problem-matchers
\n\n\nVS Code 可以使用问题匹配器处理任务的输出。问题匹配器扫描任务输出文本以查找已知的警告或错误字符串,并在编辑器和问题面板中内联报告这些内容。VS Code 附带了几个“现成的”问题匹配器:
\n
$tsc假设输出中的文件名相对于打开的文件夹。$tsc-watch匹配在监视模式下执行时 tsc 编译器报告的问题。$jshint假设文件名报告为绝对路径。$jshint-stylish假设文件名报告为绝对路径。$eslint-compact假设输出中的文件名相对于打开的文件夹。$eslint-stylish假设输出中的文件名相对于打开的文件夹。$go匹配 go 编译器报告的问题。假设文件名与打开的文件夹相关。$mscompile假定文件名报告为绝对路径。$lessc假定文件名报告为绝对路径。$node-sass假设文件名报告为绝对路径。好的,但是你说的是 JAVA、C/C++、PHP、Python ...\n=>我们需要编写自己的 ProblemMatcher
\nvscode中的c/c++支持是通过官方ms扩展添加的ms-vscode.cpptools
https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools
\n该扩展提供了$gccProblemMatcher!
任务将如下所示:
\nmodule.exports = {\n "parser": "@typescript-eslint/parser", // set eslint parser\n "parserOptions": {\n "ecmaVersion": 12, // latest ecma script features\n "sourceType": "module" // Allows for the use of imports\n },\n "plugins": [\n "@typescript-eslint"\n ],\n extends: [\n "plugin:@typescript-eslint/recommended" // Uses the recommended rules from the @typescript-eslint/eslint-plugin\n ],\n rules: {\n // Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs\n // e.g. "@typescript-eslint/explicit-function-return-type": "off",\n }\n};\nRun Code Online (Sandbox Code Playgroud)\n请注意,我刚刚进行编译以获取 src 中的文件(一级)
\n可以使用cmake来构建!
\n可能想检查:\n https://code.visualstudio.com/docs/cpp/config-linux#_build-helloworldcpp
\n您可以查看下面的文档部分:
\nhttps://code.visualstudio.com/docs/editor/tasks#_defining-a-problem-matcher
\n给出了 c/c++ 的 gcc 示例!\n编译结果将类似于:
\nhelloWorld.c:5:3: warning: implicit declaration of function \xe2\x80\x98prinft\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n我们通过以下方式设置匹配器
\n{\n "version": "2.0.0",\n "tasks": [\n {\n "type": "shell",\n "label": "g++ build all",\n "command": "/usr/bin/g++",\n "args": ["${workspaceFolder}/src/*.cpp", "-o", "${workspaceFolder}/build"],\n "options": {\n "cwd": "/usr/bin"\n },\n "problemMatcher": ["$gcc"],\n "group": {\n "kind": "build",\n "isDefault": true\n }\n }\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\n直接在任务配置中可以这样:
\nhelloWorld.c:5:3: warning: implicit declaration of function \xe2\x80\x98prinft\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n您可以看到设置是多么直接!\n您可以查看上面的文档链接以了解更多详细信息!
\nhttps://code.visualstudio.com/docs/editor/tasks#_defining-a-multiline-problem-matcher
\n\n\n有些工具将源文件中发现的问题分散到多行中,特别是在使用时尚的报告器时。一个例子是 ESLint;在时尚模式下,它会产生如下输出:
\n
test.js\n 1:0 error Missing "use strict" statement strict\n\xe2\x9c\x96 1 problems (1 errors, 0 warnings)\nRun Code Online (Sandbox Code Playgroud)\n我不会详细说明,请检查文档!它解释得很好(loop也检查属性!
{\n // The problem is owned by the cpp language service.\n "owner": "cpp",\n // The file name for reported problems is relative to the opened folder.\n "fileLocation": ["relative", "${workspaceFolder}"],\n // The actual pattern to match problems in the output.\n "pattern": {\n // The regular expression. Example to match: helloWorld.c:5:3: warning: implicit declaration of function \xe2\x80\x98printf\xe2\x80\x99 [-Wimplicit-function-declaration]\n "regexp": "^(.*):(\\\\d+):(\\\\d+):\\\\s+(warning|error):\\\\s+(.*)$",\n // The first match group matches the file name which is relative.\n "file": 1,\n // The second match group matches the line on which the problem occurred.\n "line": 2,\n // The third match group matches the column at which the problem occurred.\n "column": 3,\n // The fourth match group matches the problem\'s severity. Can be ignored. Then all problems are captured as errors.\n "severity": 4,\n // The fifth match group matches the message.\n "message": 5\n }\n}\nRun Code Online (Sandbox Code Playgroud)\nhttps://code.visualstudio.com/docs/editor/tasks#_modifying-an-existing-problem-matcher
\n只需检查文档即可!
\n好吧,现在我们知道了,如何制作问题匹配器!我们还没做java呢!那么让我们为此做吧!(等等,我刚刚用谷歌搜索,这里有人做到了)
\n{\n "version": "2.0.0",\n "tasks": [\n {\n "label": "build",\n "command": "g++",\n "args": ["${workspaceFolder}/src/*.cpp", "-o", "${workspaceFolder}/build"],\n "problemMatcher": {\n "owner": "cpp",\n "fileLocation": ["relative", "${workspaceFolder}"],\n "pattern": {\n "regexp": "^(.*):(\\\\d+):(\\\\d+):\\\\s+(warning|error):\\\\s+(.*)$",\n "file": 1,\n "line": 2,\n "column": 3,\n "severity": 4,\n "message": 5\n }\n }\n }\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\n这里还有一个使用代码嗅探的 php 任务!
\nsrc(再次谷歌搜索):https ://github.com/bmewburn/vscode-intelephense/issues/1102
\ntest.js\n 1:0 error Missing "use strict" statement strict\n\xe2\x9c\x96 1 problems (1 errors, 0 warnings)\nRun Code Online (Sandbox Code Playgroud)\n好的,问题匹配器如何提出自动修复建议?答案是没有!可以说清楚了!或者你可能会在第一次看到问题匹配器时说,嘿,这可能有关系!
\n不是这样的!语言支持或 linter 扩展提供了这样的功能!通过使用vscode Action api!
\n查看
\nhttps://code.visualstudio.com/api/references/vscode-api#CodeAction
\nhttps://code.visualstudio.com/api/references/vscode-api#CodeActionKind
\nhttps://code.visualstudio.com/api/references/vscode-api#CodeActionProvider%3CT%3E
\n很简单:问题匹配器设置如何解析任务运行的输出并在问题选项卡上输出!\n并且语言支持扩展实现自动修复!或者短绒棉绒!(扩展)[如果我愿意,我可以扩展游乐场]!
\n还要注意问题选项卡中的黄色灯泡!有效并允许自动修复!因为问题匹配器提供了问题的线路!这与扩展修复建议范围的输出进行映射!这在CodeActionProvider上得到了精确的体现!
\nAja*_*hav 20
更新2019
ES-Lint在VS Code中引入了一项新任务。您必须在工作空间设置中启用它。
"eslint.provideLintTask": true
Run Code Online (Sandbox Code Playgroud)
只需进入终端菜单并选择运行任务,然后选择
eslint: lint whole folder.
Run Code Online (Sandbox Code Playgroud)
您还可以通过在终端中运行以下命令来自动修复大多数问题:
.\node_modules\.bin\eslint.cmd --fix .
Run Code Online (Sandbox Code Playgroud)
参考:https : //marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint
尽管我们仍在等待VS Code中的问题扫描器,但是如果您使用eslint,这是一个很好的选择。
老答案
在这里,您可以在不到10秒的时间内看到所有问题。
您使用一些技巧。
打开替换所有文件Ctrl + Shift + H。
替换;为;
点击全部替换。而已。现在,检查问题。
这里的假设是所有文件都至少有一个分号。对于较大的项目,如果收到警告,要求您缩小搜索范围,则只需输入所有文件中常见但很少显示的内容。
| 归档时间: |
|
| 查看次数: |
15421 次 |
| 最近记录: |