gpt4 book ai didi

typescript - 为什么 Visual Studio 2017 不在 TypeScript 文件上使用 ESLint?

转载 作者:搜寻专家 更新时间:2023-10-30 21:56:08 24 4
gpt4 key购买 nike

我在 Visual Studio 2017 中创建了一个 Web 应用程序并安装了依赖项。

package.json:

{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"devDependencies": {
"@typescript-eslint/parser": "1.7.0",
"eslint": "5.16.0",
"typescript": "3.4.5"
}
}

我已经使用最新的最佳实践为 TypeScript 设置了 ESLint,即 @typescript-eslint/parser

.eslintrc:

{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"extends": [ "eslint:recommended" ]
},
"rules": {
"quotes": [ "error", "double" ]
}
}

(注意需要双引号的规则。)

我创建了两个文件,test_js.jstest_ts.ts,内容相同:

var x = 'Hello, world';

但是只有JS文件显示引号lint错误:

Visual Studio screenshot showing red squiggles under the signle-quoted strings, but only in the JS file - there are no squiggles in the TS file.

为什么 Visual Studio 不检查我的 TypeScript 文件?

(交叉发布在 Visual Studio 开发人员社区:https://developercommunity.visualstudio.com/content/problem/552855/why-doesnt-visual-studio-2017-use-eslint-on-typesc.html)

最佳答案

更新:

VS现在does things well .请不要做我在下面做的事。升级 VS 并以正确的方式做事。

原答案:

我已经建立了一个临时的解决方法。

Visual Studio 2017 似乎尊重 tsconfig.json 中提供的 TSLint 设置。不幸的是,它还不接受 ESLint 的 TypeScript 规则。因此,如果我想在 VS 编辑器中检查 TypeScript,我必须使用旧的 tslint.json 样式配置。

tsconfig.json:

{
"compilerOptions": {
"plugins": [
{"name": "typescript-tslint-plugin"}
],
"target": "es5"
},
"exclude": [
"node_modules"
]
}

tslint.json:

{
"rules": {
"quotemark": [true, "double"]
}
}

为了面向 future (当 ESLint 最终吸收 TSLint 时),我希望我的构建过程使用 ESLint。

.michael-eslintrc(我将其命名为这样 VS 就不会使用此配置来检查 JS 文件):

{
"plugins": [
"@typescript-eslint/tslint"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 6,
"extends": [ "eslint:recommended" ],
"project": "tsconfig.json",
"sourceType": "module"
},
"rules": {
"@typescript-eslint/tslint/config": [
"warn",
{
"lintFile": "tslint.json"
}
]
}
}

这设置了我需要的所有依赖项。

package.json:

{
"version": "1.0.0",
"name": "asp.net",
"private": true,
"devDependencies": {
"@typescript-eslint/parser": "1.7.0",
"@typescript-eslint/eslint-plugin-tslint": "1.7.0",
"eslint": "5.16.0",
"tslint": "5.16.0",
"typescript": "3.4.5",
"typescript-tslint-plugin": "0.3.1"
}
}

我已将 Webpack 配置为使用此 ESLint 配置进行 linting。所以 Visual Studio 和 Webpack 最终使用相同的 tslint.json 配置,但它们通过不同的路径达到相同的行为。

此解决方法有几个缺点:

  1. 我不能将新的 ESLint 原生规则用于 TypeScript linting
  2. 一旦 VS 最终支持使用 ESLint 的 TS linting,我将不得不更新配置
  3. 这只会为 TS 设置 lint 规则,而不是 JS - 即使没有 TS 规则,我也很难让 VS 处理 .eslintrc(这对我来说没什么影响,因为我正在将一个旧项目转换为TS - 如果 JS 被检查,很多文件会失败)

关于typescript - 为什么 Visual Studio 2017 不在 TypeScript 文件上使用 ESLint?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55911040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com