简体   繁体   English

ESLint 导入排序分组

[英]ESLint import sort grouping

In my node project, I use ESLint.在我的节点项目中,我使用 ESLint。 I want to sort imports.我想对进口进行排序。 I configured it and now everything works fine.我配置了它,现在一切正常。 But, I can't group imports and sort.但是,我无法对导入进行分组和排序。

import request from 'request';

import { asyncHandler } from 'middleware';
import { githutOptions } from 'constants/options.constant';

import profileService from 'services/profile.service';

Here, I get a warning saying Imports should be sorted alphabetically.在这里,我收到一条警告,说Imports 应该按字母顺序排序。 on second line.在第二行。 (asyncHandler) (异步处理器)

Why doesn't this get sorted based on grouping?为什么这不根据分组进行排序? In another project I had worked with, if there are several grouped imports, the are sorted only inside those groups.在我合作过的另一个项目中,如果有多个分组导入,则仅在这些组内进行排序。

But here, even I grouped these imports, it consider the whole file, so asyncHanler should go as the first line.但是在这里,即使我将这些导入分组,它也会考虑整个文件,因此 asyncHanler 应该作为第一行。 What I want here is to sort import only inside groups.我在这里想要的是仅在组内对导入进行排序。

How can I achieve this?我怎样才能做到这一点?

These are my eslint rules for sorting imports.这些是我对导入进行排序的 eslint 规则。

 "sort-imports": ["error", {
      "ignoreCase": false,
      "ignoreDeclarationSort": false,
      "ignoreMemberSort": false,
      "memberSyntaxSortOrder": ["none", "all", "single", "multiple"]
  }]

That's exactly what ignoreDeclarationSort does: if it's true then ESLint will ignore import lines.这正是ignoreDeclarationSort所做的:如果它是true那么 ESLint 将忽略import行。 And if ignoreMemberSort is false it will check ordering for named imports.如果ignoreMemberSortfalse ,它将检查命名导入的排序。

So所以

 "sort-imports": ["error", {
      "ignoreCase": false,
      "ignoreDeclarationSort": true,
      "ignoreMemberSort": false,
      "memberSyntaxSortOrder": ["none", "all", "single", "multiple"]
  }]

should work for you.应该为你工作。

eslint added what you're looking for with the allowSeparatedGroups option of sort-imports in version 7.5.0 eslint 在allowSeparatedGroups版中使用sort-importsallowSeparatedGroups选项添加了您要查找的7.5.0

cf:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM