简体   繁体   中英

ESLint: how to sort import members with eslint-plugin-import?

I'm trying to add the sorting of import members such as this rule in sort-member .

However, since I'm already using eslint-plugin-import , there is a conflict because each of these rules is trying to sort imports based on different parameters:

  • sort-member sorts following the order ["none", "all", "multiple", "single"] , depending on how many imports there is from one module
  • eslint-plugin-import sorts following the order ["builtin", "external", "parent", "sibling", "index"] , depending on the type of import

Is there a way to only use the member sort of sort-member (== ignore the memberSyntaxSortOrder rule), or is there a feature similar to memberSort built in eslint-plugin-import ?

All I'm trying to do is to have an error for:

import { b, a, c } from 'foo.js'

And have it automatically fixed to:

import { a, b, c } from 'foo.js'

I ended up finding the solution by myself: despite memberSyntaxSortOrder being mandatory and only accepting an array, you can actually ignore this rule by simply using the ignoreDeclarationSort rule, set to true .

So to make sort/import work with an alphabetical order of multiple import lines, simply add in your rule block:

'sort-imports': [
  'error',
  {
    ignoreDeclarationSort: true,
  },
],

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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