naming-convention
强制对代码库中的所有内容进行命名约定.
该规则需要 类型信息 才能运行,但这会带来性能方面的权衡。
强制命名约定有助于保持代码库的一致性,并减少考虑如何命名变量时的开销。此外,精心设计的样式指南可以帮助传达意图,例如强制所有私有属性以 _
开头,并且所有全局级常量都用 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
相同,但所有字符都必须大写。