Eslint导入/订单反应打字稿 [英] eslint import/order react typescript

查看:29
本文介绍了Eslint导入/订单反应打字稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将eslint导入/订购配置为自定义配置,但我无法使其在REACT类型脚本文件中工作。

以下是我的规则:

"import/order":[
      "error",
      {
        "groups": ["builtin", "external", "internal"],
        "pathGroups":[{
          "pattern": "react",
          "group": "external",
          "position": "before"
        }],
        "pathGroupsExcludedImportTypes": ["react"],
        "newlines-between": "always",
        "alphabetize": {
          "order": "asc",
          "caseInsensitive":true
        }
      }
    ]

.eslintrc.json文件:

{
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 6,
    "project": "./tsconfig.json"
  },
  "env": {
    "browser": true,
    "jest": true,
    "es6": true
  },
  "extends": [
    "airbnb-typescript",
    "airbnb/hooks",
    "prettier",
    "prettier/react",
    "plugin:@typescript-eslint/recommended",
    "plugin:import/errors",
    "plugin:import/warnings",
    "plugin:import/typescript",
    "plugin:jest/recommended",
    "prettier/@typescript-eslint",
    "plugin:prettier/recommended"
  ],
  "plugins": [
    "react",
    "react-hooks",
    "import",
    "@typescript-eslint",
    "prettier",
    "jest"
  ],
  "rules": {
    "no-console": 2,
    "prettier/prettier": [
      "error",
      {
        "singleQuote": true
      }
    ],
    "react-hooks/rules-of-hooks": "error",
    "react-hooks/exhaustive-deps": "off",
    "react/jsx-filename-extension": [
      "off"
    ],
    "react/jsx-closing-bracket-location": [
      1,
      "tag-aligned"
    ],
    "import/no-extraneous-dependencies": 1,
    "import/order":[
      "error",
      {
        "groups": ["builtin", "external", "internal"],
        "pathGroups":[{
          "pattern": "react",
          "group": "external",
          "position": "before"
        }],
        "pathGroupsExcludedImportTypes": ["react"],
        "newlines-between": "always",
        "alphabetize": {
          "order": "asc",
          "caseInsensitive":true
        }
      }
    ]
  },
  "settings": {
    "import/resolver": {
      "node": {
        "extensions": [".ts", ".tsx"],
        "moduleDirectory": [
          "node_modules",
          "src/"
        ]
      },
      "typescript": {
        "alwaysTryTypes": true
      }
    },
    "react": {
      "version": "detect"
    }
  }
}

当前的Package.json依赖项

"@typescript-eslint/eslint-plugin": "^4.9.1",
    "@typescript-eslint/parser": "^4.9.1",
    "eslint": "7.2.0",
    "eslint-config-airbnb": "18.2.1",
    "eslint-config-airbnb-typescript": "^12.0.0",
    "eslint-config-prettier": "^7.0.0",
    "eslint-plugin-import": "^2.22.1",
    "eslint-plugin-jest": "^24.1.3",
    "eslint-plugin-jsx-a11y": "^6.4.1",
    "eslint-plugin-prettier": "^3.2.0",
    "eslint-plugin-react": "^7.21.5",
    "eslint-plugin-react-hooks": "4.0.0",

也在tsconfig.json中

 "baseUrl": "src",

该规则在.ts文件中运行正常:

rule-working-ts

但它不适用于.tsx文件。

推荐答案

已通过添加此依赖项修复

yarn add -D eslint-import-resolver-typescript

这篇关于Eslint导入/订单反应打字稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆