1

我正在尝试在 Visual Studio 2019 中配置 ESLint,我在 Options 中启用了 ESLint,并且我在我的项目中的 node_modules 中安装了 ESLint 7.4.0,这应该会导致 Visual Studio 根据本文档使用该版本。但这会导致一个不太有用的错误。

eslint .从与文件相同的目录在命令行中运行时,.eslintrc.yml它会工作并报告有关我的代码的错误和警告。

在 Visual Studio 中查看或文件时.js,它会在文件的第一个字符处报告以下错误。没有报告其他错误(命令行确实报告错误)。Visual Studio 确实在这些文件中报告了常见的智能感知 TypeScript 错误。.ts.tsxinternal-error: (ESLint) Unexpected token {

Visual Studio 2019 中的错误

以下是我的 .eslintrc.yml 文件。

---
parser: "@typescript-eslint/parser" # Specifies the ESLint parser
parserOptions:
    ecmaFeatures:
        jsx: true # Allows for the parsing of JSX
settings:
    react:
        version: detect # Tells eslint-plugin-react to automatically detect the version of React to use
extends:
    - plugin:react/recommended # Uses the recommended rules from @eslint-plugin-react
    - plugin:react-hooks/recommended # Uses the recommended rules from @eslint-plugin-react-hooks
    - plugin:@typescript-eslint/recommended # Uses the recommended rules from the @typescript-eslint/eslint-plugin

有没有办法查看 Visual Studio 正在执行什么以及导致此错误的原因?

我尝试运行devenv.exe /log,但这没有什么有趣的记录,只是用 eslint 做了一些事情。正如你在下面看到的。

821  Begin export of category 'TypeScript_EsLint' ({d5d56c12-87c4-46f2-894f-3a75ee781f48}) from package 'Microsoft.VisualStudio.LanguageServices.TypeScript.TypeScriptPackage, Microsoft.VisualStudio.LanguageServices.TypeScript, Version=16.0.0.0, Culture=neutral' ({2FFE45C4-5C73-493C-B187-F2E955FF875E}). 3095452 KB of virtual memory currently available.   Profile manager 2020/08/19 08:11:21.001 
822  End export of category 'TypeScript_EsLint' ({d5d56c12-87c4-46f2-894f-3a75ee781f48}) from package 'Microsoft.VisualStudio.LanguageServices.TypeScript.TypeScriptPackage, Microsoft.VisualStudio.LanguageServices.TypeScript, Version=16.0.0.0, Culture=neutral' ({2FFE45C4-5C73-493C-B187-F2E955FF875E}). 3095452 KB of virtual memory currently available.   Profile manager 2020/08/19 08:11:21.003  
4

1 回答 1

0

我遇到了几乎完全相同的问题,感谢@Qurben评论中链接的文章,我通过以下方式解决了它:

  1. 降级到 ESLint 6.8,因为 ESLint 7 需要 Node.js 12,而旧版本的 Visual Studio 2019 运行 Node.js 8。这可能不再需要。
  2. 添加parserOptions.tsconfigRootDir = __dirname到 .eslintrc.js(如本指令所示。请注意,这可能在 .eslintrc.yml 或 .eslintrc.json 中不起作用。
于 2021-09-29T10:11:03.540 回答