member-ordering
要求一致的成员声明顺序.
该规则旨在标准化类、接口和类型字面量的结构化和排序方式。字段、方法和构造函数的一致顺序可以使代码更易于阅读、导航和编辑。
¥This rule aims to standardize the way classes, interfaces, and type literals are structured and ordered. A consistent ordering of fields, methods and constructors can make code easier to read, navigate, and edit.
此规则功能已冻结: ==== 此规则由 TypeScript 类型提供支持,因此,如果类型与运行时行为不匹配,则规则可能会报告不准确。它将不再接收新功能,例如新选项。它仍将接受现有功能字段的错误和文档修复。
¥This rule is feature frozen: it will no longer receive new features such as new options. It still will accept bug and documentation fixes for its existing area of features.
随着请求的功能越来越模糊,强制命名和/或排序约定的样式规则往往会变得难以理解的复杂。此规则已达到 typescript-eslint 项目维护的合理极限。有关更多信息,请参阅 eslint-plugin:功能冻结命名和排序样式规则。
¥Stylistic rules that enforce naming and/or sorting conventions tend to grow incomprehensibly complex as increasingly obscure features are requested. This rule has reached the limit of what is reasonable for the typescript-eslint project to maintain. See eslint-plugin: Feature freeze naming and sorting stylistic rules for more information.
- 扁平配置
- 旧版配置
export default tseslint.config({
rules: {
"@typescript-eslint/member-ordering": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/member-ordering": "error"
}
};
在线运行试试这个规则 ↗
选项
¥Options
interface Options {
default?: OrderConfig;
classes?: OrderConfig;
classExpressions?: OrderConfig;
interfaces?: OrderConfig;
typeLiterals?: OrderConfig;
}
type OrderConfig = MemberType[] | SortedOrderConfig | 'never';
interface SortedOrderConfig {
memberTypes?: MemberType[] | 'never';
optionalityOrder?: 'optional-first' | 'required-first';
order?:
| 'alphabetically'
| 'alphabetically-case-insensitive'
| 'as-written'
| 'natural'
| 'natural-case-insensitive';
}
// See below for the more specific MemberType strings
type MemberType = string | string[];
你可以为以下配置 OrderConfig
选项:
¥You can configure OrderConfig
options for:
-
default
:所有构造(用作后备)¥
default
: all constructs (used as a fallback) -
classes
?:覆盖专门针对类的排序¥
classes
?: override ordering specifically for classes -
classExpressions
?:重写专门针对类表达式的排序¥
classExpressions
?: override ordering specifically for class expressions -
interfaces
?:重写专门针对接口的排序¥
interfaces
?: override ordering specifically for interfaces -
typeLiterals
?:专门针对类型字面量覆盖排序¥
typeLiterals
?: override ordering specifically for type literals
每种构造的 OrderConfig
设置最多可以配置三个级别的排序:
¥The OrderConfig
settings for each kind of construct may configure sorting on up to three levels:
-
memberTypes
:按成员类型组进行组织,例如方法与属性¥
memberTypes
: organizing on member type groups such as methods vs. properties -
optionalityOrder
:是将所有可选成员放在前面还是将所有必需成员放在前面¥
optionalityOrder
: whether to put all optional members first or all required members first -
order
:根据成员名称进行组织,例如按字母顺序排列¥
order
: organizing based on member names, such as alphabetically
群组
¥Groups
你可以根据成员的不同属性定义许多不 同的组。支持的成员属性按顺序为:
¥You can define many different groups based on different attributes of members. The supported member attributes are, in order:
-
可访问性 (
'public' | 'protected' | 'private' | '#private'
)¥Accessibility (
'public' | 'protected' | 'private' | '#private'
) -
装饰 (
'decorated'
):成员是否具有显式可访问性装饰器¥Decoration (
'decorated'
): Whether the member has an explicit accessibility decorator -
种类(
'call-signature' | 'constructor' | 'field' | 'readonly-field' | 'get' | 'method' | 'set' | 'signature' | 'readonly-signature'
)¥Kind (
'call-signature' | 'constructor' | 'field' | 'readonly-field' | 'get' | 'method' | 'set' | 'signature' | 'readonly-signature'
)
成员属性可以与 '-'
连接以组合成更具体的组。例如,'public-field'
将位于 'private-field'
之前。
¥Member attributes may be joined with a '-'
to combine into more specific groups.
For example, 'public-field'
would come before 'private-field'
.
命令
¥Orders
order
值指定成员在组中的顺序。它默认为 as-written
,这意味着任何顺序都可以。其他允许的值有:
¥The order
value specifies what order members should be within a group.
It defaults to as-written
, meaning any order is fine.
Other allowed values are:
-
alphabetically
:按 a-z 字母顺序排序,直接使用字符串<
比较(因此B
位于a
之前)¥
alphabetically
: Sorted in a-z alphabetical order, directly using string<
comparison (soB
comes beforea
) -
alphabetically-case-insensitive
:按 a-z 字母顺序排序,忽略大小写(因此a
位于B
之前)¥
alphabetically-case-insensitive
: Sorted in a-z alphabetical order, ignoring case (soa
comes beforeB
) -
natural
:与alphabetically
相同,但使用natural-compare-lite
进行更友好的数字排序¥
natural
: Same asalphabetically
, but usingnatural-compare-lite
for more friendly sorting of numbers -
natural-case-insensitive
:与alphabetically-case-insensitive
相同,但使用natural-compare-lite
进行更友好的数字排序¥
natural-case-insensitive
: Same asalphabetically-case-insensitive
, but usingnatural-compare-lite
for more friendly sorting of numbers