首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误与我的".eslintrc.js“文件-”解析错误:"parserOptions.project“已设置为@typescript-eslint/解析器。”

错误与我的".eslintrc.js“文件-”解析错误:"parserOptions.project“已设置为@typescript-eslint/解析器。”
EN

Stack Overflow用户
提问于 2020-10-08 22:36:45
回答 2查看 19.8K关注 0票数 15

ESLint似乎无法解析".eslintrc.js“文件

复制步骤:我设置了一个新的"hello world“TypeScript项目,如下所示:

代码语言:javascript
运行
复制
# Make a new directory for our new project
mkdir test
# Go into the new directory
cd test
# Create a "package.json" file
npm init --yes
# Install TypeScript
npm install typescript --save-dev
# Install ESLint (the linter)
npm install eslint --save-dev
# Install the Airbnb ESLint config (the most popular linting config in the world)
npm install eslint-config-airbnb-typescript --save-dev
# The import plugin for ESLint is needed for the Airbnb config to work properly with TypeScript
npm install eslint-plugin-import@^2.22.0 --save-dev
# The TypeScript plugin for ESLint is needed for the Airbnb config to work properly with TypeScript
npm install @typescript-eslint/eslint-plugin@^4.2.0 --save-dev
# Create the config file for TypeScript
touch tsconfig.json
# Create the config file for ESLint
touch .eslintrc.js
# Create the entry point for our TypeScript application
touch main.ts

我用以下内容填充"tsconfig.json“文件(空白/默认配置):

代码语言:javascript
运行
复制
{}

我用以下内容填充".eslintrc.js“文件,如Airbnb文件中所指定的

代码语言:javascript
运行
复制
module.exports = {
  extends: ['airbnb-typescript/base'],
  parserOptions: {
    project: './tsconfig.json',
  },
};

我在"main.ts“中填写以下内容:

代码语言:javascript
运行
复制
const foo = 'bar';

然后,当我运行npx eslint main.ts时,它正确地生成以下错误:

代码语言:javascript
运行
复制
  1:7  error  'foo' is assigned a value but never used  @typescript-eslint/no-unused-vars

因此,ESLint似乎工作正常。但是,当我运行npx eslint .eslintrc.js时,会得到以下错误:

代码语言:javascript
运行
复制
  0:0  error  Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: .eslintrc.js.
The file must be included in at least one of the projects provided

每当我打开".eslintrc.js“文件时,这个错误也会出现在.eslintrc.js中。需要解决该错误,以便ESLint可以将文件的其余部分链接起来。(为了澄清,我希望".eslintrc.js“文件的排列方式与我希望我的TypeScript源代码被排列的方式相同--例如,有2个空格缩进等等)。

附加信息:我在Windows 10 LTSC与Nodev14.8.0和npm版本6.14.7。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-09 15:39:55

当您使用类型记录驱动的eslint规则(当您的eslint配置包括一个"parserOptions.project"配置时),如果您试图链接一个不是类型记录项目中包含的文件之一的文件,eslint就会抛出一个错误。

这是由于性能方面的原因--以前是eslint允许这样做的,但是这样做会造成很大的性能损失,所以他们将其更改为引发错误。

不属于TS项目一部分的linting文件的解决方案是创建一个tsconfig,它扩展了您的“真正的”tsconfig,但是包含了您想要链接的所有文件。这通常称为tsconfig.eslint.json,如下所示:

代码语言:javascript
运行
复制
// Special typescript project file, used by eslint only.
{
    "extends": "./tsconfig.json",
    "include": [
        // repeated from base config's "include" setting
        "src",
        "tests",

        // these are the eslint-only inclusions
        ".eslintrc.js",
    ]
}

在您的.eslintrc.js中,您将把project: './tsconfig.json'更改为project: './tsconfig.eslint.json'

这将解决错误,并允许对.eslintrc.js文件进行链接。

票数 41
EN

Stack Overflow用户

发布于 2021-10-19 10:44:20

雷萨姆的回答工作得非常好。但是,您也只能使用@ TypeScript eslint/解析器对TypeScript文件使用ESLint覆盖。这避免了只为ESLint创建一个新的ESLint。

代码语言:javascript
运行
复制
module.exports = {
  // Common configuration for .eslintrc.js and TypeScript files
  rules: {
    eqeqeq: "error",
    "no-var": "error",
    // ...
  },
  overrides: [{
    files: "**/*.ts"
    // TypeScript-only configuration
    parser: "@typescript-eslint/parser",
    parserOptions: {
      project: "tsconfig.json",
      tsconfigRootDir: __dirname,
      sourceType: "module",
    },
    plugins: ["@typescript-eslint"],
    rules: {
      "@typescript-eslint/no-floating-promises": "error",
      // ...
    }
  ]},
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64271575

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档