簡體   English   中英

加載規則“@typescript-eslint/dot-notation”時出錯

[英]Error while loading rule '@typescript-eslint/dot-notation'

今天跑了eslint,兩個腳本

 "lint-staged": "lint-staged",
 "eslint": "eslint --ext .tsx,.ts --fix ./src -c .eslintrc.js",

當我運行npm run eslint // 沒問題 當我運行npm run lint-staged // 它是錯誤的

lint-staged 的結果;

> lint-staged
  ❯ Running tasks for src/**/*.tsx
    ✖ eslint --fix [FAILED]
    ◼ git add
  
✖ eslint --fix :


Oops! Something went wrong! :(

ESLint: 7.10.0

Error: Error while loading rule '@typescript-eslint/dot-notation': You have used a rule which requires parserServices to be generated. You must therefore provide a value for the "parserOptions.project" property for @typescript-eslint/parser.
Occurred while linting /Users/***/components/BrowserInfo/Index.tsx
    at Object.getParserServices (/Users/fugang/workspace/xinao/channel-desk/node_modules/_@typescript-eslint_experimental-utils@4.3.0@@typescript-eslint/experimental-utils/dist/eslint-utils/getParserServices.js:26:15)

在 package lint 階段

發生了什么事?

"lint-staged": {
    "src/**/*.tsx": [
      "eslint --fix -c .eslintrc.js",
      "git add"
    ]
  },

您必須將以下配置添加到 .eslint.json 文件中

{
    "parserOptions": {
        ...
        "project": ["path/to/your/tsconfig/file"]
        ...
    },
}

如果任何人在外面使用.eslintrc.yml可以使用以下來解決上述問題

extends:
  - airbnb-typescript
parserOptions:
  project:
    - tsconfig.json

PS: tsconfig.json在根文件夾中,如果它不在根文件夾中,請更改它以匹配您的位置。

這是我最終放入 my.eslintrc.json 的內容:

module.exports = {
  ...
  parserOptions: {
    parser: '@typescript-eslint/parser',
    project: './tsconfig.json',
    tsconfigRootDir: __dirname,
  },
};

可能是項目需要./tsconfig.json而不是tsconfig.json 我的 tsconfig 文件在項目根目錄中。

這帶來了更多錯誤,例如 babel 配置,所以我創建了一個.eslintignore文件並在其中添加了這些文件:

.eslintrc.js
ios/
android/
*.config.js

這解決了我所有的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM