简体   繁体   English

如何在VS代码中使用TSLint?

[英]How can I use TSLint in VS Code?

I have installed TSLint in VSCode and created a tslint.json file next to tsconfig.json . 我在VSCode安装TSLint并创建了一个tslint.json旁边文件tsconfig.json But TSLint is not working. 但是TSLint没有用。 For example, I added "curly": true to tslint.json , but when I write a if statement without curly braces, VS Code doesn't give any warning. 例如,我添加了"curly": truetslint.jsontslint.json ,但是当我编写没有花括号的if语句时,VS Code不会给出任何警告。 What does this extension do? 这个扩展做了什么?

在此输入图像描述

On a new machine, I installed VS Code tslint extension before installing tslint itself (via npm), and nothing helped to make it work other than disabling and re-enabling the extension. 在新机器上,我在安装tslint之前安装了VS Code tslint扩展(通过npm),除了禁用和重新启用扩展之外没有任何帮助使其工作。

在此输入图像描述

The vscode-tslint extension currently crashes silently when it encounters an invalid config-option. 当vscode-tslint扩展遇到无效的config-option时,它会以静默方式崩溃。 In my case, it was the no-trailing-comma rule which has to be changed to trailing-comma . 就我而言,这是一个no-trailing-comma规则,必须改为trailing-comma

More info here: https://github.com/Microsoft/vscode-tslint/issues/66 更多信息: https//github.com/Microsoft/vscode-tslint/issues/66

VS Code doesn't give any warning. VS Code不会发出任何警告。 What does this extension do 这个扩展做了什么

When in doubt. 有疑问时。 Restart VSCode. 重启VSCode。

In my case it was the .vscode/tasks.json file. 在我的例子中,它是.vscode/tasks.json文件。 I removed and recreated the file and its all working fine now. 我删除并重新创建了文件,现在一切正常。

Make sure that you've got a valid tslint.json file in your working directory root. 确保您的工作目录根目录中有一个有效的tslint.json文件。 There's a good guide here if you scroll down to the readme - https://github.com/palantir/tslint 如果您向下滚动到自述文件,这里有一个很好的指南 - https://github.com/palantir/tslint

Try making a deliberate error to a TS file, and you should see that the error gets underlined with a squiggly line. 尝试对TS文件进行故意的错误,你应该看到错误用一条波浪线加下划线。 I 一世

Note: I'm using VSCode 1.3.1, and vscode-tslint 0.5.32. 注意:我使用的是VSCode 1.3.1和vscode-tslint 0.5.32。

I had the same problem as you. 我遇到了和你一样的问题。 For some reason after updating either TSLint or Visual Studio Code, linting stopped working. 由于某种原因,在更新TSLint或Visual Studio代码后,linting停止工作。 After cloning the project Zen recommended in the comments, I received an error saying that TSLint wasn't installed. 克隆了评论中推荐的Zen项目后,我收到一条错误消息,指出没有安装TSLint。 I installed TSLint globally but not as a dev dependency for my project so after running "npm install tslint --save-dev" Visual Studio Code started linting again. 我在全局安装了TSLint,但没有作为我的项目的dev依赖项,因此在运行“npm install tslint --save-dev”后,Visual Studio Code再次启动了linting。

One possibility is that your tslint.json file may not be in proper JSON format. 一种可能性是您的tslint.json文件可能不是正确的JSON格式。 The tslint.json file as shown when opened in VSCode may analyze it for errors using its jsonc parser , which does not show errors when the last key-value pair has a trailing comma (which is invalid in plain JSON). 在VSCode中打开时显示的tslint.json文件可以使用其jsonc解析器分析它的错误,当最后一个键值对有一个尾随逗号(在普通JSON中无效)时,它不显示错误。 But, the linting process (or at least ms-vscode.vscode-typescript-tslint-plugin ) will silently fail if the tslint.json is not actual JSON. 但是,如果tslint.json不是实际的JSON,则linting进程(或至少ms-vscode.vscode-typescript-tslint-plugin )将无声地失败

For example, the following will result in a silent failure, without any indication of where the problem is, due to the trailing comma: 例如,由于尾随逗号,以下内容将导致静默失败,而不会显示问题所在的位置:

{
  "extends": "tslint:latest",
  "rules": {
    "ordered-imports": false,
    "prefer-for-of": false,
  }
}

Whereas the following will work as expected: 以下内容将按预期工作:

{
  "extends": "tslint:latest",
  "rules": {
    "ordered-imports": false,
    "prefer-for-of": false
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM