naming-convention
强制对代码库中的所有内容进行命名约定.
该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。
This rule is currently frozen and is not accepting feature requests.
强制命名约定有助于保持代码库的一致性,并减少考虑如何命名变量时的开销。此外,精心设计的样式指南可以帮助传达意图,例如强制所有私有属性以 _
开头,并且所有全局级常量都用 UPPER_CASE
编写。
¥Enforcing naming conventions helps keep the codebase consistent, and reduces overhead when thinking about how to name a variable.
Additionally, a well-designed style guide can help communicate intent, such as by enforcing all private properties begin with an _
, and all global-level constants are written in UPPER_CASE
.
此规则功能已冻结: ==== 此规则由 TypeScript 类型提供支持,因此,如果类型与运行时行为不匹配,则规则可能会报告不准确。它将不再接收新功能,例如新选项。它仍将接受针对其现有功能字段的错误和文档修复,并支持新的 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 and to support new TypeScript versions.
随着请求的功能越来越模糊,强制命名和/或排序约定的样式规则往往会变得难以理解的复杂。此规则已达到 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/naming-convention": "error"
}
});
module.exports = {
"rules": {
"@typescript-eslint/naming-convention": "error"
}
};
在线运行试试这个规则 ↗
示例
¥Examples
此规则允许你对任何标识符强制执行约定,使用细粒度选择器来创建细粒度的样式指南。
¥This rule allows you to enforce conventions for any identifier, using granular selectors to create a fine-grained style guide.
此规则谨慎行事,仅适用于底部类型、顶部类型以及将字面量类型与原始类型进行比较。
¥This rule only needs type information in specific cases, detailed below.
选项
¥Options
该规则接受对象数组,每个对象描述不同的命名约定。下面将详细描述每个属性。另请参阅下面的示例部分以获取图示示例。
¥This rule accepts an array of objects, with each object describing a different naming convention. Each property will be described in detail below. Also see the examples section below for illustrated examples.
type Options = {
// format options
format:
| (
| 'camelCase'
| 'strictCamelCase'
| 'PascalCase'
| 'StrictPascalCase'
| 'snake_case'
| 'UPPER_CASE'
)[]
| null;
custom?: {
regex: string;
match: boolean;
};
leadingUnderscore?:
| 'forbid'
| 'require'
| 'requireDouble'
| 'allow'
| 'allowDouble'
| 'allowSingleOrDouble';
trailingUnderscore?:
| 'forbid'
| 'require'
| 'requireDouble'
| 'allow'
| 'allowDouble'
| 'allowSingleOrDouble';
prefix?: string[];
suffix?: string[];
// selector options
selector: Selector | Selector[];
filter?:
| string
| {
regex: string;
match: boolean;
};
// the allowed values for these are dependent on the selector - see below
modifiers?: Modifiers<Selector>[];
types?: Types<Selector>[];
}[];
// the default config is similar to ESLint's camelcase rule but more strict
const defaultOptions: Options = [
{
selector: 'default',
format: ['camelCase'],
leadingUnderscore: 'allow',
trailingUnderscore: 'allow',
},
{
selector: 'import',
format: ['camelCase', 'PascalCase'],
},
{
selector: 'variable',
format: ['camelCase', 'UPPER_CASE'],
leadingUnderscore: 'allow',
trailingUnderscore: 'allow',
},
{
selector: 'typeLike',
format: ['PascalCase'],
},
];
格式选项
¥Format Options
每个选择器都可以具有相同的格式选项集。有关如何应用每个选择器的信息,请参阅 "该规则如何评估名称的格式?"。
¥Every single selector can have the same set of format options. For information about how each selector is applied, see "How does the rule evaluate a name's format?".
format
format
选项定义标识符的允许格式。此选项接受以下值的数组,并且标识符可以与其中任何一个匹配:
¥The format
option defines the allowed formats for the identifier. This option accepts an array of the following values, and the identifier can match any of them:
-
camelCase
- 标准 camelCase 格式 - 字符之间不允许有下划线,允许连续大写(即myID
和myId
均有效)。¥
camelCase
- standard camelCase format - no underscores are allowed between characters, and consecutive capitals are allowed (i.e. bothmyID
andmyId
are valid). -
PascalCase
- 与camelCase
相同,但第一个字符必须大写。¥
PascalCase
- same ascamelCase
, except the first character must be upper-case. -
snake_case
- 标准 snake_case 格式 - 所有字符都必须是小写,并且允许使用下划线。¥
snake_case
- standard snake_case format - all characters must be lower-case, and underscores are allowed. -
strictCamelCase
- 与camelCase
相同,但不允许连续大写(即myId
有效,但myID
无效)。¥
strictCamelCase
- same ascamelCase
, but consecutive capitals are not allowed (i.e.myId
is valid, butmyID
is not). -
StrictPascalCase
- 与strictCamelCase
相同,但第一个字符必须大写。¥
StrictPascalCase
- same asstrictCamelCase
, except the first character must be upper-case. -
UPPER_CASE
- 与snake_case
相同,但所有字符都必须大写。¥
UPPER_CASE
- same assnake_case
, except all characters must be upper-case.
除了数组,你还可以传递 null
。这表示 "此选择器不应检查其格式"。如果你想在应用组选择器后对特定选择器不强制执行特定格式,这会很有用。
¥Instead of an array, you may also pass null
. This signifies "this selector shall not have its format checked".
This can be useful if you want to enforce no particular format for a specific selector, after applying a group selector.
custom
custom
选项定义标识符必须(或不能)匹配的自定义正则表达式。此选项允许你对标识符进行更细粒度的控制,从而禁止(或强制)某些模式和子字符串。接受具有以下属性的对象:
¥The custom
option defines a custom regex that the identifier must (or must not) match. This option allows you to have a bit more finer-grained control over identifiers, letting you ban (or force) certain patterns and substrings.
Accepts an object with the following properties:
-
match
- 如果标识符必须与regex
匹配,则为 true,如果标识符不能与regex
匹配,则为 false。¥
match
- true if the identifier must match theregex
, false if the identifier must not match theregex
. -
regex
- 然后传递到 RegExp 中以创建新正则表达式的字符串:new RegExp(regex)
¥
regex
- a string that is then passed into RegExp to create a new regular expression:new RegExp(regex)
filter
filter
选项的操作类似于 custom
,接受相同形状的对象,不同之处在于它控制是否应将其余配置应用于标识符。
¥The filter
option operates similar to custom
, accepting the same shaped object, except that it controls if the rest of the configuration should or should not be applied to an identifier.
你可以使用它来包含或排除特定配置中的特定标识符。
¥You can use this to include or exclude specific identifiers from specific configurations.
接受具有以下属性的对象:
¥Accepts an object with the following properties:
-
match
- 如果标识符必须与regex
匹配,则为 true,如果标识符不能与regex
匹配,则为 false。¥
match
- true if the identifier must match theregex
, false if the identifier must not match theregex
. -
regex
- 然后传递到 RegExp 中以创建新正则表达式的字符串:new RegExp(regex)
¥
regex
- a string that is then passed into RegExp to create a new regular expression:new RegExp(regex)
或者,filter
接受正则表达式(new RegExp(filter)
中接受的任何内容)。在这种情况下,它会被视为你已经传递了一个带有正则表达式和 match: true
的对象。
¥Alternatively, filter
accepts a regular expression (anything accepted into new RegExp(filter)
). In this case, it's treated as if you had passed an object with the regex and match: true
.
leadingUnderscore
/trailingUnderscore
leadingUnderscore
/ trailingUnderscore
选项控制前导/尾随下划线是否被视为有效。接受以下值之一:
¥The leadingUnderscore
/ trailingUnderscore
options control whether leading/trailing underscores are considered valid. Accepts one of the following values:
-
allow
- 没有明确强制要求存在单个前导/尾随下划线。¥
allow
- existence of a single leading/trailing underscore is not explicitly enforced. -
allowDouble
- 没有明确强制要求存在双前导/尾随下划线。¥
allowDouble
- existence of a double leading/trailing underscore is not explicitly enforced. -
allowSingleOrDouble
- 没有明确强制要求存在单个或双前导/尾随下划线。¥
allowSingleOrDouble
- existence of a single or a double leading/trailing underscore is not explicitly enforced. -
forbid
- 完全不允许使用前导/尾随下划线。¥
forbid
- a leading/trailing underscore is not allowed at all. -
require
- 必须包含单个前导/尾随下划线。¥
require
- a single leading/trailing underscore must be included. -
requireDouble
- 必须包含两个前导/尾随下划线。¥
requireDouble
- two leading/trailing underscores must be included.
prefix
/suffix
prefix
/ suffix
选项控制标识符必须存在哪些前缀/后缀字符串。接受字符串数组。
¥The prefix
/ suffix
options control which prefix/suffix strings must exist for the identifier. Accepts an array of strings.
如果提供了这些,则标识符必须以提供的值之一开头。例如,如果你提供 { prefix: ['Class', 'IFace', 'Type'] }
,则以下名称有效:ClassBar
、IFaceFoo
、TypeBaz
,但名称 Bang
无效,因为它不包含任何前缀。
¥If these are provided, the identifier must start with one of the provided values. For example, if you provide { prefix: ['Class', 'IFace', 'Type'] }
, then the following names are valid: ClassBar
, IFaceFoo
, TypeBaz
, but the name Bang
is not valid, as it contains none of the prefixes.
注意:与 上面记录了 一样,前缀在验证格式之前被修剪,因此必须使用 PascalCase 来允许使用前缀 is
的变量(例如 isEnabled
)。
¥Note: As documented above, the prefix is trimmed before format is validated, therefore PascalCase must be used to allow variables such as isEnabled
using the prefix is
.