版本

sort-imports

强制模块内 import 声明排序

🔧 可修复

此规则报告的某些问题可以通过 --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"

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

规则详情

此规则检查所有 import 声明,并验证所有导入首先按使用的成员语法排序,然后按第一个成员或别名名称按字母顺序排序。

命令行中的 --fix 选项会自动修复此规则报告的某些问题:单行上的多个成员会自动排序(例如,import { b, a } from 'foo.js' 会更正为 import { a, b } from 'foo.js'),但不会重新排序多行。

选项

此规则接受一个对象,其属性如下:

  • ignoreCase (默认值: false)
  • ignoreDeclarationSort (默认值: false)
  • ignoreMemberSort (默认值: false)
  • memberSyntaxSortOrder (默认值: ["none", "all", "multiple", "single"]); 所有 4 个项目都必须存在于数组中,但您可以更改顺序
    • none = 导入没有导出绑定的模块。
    • all = 导入由导出绑定提供的所有成员。
    • multiple = 导入多个成员。
    • single = 导入单个成员。
  • allowSeparatedGroups (默认值: false)

默认选项设置是

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

示例

默认设置

使用默认选项时,此规则的正确代码示例

在 Playground 中打开
/*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';
在 Playground 中打开
/*eslint sort-imports: "error"*/
import a from 'foo.js';
import b from 'bar.js';
import c from 'baz.js';
在 Playground 中打开
/*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';
在 Playground 中打开
/*eslint sort-imports: "error"*/
import {a, b, c} from 'foo.js'

使用默认选项时,此规则的不正确代码示例

在 Playground 中打开
/*eslint sort-imports: "error"*/
import b from 'foo.js';
import a from 'bar.js';
在 Playground 中打开
/*eslint sort-imports: "error"*/
import a from 'foo.js';
import A from 'bar.js';
在 Playground 中打开
/*eslint sort-imports: "error"*/
import {c, d} from 'foo.js';
import {a, b} from 'bar.js';
在 Playground 中打开
/*eslint sort-imports: "error"*/
import a from 'foo.js';
import {b, c} from 'bar.js';
在 Playground 中打开
/*eslint sort-imports: "error"*/
import {a} from 'foo.js';
import {b, c} from 'bar.js';
在 Playground 中打开
/*eslint sort-imports: "error"*/
import a from 'foo.js';
import * as b from 'bar.js';
在 Playground 中打开
/*eslint sort-imports: "error"*/
import {b, a, c} from 'foo.js'

ignoreCase

false(默认值)时,字母表的大写字母必须始终在小写字母之前。

true 时,该规则忽略导入本地名称的大小写敏感性。

对于默认 { "ignoreCase": false } 选项,此规则的不正确代码示例

在 Playground 中打开
/*eslint sort-imports: ["error", { "ignoreCase": false }]*/
import a from 'bar.js';
import B from 'foo.js';
import c from 'baz.js';

对于默认 { "ignoreCase": false } 选项,此规则的正确代码示例

在 Playground 中打开
/*eslint sort-imports: ["error", { "ignoreCase": false }]*/
import B from 'bar.js';
import a from 'foo.js';
import c from 'baz.js';

对于 { "ignoreCase": true } 选项,此规则的正确代码示例

在 Playground 中打开
/*eslint sort-imports: ["error", { "ignoreCase": true }]*/
import a from 'bar.js';
import B from 'foo.js';
import c from 'baz.js';

对于 { "ignoreCase": true } 选项,此规则的不正确代码示例

在 Playground 中打开
/*eslint sort-imports: ["error", { "ignoreCase": true }]*/
import B from 'foo.js';
import a from 'bar.js';

ignoreDeclarationSort

true 时,该规则忽略导入声明语句的排序。默认为 false

对于默认 { "ignoreDeclarationSort": false } 选项,此规则的不正确代码示例

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

对于默认 { "ignoreDeclarationSort": false } 选项,此规则的正确代码示例

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

对于 { "ignoreDeclarationSort": true } 选项,此规则的正确代码示例

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

对于 { "ignoreDeclarationSort": true } 选项,此规则的不正确代码示例

在 Playground 中打开
/*eslint sort-imports: ["error", { "ignoreDeclarationSort": true }]*/
import {b, a, c} from 'foo.js';

ignoreMemberSort

true 时,该规则忽略 multiple 成员导入声明中的成员排序。默认为 false

对于默认 { "ignoreMemberSort": false } 选项,此规则的不正确代码示例

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

对于默认 { "ignoreMemberSort": false } 选项,此规则的正确代码示例

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

对于 { "ignoreMemberSort": true } 选项,此规则的正确代码示例

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

对于 { "ignoreMemberSort": true } 选项,此规则的不正确代码示例

在 Playground 中打开
/*eslint sort-imports: ["error", { "ignoreMemberSort": true }]*/
import b from 'foo.js';
import a from 'bar.js';

memberSyntaxSortOrder

此选项接受一个包含四个预定义元素的数组,元素的顺序指定导入样式的顺序。

默认顺序为 ["none", "all", "multiple", "single"]

有四种不同的样式,默认的成员语法排序顺序是

  • none - 导入没有导出绑定的模块。
  • all - 导入由导出绑定提供的所有成员。
  • multiple - 导入多个成员。
  • single - 导入单个成员。

所有四个选项都必须在数组中指定,但您可以自定义它们的顺序。

对于默认 { "memberSyntaxSortOrder": ["none", "all", "multiple", "single"] } 选项,此规则的不正确代码示例

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

对于 { "memberSyntaxSortOrder": ['single', 'all', 'multiple', 'none'] } 选项,此规则的正确代码示例

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

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

对于 { "memberSyntaxSortOrder": ['all', 'single', 'multiple', 'none'] } 选项,此规则的正确代码示例

在 Playground 中打开
/*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';

allowSeparatedGroups

true 时,该规则仅检查连续行上出现的导入声明语句的排序。默认为 false

换句话说,在导入声明语句之后的空行或注释行或任何其他语句行将重置导入声明语句的排序。

对于 { "allowSeparatedGroups": true } 选项,此规则的不正确代码示例

在 Playground 中打开
/*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/

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

对于 { "allowSeparatedGroups": true } 选项,此规则的正确代码示例

在 Playground 中打开
/*eslint sort-imports: ["error", { "allowSeparatedGroups": true }]*/

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

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

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

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

何时不使用

此规则是一种格式偏好,不遵循它不会对您的代码质量产生负面影响。如果字母顺序排列导入不是您的编码标准的一部分,那么您可以禁用此规则。

版本

此规则在 ESLint v2.0.0-beta.1 中引入。

资源

更改语言