sort-imports

在模块中强制执行排序的导入声明

一些该规则报告的问题可以通过 --fix 命令行选项 自动修复

import 语句用于导入已从外部模块导出的成员(函数、对象或原语)。使用特定的成员语法:

// single - Import single member.
import myMember from "my-module.js";
import {myOtherMember} from "my-other-module.js";

// multiple - Import multiple members.
import {foo, bar} from "my-module.js";

// all - Import all members, where myModule contains all the exported bindings.
import * as myModule from "my-module.js";

import 语句也可以在没有导出绑定的情况下导入模块。当模块不导出任何内容,但运行它自己的代码或更改全局上下文对象时使用。

// none - Import module without exported bindings.
import "my-module.js"

在声明多个导入时,导入声明的排序列表使开发人员更容易阅读代码并在以后找到必要的导入。这条规则纯粹是风格问题。

规则详情

x3SxIi1kId8l/s9qZ6MIh1kswbQpGZthA1qyAHkH6gfTD9+V5g+2LfhEjU2nCNNA0CtFX6jZpzdcCSJIHSkl70kG5DXGL28mOaDJfLvdCR8p+MMQOwpGbLv3/CtpvicxJ9IPrr9BJ13aZyjoJsv3UHeGHtZDW7sldV+2WJXk0F5jfDT8yJmdFRjZTaoKsQ9F9DX8l/HGzLSZm5yysH3g6Q==

1hCVJARDp5ub1z2nUkbo9vfB/SwzMQD/I9/vljE34hdiRPM+by//BZGc+iMjyXKUBKShxTbRrMmSJNT+OR7bO3vR1I0bEbsqYsNpAKLrta8l0zXh4KafPF4x732PftZN4g6/m76s2aGwUSgqFIUd1OT9B1nHTFO4zsp/HODbfoC4JJkfQsG0kamcnRueC/OFpR0AKkoSF0a4xE0CQBLVpo0lmxCJlHin0xkckVjkI9Jo8UkLH/q5bjgJNu19/Kf6V2KaTUGzWl6JnA50rfSSJqfnbwGtQERWVNMxz41KEvFpoLAZIBddDiVsHQ0AJslhEnt0rL+wk6d39+EuMpGiemoc1PWEm6oOVH9XyGHXGuE=

选项

H6cZnBKVWBeuvpxchmS6US3/lzig0O68vdGQV4FdMvTAT0njERD6mAyqIfS+kxl9

    UrkSo5UUyMXGOWlXefSj+5C+zG61ybAjOUZIHxhabEC2QtFI/jADkRP3UegIW5NQf1vwgb6zbZRFDJ5bQH4tNjc4FK+o5PZnVhGt1IP/JFF7Uy8PuvY39tSyfV4HctYazomnGVyaOtwFfldXjZ0M/n1JhVaPjQWyyLe257XSzihMVxb7DEWIHjbgAFLKNC3GjZ9aasib8birsExUaYa3/+TBhGxujzw4+xfEe8cup5DD736gdWmQXwjj/LpvU0pUhj4hzWGqIT2/EFntlomZDRBnjjVn5rfHkugo+FW+wFC9cBWhMKtnR5LRjJt4959VNlhtmaSece37QWzlDP3RizNMvtm8xnt/bzq3byJvE5Gc11Sr6nazgg1Q8snrGTzbCpd6xNRA1XpxUCXt8xiTAbvmvEoyMxY0MZY9vxlAP2B2Ssq54OWygMI4KVQohJ2TCpYaCNHpsLmBD10j6ptbTJRa+rzFAkAdgmefrACa+nA4Iz+vgq+z1On6DgDDyQD9p3rRNKqB/BpdYEgsvYOI6oMk1kI/OAntEmZmyabH4U+FyeQJRTnOej2Pg/8BIbprQZcGMS6aNFydMYblmyg9i52gYjI17Kzd9xMHhA88zMvKwJJTj32FsCHMmWnksD1odY0Vua3ime5bqPgVK5+sbVqhy8hl5V5EhXAu49SzTwdajfSN8LVXVahW8RfltXyBt8RxXJn2/6iAzrusrxtzs5XgxXewievED0usIJ7uX5vYoxRqB1W4ebKbS0u73IYWn+EDXsiDpcgV5yfKkVHb1aVQQQwRi74jPevmoVagd4gibaSCVIhB+TLNxqwjflfr9V3mh7ZGLgCiNSrkI1y9hAvUhSUcwPNdJCCxpSiYst6DACH9cA6E7CWddBd5jYa07B7gp71T1mLD6qztZBafwRH/77pUJAtpBhcbvEbGpUUohOzQ1LLhZdn0Z5KCUBqsDUGR1+zMUrwe+dnEitxgTA==

ehBdM/b4RHoTsILjxavuE2w78uEUWhJ9UTEGsjANOxM=

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

示例

默认设置

y28nUNQTkDSY5QylD+lTV9Z0FYLjLD1TOKG5fPqjEMsprrQ7yTZ0zXCWGRd6Hw/PI1OAavpp0zDjfxtW9pJ9Ng==

/*eslint sort-imports: "error"*/
import 'module-without-export.js';
import * as bar from 'bar.js';
import * as foo from 'foo.js';
import {alpha, beta} from 'alpha.js';
import {delta, gamma} from 'delta.js';
import a from 'baz.js';
import {b} from 'qux.js';

/*eslint sort-imports: "error"*/
import a from 'foo.js';
import b from 'bar.js';
import c from 'baz.js';

/*eslint sort-imports: "error"*/
import 'foo.js'
import * as bar from 'bar.js';
import {a, b} from 'baz.js';
import c from 'qux.js';
import {d} from 'quux.js';

/*eslint sort-imports: "error"*/
import {a, b, c} from 'foo.js'

y28nUNQTkDSY5QylD+lTV9Z0FYLjLD1TOKG5fPqjEMufp9SeXqvrvkS3P0nrqx3ipkZMfZeYcKZa94xn/J0KFg==

/*eslint sort-imports: "error"*/
import b from 'foo.js';
import a from 'bar.js';

/*eslint sort-imports: "error"*/
import a from 'foo.js';
import A from 'bar.js';

/*eslint sort-imports: "error"*/
import {b, c} from 'foo.js';
import {a, b} from 'bar.js';

/*eslint sort-imports: "error"*/
import a from 'foo.js';
import {b, c} from 'bar.js';

/*eslint sort-imports: "error"*/
import {a} from 'foo.js';
import {b, c} from 'bar.js';

/*eslint sort-imports: "error"*/
import a from 'foo.js';
import * as b from 'bar.js';

/*eslint sort-imports: "error"*/
import {b, a, c} from 'foo.js'

ignoreCase

fL4tT0ay+W9+tvye9LDC7IDvbT9rE8zvttmj4/gSKNRo3LQVwM4jeoY3dlfvWTQaxKf9sva5Z2aodIEF6c5+6TRXiiqsGVj5dBpB0xhDSyg=

gXTXeFewMhKUGG8XamsAXTKhAZutx9iirG71aJrnlP+3iOQk0Z1LHr+Q1Mjr+i0XJbx4GhoK6VlI5SAiDD3g4C/z65pnH51Ero492HGj1PIZAUy6OtNOGMK3gpSRTCEQ

/*eslint sort-imports: ["error", { "ignoreCase": true }]*/

import B from 'foo.js';
import a from 'bar.js';

gXTXeFewMhKUGG8XamsAXTKhAZutx9iirG71aJrnlP+3iOQk0Z1LHr+Q1Mjr+i0Xmu8PJ0B+sK/2XwR+ScwA40aegMefjvidHmFv1NdQnNfqzerOQHI8t5PKiwFJrC0f

/*eslint sort-imports: ["error", { "ignoreCase": true }]*/

import a from 'foo.js';
import B from 'bar.js';
import c from 'baz.js';

pVILD4QIu1ZDjUKBOVzHJ/QNmmiY9n899Z0N8aIkPG8=

ignoreDeclarationSort

JNUzv0l+lMIwc5sSPDH8letV5qipG8HbufvIXoixLLFLNHK7l173cnctyQ4RGJ66

xpQlHQir/OjetBCVQdvE2Tdsr06tKHweboRcgMJGRoz+sD4aaAHzalUmib7Yj8nzk3ofS7F06RcwiSOM8Q5KRb7jVnu5XtsSz2q1AIMqxYXNm5q4rGfcMs4wXM72R5H9pmVwl0zYU7Pw5PJqtpmfgg==

/*eslint sort-imports: ["error", { "ignoreDeclarationSort": false }]*/
import b from 'foo.js'
import a from 'bar.js'

gXTXeFewMhKUGG8XamsAXcLCC/tDp04Lp0Y3h2aKYLG0PLV5g7y4yyxQU08mdNvoFfwyMA3VTauPF3vtPu2/cODHWvuYdTthVC8guCsY76cnNEmQ7TKij8CChZajfKtE

/*eslint sort-imports: ["error", { "ignoreDeclarationSort": true }]*/
import a from 'foo.js'
import b from 'bar.js'
/*eslint sort-imports: ["error", { "ignoreDeclarationSort": true }]*/
import b from 'foo.js'
import a from 'bar.js'

pVILD4QIu1ZDjUKBOVzHJ/QNmmiY9n899Z0N8aIkPG8=

ignoreMemberSort

ldeiBb/SQl5XH9dRGpr9BWDfNQOP8TPj6bBozQrY+LusZd0S1EVBNyhG/daZofZekWdF11ssxvntlMcPSM7iUesjETkdZblkNdHjdcEh26I=

xpQlHQir/OjetBCVQdvE2Tdsr06tKHweboRcgMJGRoxvDGUnOdEHB7QH78puQQge8Wl9vPhlx2xahtgluN1v4MfYBQKPSzmq9nIj0XGcKe3M0A8Cv0tZmp4hDGGrpQ7d

/*eslint sort-imports: ["error", { "ignoreMemberSort": false }]*/
import {b, a, c} from 'foo.js'

gXTXeFewMhKUGG8XamsAXS1g7GrdYfXCiT7+tmbOU38kiiEknM8ka76uL5S/9sOw1nPKExfLPq/cg8TIVgUeV13F6tSzT8k9h7zUKB8kyOGdh00on9WK+3HjfQ4ytClg

/*eslint sort-imports: ["error", { "ignoreMemberSort": true }]*/
import {b, a, c} from 'foo.js'

pVILD4QIu1ZDjUKBOVzHJ/QNmmiY9n899Z0N8aIkPG8=

memberSyntaxSortOrder

zFe6MVxow6exjcyNyXIMBQHPnj6nj1+XR72ceN+YrQiKv5xjOrlSqFZqex2sKFvBqNmto4/grZaIX7Ux0ZQ4agmFhk3dzHd3/a3GzodzPMk=

    DNH0KCw50BuLTrXYql6DnF2lqUMU92XpIIyyBPY7c4D8Vw4pW8pPDWNq6oFrB/JLbHa38DF9G/5A9F17BU3imNXlTwWYosF2WIDaB6ClJrioCf3CF39qY0xGEiu+rdQ82rCO1BIar8mkY8XR8MDNfhGhVIwFSQp19biiF+bhU113AGSYo3oOC3w1EGo7+jbmaP++Zmt0sxtQT9dGhcTjzUjKFZ3TEQlXPXdiHRO2SJ1yjy/W2RJVvjK22opN7fzCLm1OtuT3b6V45LDl3MzeMDPf90/faJIdhgaG3YuC3DkmBVUuJlJkV9Pd8q/jCvcIsB9USMEL2O5Ob83pkKMQiQ==

Ezh0lCTqgOk1OCRjiCS+7RAP9WOZZxjudomCb53KsyZZl7ebOI+v43AlU04hsf1OMrAsHf62QJ4VimhisD+X5doDmiQn87+0xGHhuu2GO/QMOIhzAE9bnHV4YqRh7W2/

xpQlHQir/OjetBCVQdvE2SbpkrQV+wB78m/Zqc6IxLKXm0aZvbmsnorNfyDdsvvDmNVrFHW48Ms4FT6IDevviIouapNp1GbLvgcR50y0ERtbHiDsq3p3lF29RUN5zQS99Spd0L4f2vOF9IVTaOGWFkwPL+SRZTKfL5Epx5wFMOHKdEOyGN4BmHcKbI840bu/

/*eslint sort-imports: "error"*/
import a from 'foo.js';
import * as b from 'bar.js';

gXTXeFewMhKUGG8XamsAXVXvcYITolvelKTxVH1M6nA+aZUoz3lHmLfXfYYkTrvpNkQ75s14m731PaGbcVNn327Ozno6yBYay1+rpzOasrkcJrdZWdvDXLhZaqI4CQnEroKrPQVPYqTQTDeGhirqwIF5ZiRMtSf72KcyFlGvf0Q=

/*eslint sort-imports: ["error", { "memberSyntaxSortOrder": ['single', 'all', 'multiple', 'none'] }]*/

import a from 'foo.js';
import * as b from 'bar.js';

gXTXeFewMhKUGG8XamsAXVXvcYITolvelKTxVH1M6nA+aZUoz3lHmLfXfYYkTrvpK+JSQ4T3i23ACzJN65DLlmwwK2vN5ptsjfy7i+56etTEKqo3vPPm+dpZ+vLxIaSBvmA2LZS/r/jGpHCiEEZUYYkdKiXjBdjtKg6JXmGax6I=

/*eslint sort-imports: ["error", { "memberSyntaxSortOrder": ['all', 'single', 'multiple', 'none'] }]*/

import * as foo from 'foo.js';
import z from 'zoo.js';
import {a, b} from 'foo.js';

pVILD4QIu1ZDjUKBOVzHJyDoiMzWAEd84ivD8eCR0KzO5pKAFMCCDOq6mXd7hKfzN/kE6HKKJNdTTEbDZMAwpw==

allowSeparatedGroups

fL4tT0ay+W9+tvye9LDC7NVIN848RuV1hoIN+7NV6Gj23WBb9Q6ShZRqZIFNcP1SwmSc8+vin2gjekMYqjhxxCr1V0UpU2fdO0DMzzMJgFnmV4b+o4TDF68I24zy+2jkjg80sIUgWQSqSaN+ZE/Cbw==

9Erf+UI38TTpjcVWMWjF3XOFbgnmUD/QSLYmHfK5SQHuxVsxA22nJaurCOnx7+Mp3gOaaHNJ9AEM2HfaWFvwmLt3wM2jYwHNfJo7L+ABkNQO5eTH6nTvo1qRSNXD55FTm6vzPwbowM686JRSt+6jUFuUrRdaQtZBpIHMJ6Kb+ryxzSqo5PxAYm7Q0X2xEKxb

gXTXeFewMhKUGG8XamsAXT/G+ZP+jT2fNyRuYWMdjL6rUbrZtB7XXegE4wwUnGapdH2x9klu0fAGudjffo745gxE3DJy61FwsMxNsWWF1WMsctai8gf19pyYd6dWRzHa

/*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/

import b from 'foo.js';
import c from 'bar.js';
import a from 'baz.js';

gXTXeFewMhKUGG8XamsAXT/G+ZP+jT2fNyRuYWMdjL6rUbrZtB7XXegE4wwUnGapdH2x9klu0fAGudjffo745mVRPHo9pM2ckCw2eKdOCTDDVVmOqhC6N85qdUT9Arbu

/*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/

import b from 'foo.js';
import c from 'bar.js';

import a from 'baz.js';
/*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/

import b from 'foo.js';
import c from 'bar.js';
// comment
import a from 'baz.js';
/*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/

import b from 'foo.js';
import c from 'bar.js';
quux();
import a from 'baz.js';

pVILD4QIu1ZDjUKBOVzHJ/QNmmiY9n899Z0N8aIkPG8=

何时不使用

MqHTaFTu5WXNDoYLOYL5zpO+FIYPt3MlY+V4mB23BCLZUeOovRB/Tp+q8q6YbsPh56Rg8kKUETCGL4XdWpUWXloRGzcoiPbFOXNi5EL1fA02FjyWei6xqZXStce2hsqJAd8CRqTqyH5G/ErbcHVMzcK1Uuai+kN+Ynl7Xnsz8w5JnrX7ngmGWmji9GPBIaYMy6fp45t5sXHxh3itoDJYcLm6yUU7tK8LbM9wPAJqIhIXU0CuPa5wPsd0Vzib3Ha5AqJ+cKyMzo2VoXCFzwwXgg==