首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >eslint import/order react类型脚本

eslint import/order react类型脚本
EN

Stack Overflow用户
提问于 2021-01-07 04:13:53
回答 1查看 4.6K关注 0票数 2

我想要将eslint import/order配置为自定义配置,但我无法在react typescript文件中使用它。

这是我的规则:

代码语言:javascript
复制
"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文件:

代码语言:javascript
复制
{
  "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依赖项

代码语言:javascript
复制
"@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中

代码语言:javascript
复制
 "baseUrl": "src",

该规则在.ts文件中运行良好:

rule-working-ts

但它不适用于.tsx文件。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-07 05:02:32

已通过添加此依赖项修复

代码语言:javascript
复制
yarn add -D eslint-import-resolver-typescript
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65602807

复制
相关文章

相似问题

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