关于eslint:eslintpluginimport-规则之-Import-Order

顾名思义,是对导入的模块进行排序,成果如下:

比照排序前后代码,排序后的代码看起来更整洁,react**全家桶内部库公共组件子组件api工具类顺次排列。

罕用规定配置

{
  "import/order": ["error", {
        "groups": [
          "builtin",
      "external",
      ["internal", "parent", "sibling", "index"],
      "unknown",
        ],
    "pathGroups": [
      {
        "pattern": "@app/**",
        "group": "external",
        "position": "after"
      }
    ],
    "pathGroupsExcludedImportTypes": ["builtin"],
        "newlines-between": "always",
        "alphabetize": {
            "order": "asc",
            "acseInsensitive": true
        }
  }]
}

其中,group 是对导入模块进行分组,pathGroups 是通过门路自定义分组,newlines-between 不同组之间是否进行换行,alphabetize 依据字母程序对每个组内的程序进行排序

重点介绍下 pathGroups 属性,该属性有 4 个子属性:

  • pattern:以后组中模块的最短门路匹配
  • patternOptions:如果须要更准确的匹配,看这里
  • group:在规定的组当选其一,indexsiblingparentinternalexternalbuiltinobjecttypeunknown
  • position:定义组的地位,afterbefore

对于 pathGroupsExcludedImportTypes 的作用愣是没看懂,欢送大家补充。

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理