加载规则“@typescript-eslint/dot-notation”时出错

leo*_*ofu 10 javascript eslint husky lint-staged

今天我运行 eslint?two 脚本

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

Run Code Online (Sandbox Code Playgroud)

当我运行时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)
Run Code Online (Sandbox Code Playgroud)

在包 lint-staged 中

发生了什么事?

"lint-staged": {
    "src/**/*.tsx": [
      "eslint --fix -c .eslintrc.js",
      "git add"
    ]
  },
Run Code Online (Sandbox Code Playgroud)

Vla*_*vić 33

您必须将以下配置添加到 .eslint.json 文件中

{
    "parserOptions": {
        ...
        "project": ["path/to/your/tsconfig/file"]
        ...
    },
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢@Vladimir Jovanović。在我的项目中,我添加了如下内容,因为“tsconfig.json”和“.eslintrc.json”处于同一级别。"parserOptions": { "project": ["tsconfig.json"] } (9认同)
  • 这个答案看起来很有希望并解决了该错误,但导致在`.eslintrc.js`文件本身的顶部出现了这个新的Eslint错误:`解析错误:“parserOptions.project”已为@typescript-eslint/parser设置。该文件与您的项目配置不匹配:.eslintrc.js。该文件必须至少包含在提供的项目之一中。 /sf/ask/4095720121/ 似乎相关,但我还没有得到任何可用的答案。 (6认同)
  • @RamyaVelivala 你有没有弄清楚你的问题?我也经历着同样的事情 (2认同)

jus*_*ird 10

如果有人在外面使用.eslintrc.yml可以使用以下方法来解决上述问题

extends:
  - airbnb-typescript
parserOptions:
  project:
    - tsconfig.json
Run Code Online (Sandbox Code Playgroud)

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