“parserOptions.project” has been set for @typescript-eslint/parser

半城伤御伤魂 提交于 2020-11-24 16:32:07

问题


I created a new React Native project with --template typescript

I deleted the template directory which came as part of the boilerplate.

I then proceeded to add ESLint:

module.exports = {
  parser: "@typescript-eslint/parser",
  plugins: ["@typescript-eslint"],
  extends: ["airbnb-typescript-prettier"]
};

However, when I open babel.config.js, I get this error

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser. The file does not match your project config: /Users/Dan/site/babel.config.js. The file must be included in at least one of the projects provided.eslint


回答1:


You can create a separate TypeScript config file (tsconfig.eslint.json) intended for eslint configuration. That file extends tsconfig configuration and setups include key for files that have to be linted.

.eslint.js:

// ...
parserOptions: {
  // ...
  project: "./tsconfig.eslint.json",
  // ...
},
// ...

tsconfig.eslint.json:

{
  "extends": "./tsconfig.json",
  "include": [
    // ...
    "babel.config.js"
  ]
}

Or if you want to ignore it, you can put it into .eslintignore.

.eslintignore:

// ...
babel.config.js



回答2:


As suggested in this issue, you can add createDefaultProgram: true to your parser options as a temporary solution, albeit at the expense of potentially making your IDE more sluggish.

{
  ...
  parserOptions: {
    project: './tsconfig.json',
    createDefaultProgram: true,
  },
}



回答3:


You need to add that file to your tsconfig include array. See typescript-eslint/typescript-eslint#967 for more details.




回答4:


In my projects a proper config I use is as follows:

tsconfig.json

{
  "compilerOptions": {
    /* Basic Options */
    "target": "es5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */,
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
    "resolveJsonModule": true,
    "lib": [
      "es2017",
      "es7",
      "es6",
      "dom"
    ] /* Specify library files to be included in the compilation. */,
    "declaration": true /* Generates corresponding '.d.ts' file. */,
    "outDir": "lib" /* Redirect output structure to the directory. */,

    /* Strict Type-Checking Options */
    "strict": true /* Enable all strict type-checking options. */,
    "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,

    /* Advanced Options */
    "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */,
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    }
  },
  "include": ["src/**/*.ts"]
}

tsconfig.eslint.json

{
  "extends": "./tsconfig.json",
  "include": [
    "src/**/*.ts",
    ".eslintrc.js",
    "tests/**/*.ts",
    "jest.config.js",
    "examples/**/*.ts"
  ],
  "exclude": ["node_modules"]
}

.eslintrc.js

module.exports = {
  root: true,
  env: {
    node: true,
  },
  globals: {
    Promise: "readonly"
  },
  parser: "@typescript-eslint/parser",
  parserOptions: {
    sourceType: "module",
    tsconfigRootDir: __dirname,
    project: ["./tsconfig.eslint.json"],
  },
  plugins: ["@typescript-eslint"],
  extends: [
    "eslint:recommended",
    "prettier/@typescript-eslint",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking",
  ],
}



回答5:


I use a symlink from my project folder point to my home folder:

/opt/project/workspace => ~/worspace

Opening the VSCode using the symlink path ~/workspace the error always persists.

Opening VSCode using the original path: /opt/project/workspace solves the problem.

This shouldn't be a problem, but for now it is.




回答6:


The problem happens for one of the reasons below:

  1. You're using a rule which require type information and didn't specify a parserOptions.project;
  2. You specified parserOptions.project, didn't specify createDefaultProgram (it will be removed in a future version), and you're linting files not included in the project (e.g. babel.config.js, metro.config.js)

As from the TypeScript ESLint Parser docs:

parserOptions.project

This option allows you to provide a path to your project's tsconfig.json. This setting is required if you want to use rules which require type information.

(...)

Note that if this setting is specified and createDefaultProgram is not, you must only lint files that are included in the projects as defined by the provided tsconfig.json files. If your existing configuration does not include all of the files you would like to lint, you can create a separate tsconfig.eslint.json.

To solve it, update your ESLint config to the following:

{
  // ...
  overrides: [
    {
      files: ['*.ts', '*.tsx'], // Your TypeScript files extension
      parserOptions: {
        project: ['./tsconfig.json'], // Specify it only for TypeScript files
      },
    }
  ],
  parser: '@typescript-eslint/parser',
  // ...
}



回答7:


In our case we have multiple .eslintrc and multiple tsconfig.json in our directory tree. (One each for the server at ., and one each for the React client at ./client.)

This worked fine with the CLI but not with VS Code's plug-in.

The fix was to set the ./client/.eslintrc project value to be relative to the root - not to the .eslintrc file. After changing it from "./tsconfig.json" to "./client/tsconfig.json" the IDE linting works as expected.




回答8:


I had this issue in VsCode when I was opening my Project too high up in the Folder Structure. A weird solution but it worked.

In my example, Firestore functions project for express in .ts, I had to open it from the functions folder it created.

I should have noticed it was this sort of problem since 'npm run-script lint' never returned an error itself.




回答9:


You need to include typescript support for *.config.js files:

tsconfig.json:

{
  "include": [
    "**/*.config.js" // for *.config.js files
  ]
}

And then reload your IDE!



来源:https://stackoverflow.com/questions/58510287/parseroptions-project-has-been-set-for-typescript-eslint-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!