Skip to content

useNamingConvention

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": "error"
}
}
}
}

¥Description

强制 JavaScript 和 TypeScript 文件名的命名约定。

¥Enforce naming conventions for everything across a codebase.

强制 命名约定 有助于保持代码库的一致性,并在考虑变量的名称 case 时减少开销。

¥Enforcing naming conventions helps to keep the codebase consistent, and reduces overhead when thinking about the name case of a variable.

以下部分描述了规则强制执行的默认约定。你还可以使用 规则选项 强制执行自定义约定。

¥The following section describes the default conventions enforced by the rule. You can also enforce custom conventions with the rule options.

¥Naming conventions

所有名称都可以使用下划线 _ 和美元符号 $ 作为前缀和后缀。名称以 _ 为前缀的未使用变量将被完全忽略。这避免了与 noUnusedVariables 规则的冲突。

¥All names can be prefixed and suffixed with underscores _ and dollar signs $. Unused variables with a name prefixed with _ are completely ignored. This avoids conflicts with the noUnusedVariables rule.

¥Variable and parameter names

所有变量和函数参数都在 camelCasePascalCase 中。CLI 现在能够自动搜索和解析 camelCase ()。

¥All variables and function parameters are in camelCase or PascalCase. Catch parameters are in camelCase.

此外,声明为 constvar 的全局变量可能在 CONSTANT_CASE 中。全局变量在模块或脚本级别声明。在 TypeScript namespace 中声明的变量也被视为全局变量。

¥Additionally, global variables declared as const or var may be in CONSTANT_CASE. Global variables are declared at module or script level. Variables declared in a TypeScript namespace are also considered global.

function f(param, _unusedParam) {
let localValue = 0;
try {
/* ... */
} catch (customError) {
/* ... */
}
}
export const A_CONSTANT = 5;
let aVariable = 0;
export namespace ns {
export const ANOTHER_CONSTANT = "";
}

不正确名称的示例:

¥Examples of incorrect names:

let a_value = 0;
code-block.js:1:5 lint/style/useNamingConvention  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This let name should be in camelCase or PascalCase.

> 1 │ let a_value = 0;
^^^^^^^
2 │

Safe fix: Rename this symbol in camelCase.

1 - let·a_value·=·0;
1+ let·aValue·=·0;
2 2

const fooYPosition = 0;
code-block.js:1:7 lint/style/useNamingConvention ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Two consecutive uppercase characters are not allowed in camelCase because strictCase is set to `true`.

> 1 │ const fooYPosition = 0;
^^^^^^^^^^^^
2 │

If you want to use consecutive uppercase characters in camelCase, then set the strictCase option to `false`.
See the rule options for more details.

function f(FIRST_PARAM) {}
code-block.js:1:12 lint/style/useNamingConvention  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This function parameter name should be in camelCase or PascalCase.

> 1 │ function f(FIRST_PARAM) {}
^^^^^^^^^^^
2 │

Safe fix: Rename this symbol in PascalCase.

1 - function·f(FIRST_PARAM)·{}
1+ function·f(FirstParam)·{}
2 2

¥Function names

  • function 名称在 camelCasePascalCase 中。

    ¥A function name is in camelCase or PascalCase.

  • 全局 function 也可以在 UPPERCASE 中。这允许支持需要某些功能才能使用有效 HTTP 方法名称 的框架。

    ¥A global function can also be in UPPERCASE. This allows supporting the frameworks that require some function to use valid HTTP method names.

function trimString(s) { /*...*/ }
function Component() {
return <div></div>;
}
export function GET() { /*...*/ }

¥TypeScript enum names

TypeScript enum 名称在 PascalCase 中。

¥A TypeScript enum name is in PascalCase.

enum 成员默认在 PascalCase 中。但是,你可以配置 enum 成员的 case。有关更多详细信息,请参阅 options

¥enum members are by default in PascalCase. However, you can configure the case of enum members. See options for more details.

enum Status {
Open,
Close,
}

¥Classes

class Person {
static MAX_FRIEND_COUNT = 256;
static get SPECIAL_PERSON_INSTANCE() { /*...*/ }
initializedProperty = 0;
specialMethod() {}
}

¥TypeScript type aliases and interface

  • type 别名或接口名称在 PascalCase 中。

    ¥A type alias or an interface name are in PascalCase.

  • 类型的成员名称在 camelCase 中。

    ¥Member names of a type are in camelCase.

  • readonly 属性和 getter 名称也可以在 CONSTANT_CASE 中。

    ¥readonly property and getter names can also be in CONSTANT_CASE.

type Named = {
readonly fullName: string;
specialMethod(): void;
};
interface Named {
readonly fullName: string;
specialMethod(): void;
}
interface PersonConstructor {
readonly MAX_FRIEND_COUNT: number;
get SPECIAL_PERSON_INSTANCE(): Person;
new(): Person;
}

错误类型别名的示例:

¥Examples of an incorrect type alias:

type person = { fullName: string };
code-block.ts:1:6 lint/style/useNamingConvention  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This type alias name should be in PascalCase.

> 1 │ type person = { fullName: string };
^^^^^^
2 │

Safe fix: Rename this symbol in PascalCase.

1 - type·person·=·{·fullName:·string·};
1+ type·Person·=·{·fullName:·string·};
2 2

¥Literal object member names

const alice = {
fullName: "Alice",
}

错误名称的示例:

¥Example of an incorrect name:

const alice = {
full_name: "Alice",
}
code-block.js:2:5 lint/style/useNamingConvention ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This object property name should be in camelCase.

1 │ const alice = {
> 2 │ full_name: “Alice”,
^^^^^^^^^
3 │ }
4 │

¥Import and export aliases and namespaces

导入和导出命名空间在 camelCasePascalCase 中。

¥Import and export namespaces are in camelCase or PascalCase.

import * as myLib from "my-lib";
import * as Framework from "framework";
export * as myLib from "my-lib";
export * as Framework from "framework";

importexport 别名在 camelCasePascalCaseCONSTANT_CASE 中:

¥import and export aliases are in camelCase, PascalCase, or CONSTANT_CASE:

import assert, {
deepStrictEqual as deepEqual,
AssertionError as AssertError
} from "node:assert";

错误名称的示例:

¥Examples of an incorrect name:

import * as MY_LIB from "my-lib";
code-block.ts:1:13 lint/style/useNamingConvention  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This import namespace name should be in camelCase or PascalCase.

> 1 │ import * as MY_LIB from “my-lib”;
^^^^^^
2 │

Safe fix: Rename this symbol in PascalCase.

1 - import·*·as·MY_LIB·from·my-lib;
1+ import·*·as·MyLib·from·my-lib;
2 2

¥TypeScript type parameter names

TypeScript 类型参数名称在 PascalCase 中。

¥A TypeScript type parameter name is in PascalCase.

function id<Val>(value: Val): Val { /* ... */}

¥TypeScript namespace names

TypeScript namespace 名称位于 camelCasePascalCase 中。

¥A TypeScript namespace name is in camelCase or in PascalCase.

namespace mathExtra {
/*...*/
}
namespace MathExtra {
/*...*/
}

¥Ignored declarations

请注意,某些声明始终被忽略。你不能对它们应用约定。以下情况适用:

¥Note that some declarations are always ignored. You cannot apply a convention to them. This is the case for:

  • 不是标识符的成员名称

    ¥Member names that are not identifiers

class C {
["not an identifier"]() {}
}
  • 命名导入

    ¥Named imports

import { an_IMPORT } from "mod"
  • 解构对象属性

    ¥Destructured object properties

const { destructed_PROP } = obj;
  • 标记为 override 的类成员:

    ¥Class members marked with override:

class C extends B {
override overridden_METHOD() {}
}
  • 外部 TypeScript 模块中的声明

    ¥Declarations inside an external TypeScript module

declare module "myExternalModule" {
export interface my_INTERFACE {}
}
  • 全局声明中的声明

    ¥Declarations inside a global declaration

declare global {
interface HTMLElement {}
}

¥Options

规则提供了几个选项,详见以下小节。

¥The rule provides several options that are detailed in the following subsections.

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"options": {
"strictCase": false,
"requireAscii": false,
"conventions": [
{
"selector": {
"kind": "classMember",
"modifiers": [
"private"
]
},
"match": "_(.+)",
"formats": [
"camelCase"
]
}
]
}
}
}
}
}
}

当此选项设置为 true 时,它禁止在 camelCasePascalCase 中使用连续的大写字符。

¥When this option is set to true, it forbids consecutive uppercase characters in camelCase and PascalCase.

默认:true

¥Default: true

例如,strictCase: true 不允许使用 HTTPServeraHTTPServer。这些名称应重命名为 HttpServeraHttpServer

¥For instance, HTTPServer or aHTTPServer are not permitted for strictCase: true. These names should be renamed to HttpServer and aHttpServer:

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"options": {
"strictCase": true
}
}
}
}
}
}
class HTTPServer {
}
code-block.js:1:7 lint/style/useNamingConvention  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Two consecutive uppercase characters are not allowed in PascalCase because strictCase is set to `true`.

> 1 │ class HTTPServer {
^^^^^^^^^^
2 │ }
3 │

If you want to use consecutive uppercase characters in PascalCase, then set the strictCase option to `false`.
See the rule options for more details.

Safe fix: Rename this symbol in PascalCase.

1 - class·HTTPServer·{
1+ class·HttpServer·{
2 2 }
3 3

strictCase 设置为 false 时,允许使用连续的大写字母。例如,HTTPServeraHTTPServer 都被认为是有效的:

¥When strictCase is set to false, consecutive uppercase characters are allowed. For example, HTTPServer and aHTTPServer would be considered valid then:

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"options": {
"strictCase": false
}
}
}
}
}
}
class HTTPServer {
}

使用 true 时,名称必须仅包含 ASCII 字符,禁止使用包含非 ASCII 字符的名称,例如 café안녕하세요

¥When true, names must only consist of ASCII characters only, forbidding names like café or 안녕하세요 that include non-ASCII characters.

requireAscii 设置为 false 时,名称可以包含非 ASCII 字符。例如,café안녕하세요 将被视为有效。

¥When requireAscii is set to false, names may include non-ASCII characters. For example, café and 안녕하세요 would be considered valid then.

默认:true

¥Default: true

conventions 选项允许应用自定义约定。该选项采用一组约定。每个约定都是一个对象,它包含一个可选的 selector 和一个或多个要求(matchformats)。

¥The conventions option allows applying custom conventions. The option takes an array of conventions. Every convention is an object that includes an optional selector and one or more requirements (match and formats).

例如,你可以强制使用 CONSTANT_CASE 进行全局 const 声明:

¥For example, you can enforce the use of CONSTANT_CASE for global const declarations:

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"options": {
"conventions": [
{
"selector": {
"kind": "const",
"scope": "global"
},
"formats": [
"CONSTANT_CASE"
]
}
]
}
}
}
}
}
}

选择器描述约定适用于哪些声明。你可以根据几个标准选择一个声明:

¥A selector describes which declarations the convention applies to. You can select a declaration based on several criteria:

  • kind:声明的类型如下:

    ¥kind: the kind of the declaration among:

    • any(如果未设置类型,则为默认类型)

      ¥any (default kind if the kind is unset)

    • typeLike:类、枚举、类型别名和接口

      ¥typeLike: classes, enums, type aliases, and interfaces

    • class

    • enum

    • enumMember

    • interface

    • typeAlias

    • function:命名函数声明和表达式

      ¥function: named function declarations and expressions

    • namespaceLike:TypeScript 命名空间、导入和导出命名空间 (import * as namespace from)

      ¥namespaceLike: TypeScript namespaces, import and export namespaces (import * as namespace from)

    • namespace:TypeScript 命名空间

      ¥namespace: TypeScript namespaces

    • importNamespace

    • exportNamespace

    • importAlias:默认导入和命名导入的别名

      ¥importAlias: default imports and aliases of named imports

    • exportAlias:重新导出名称的别名

      ¥exportAlias: aliases of re-exported names

    • variable:const、let、using 和 var 声明

      ¥variable: const, let, using, and var declarations

    • const

    • let

    • var

    • using

    • functionParameter

    • catchParameter

    • indexParameter:索引签名的参数

      ¥indexParameter: parameters of index signatures

    • typeParameter:通用类型参数

      ¥typeParameter: generic type parameter

    • classMember:类属性、参数属性、方法、getter 和 setter

      ¥classMember: class properties, parameter properties, methods, getters, and setters

    • classProperty:类属性,包括参数属性

      ¥classProperty: class properties, including parameter properties

    • classMethod

    • classGetter

    • classSetter

    • objectLiteralMember:文字对象属性、方法、getter 和 setter

      ¥objectLiteralMember: literal object properties, methods, getters, and setters

    • objectLiteralProperty

    • objectLiteralMethod

    • objectLiteralGetter

    • objectLiteralSetter

    • typeMember:在类型别名和接口中声明的属性、方法、getter 和 setter

      ¥typeMember: properties, methods, getters, and setters declared in type aliases and interfaces

    • typeProperty

    • typeMethod

    • typeGetter

    • typeSetter

  • modifiers:修饰符数组,其中包括:

    ¥modifiers: an array of modifiers among:

    • abstract:适用于类成员和类

      ¥abstract: applies to class members and classes

    • private:适用于类成员

      ¥private: applies to class members

    • protected:适用于类成员

      ¥protected: applies to class members

    • readonly:适用于类成员和类型成员

      ¥readonly: applies to class members and type members

    • static:适用于类成员

      ¥static: applies to class members

  • scope:声明出现的位置。允许的值:

    ¥scope: where the declaration appears. Allowed values:

    • any:任何地方(如果未设置范围,则为默认值)

      ¥any: anywhere (default value if the scope is unset)

    • global:全局范围(还包括命名空间范围)

      ¥global: the global scope (also includes the namespace scopes)

对于每个声明,系统会按顺序遍历 conventions 数组,直到找到选择器选中该声明为止。约定的要求在声明中得到验证。

¥For each declaration, the conventions array is traversed in-order until a selector selects the declaration. The requirements of the convention are so verified on the declaration.

约定必须至少设置以下一项要求:

¥A convention must set at least one requirement among:

如果仅设置了 formats,则 将与声明名称进行比对。在以下配置中,我们要求 static readonly 类属性必须位于 CONSTANT_CASE 中。

¥If only formats is set, it’s checked against the name of the declaration. In the following configuration, we require static readonly class properties to be in CONSTANT_CASE.

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"options": {
"conventions": [
{
"selector": {
"kind": "classProperty",
"modifiers": [
"static",
"readonly"
]
},
"formats": [
"CONSTANT_CASE"
]
}
]
}
}
}
}
}
}

以下代码随后会被规则报告:

¥The following code is then reported by the rule:

class C {
static readonly prop = 0;
}
code-block.ts:2:21 lint/style/useNamingConvention ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

This readonly static class property name should be in CONSTANT_CASE.

1 │ class C {
> 2 │ static readonly prop = 0;
^^^^
3 │ }
4 │

一个约定可能会使另一个约定失效。在以下配置中,第二个约定无效,因为第一个约定始终适用于类成员,包括类属性。你应该始终将更具体的约定放在首位。

¥A convention can make another one useless. In the following configuration, the second convention is useless because the first one always applies to class members, including class properties. You should always place first more specific conventions.

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"options": {
"conventions": [
{
"selector": {
"kind": "classMember"
},
"formats": [
"camelCase"
]
},
{
"selector": {
"kind": "classProperty"
},
"formats": [
"camelCase",
"CONSTANT_CASE"
]
}
]
}
}
}
}
}
}

如果仅设置了 match 且正则表达式没有捕获组,则 match 将直接与声明名称进行比对。在以下配置中,所有变量名必须至少包含 3 个字符,最多包含 20 个字符。

¥If only match is set and the regular expression has no capturing groups, then match is checked against the name of the declaration directly. In the following configuration, all variable names must have a minimum of 3 characters and a maximum of 20 characters.

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"options": {
"conventions": [
{
"selector": {
"kind": "variable"
},
"match": ".{3,20}"
}
]
}
}
}
}
}
}

如果同时设置了 matchformats,则根据正则表达式的第一次捕获检查 formats。仅测试第一次捕获。其他捕获被忽略。如果未捕获任何内容,则忽略 formats

¥If both match and formats are set, then formats is checked against the first capture of the regular expression. Only the first capture is tested. Other captures are ignored. If nothing is captured, then formats is ignored.

在以下示例中,我们要求:

¥In the following example, we require that:

  • 私有属性以 _ 开头,且至少包含两个字符。

    ¥A private property starts with _ and consists of at least two characters.

  • 捕获的名称(不带前导 _ 的名称)在 camelCase 中。

    ¥The captured name (the name without the leading _) is in camelCase.

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"options": {
"conventions": [
{
"selector": {
"kind": "classMember",
"modifiers": [
"private"
]
},
"match": "_(.+)",
"formats": [
"camelCase"
]
}
]
}
}
}
}
}
}

如果设置了 match 而未设置 formats,则正则表达式捕获的名称部分将传递给选择该声明的数组的下一个约定。以下配置与上一个配置的效果完全相同。第一条约定适用于任何私有类成员名称。它规定名称必须以一个下划线开头。正则表达式捕获名称中不带前导下划线的部分。由于 formats 未设置,捕获将转发到适用于私有类成员名称的下一个约定。在我们的例子中,适用以下约定。捕获的内容会与 formats 进行比较。

¥If match is set and formats is unset, then the part of the name captured by the regular expression is forwarded to the next conventions of the array that selects the declaration. The following configuration has exactly the same effect as the previous one. The first convention applies to any private class member name. It stipulates that the name must have a leading underscore. The regular expression captures the part of the name without the leading underscore. Because formats is not set, the capture is forwarded to the next convention that applies to a private class member name. In our case, the next convention applies. The capture is then checked against formats.

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"options": {
"conventions": [
{
"selector": {
"kind": "classMember",
"modifiers": [
"private"
]
},
"match": "_(.+)"
// We don't need to specify `formats` because the capture is forwarded to the next conventions.
},
{
"selector": {
"kind": "classMember",
"modifiers": [
"private"
]
},
"formats": [
"camelCase"
]
}
]
}
}
}
}
}
}

转发机制对于分解某些约定尤为有用。例如,以下配置……

¥The forwarding has particularly useful to factorize some conventions. For example, the following configuration…

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"options": {
"conventions": [
{
"selector": {
"kind": "classMember",
"modifiers": [
"private"
]
},
"match": "_(.+)",
"formats": [
"camelCase"
]
},
{
"selector": {
"kind": "classMember"
},
"formats": [
"camelCase"
]
}
]
}
}
}
}
}
}

可以分解为……

¥can be factorized to…

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"options": {
"conventions": [
{
"selector": {
"kind": "classMember",
"modifiers": [
"private"
]
},
"match": "_(.+)"
},
{
"selector": {
"kind": "classMember"
},
"formats": [
"camelCase"
]
}
]
}
}
}
}
}
}

如果未选择声明,或者在没有更多约定的情况下转发捕获,则将根据默认约定验证声明名称。因为默认约定已经确保类成员在 [“camelCase”] 中,所以前面的示例可以简化为:

¥If a declaration is not selected or if a capture is forwarded while there are no more conventions, then the declaration name is verified against the default conventions. Because the default conventions already ensure that class members are in [“camelCase”], the previous example can be simplified to:

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"options": {
"conventions": [
{
"selector": {
"kind": "classMember",
"modifiers": [
"private"
]
},
"match": "_(.+)"
// We don't need to specify `formats` because the capture is forwarded to the next conventions.
}
// default conventions
]
}
}
}
}
}
}

如果捕获与初始名称相同(它不是初始名称的一部分),则在根据默认约定进行检查之前,会修剪前导和尾随下划线和美元符号。在前面的示例中,捕获的内容是名称的一部分,因为 _ 不包含在捕获的内容中,因此不会执行任何修剪操作。

¥If the capture is identical to the initial name (it is not a part of the initial name), then, leading and trailing underscore and dollar signs are trimmed before being checked against default conventions. In the previous example, the capture is a part of the name because _ is not included in the capture, thus, no trimming is performed.

你可以通过在数组末尾添加一个接受任何内容的约定来重置所有默认约定:

¥You can reset all default conventions by adding a convention at the end of the array that accepts anything:

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"options": {
"conventions": [
// your conventions
// ...
// Otherwise, accept anything
{
"match": ".*"
}
]
}
}
}
}
}
}

让我们举一个更复杂的例子,遵循以下约定:

¥Let’s take a more complex example with the following conventions:

  1. 变量名可以是 ij,或者遵循下一个选定的约定(约定 (2))。

    ¥A variable name is i, j, or follows the next selected convention (convention (2)).

  2. 标识符至少包含两个字符,并遵循下一个选定的约定(默认约定)。

    ¥An identifier contains at least two characters and follow the next selected convention (the default convention).

  3. private 类成员名称以下划线 _ 开头,去掉下划线后的名称遵循下一个选定的约定(某些成员遵循约定 (4),其他成员遵循默认约定)。

    ¥A private class member name starts with an underscore _ and the name without the underscore follows the next selected convention (convention (4) for some of them, and the default convention for others).

  4. static readonly 类属性名称位于 CONSTANT_CASE 中。

    ¥A static readonly class property name is in CONSTANT_CASE.

  5. 全局常量位于 CONSTANT_CASE 中,可以用双下划线括起来,也可以命名为 _SPECIAL_

    ¥A global constant is in CONSTANT_CASE and can be enclosed by double underscores or to be named _SPECIAL_.

  6. 接口名称以 I 开头,以 Error 结尾的接口除外,并且符合 PascalCase 规则。

    ¥An interface name starts with I, except for interfaces ending with Error, and is in PascalCase.

  7. 所有其他名称遵循默认约定

    ¥All other names follow the default conventions

biome.json
{
"linter": {
"rules": {
"style": {
"useNamingConvention": {
"options": {
"conventions": [
{
"selector": {
"kind": "variable"
},
"match": "[ij]|(.*)"
},
{
"match": "(.{2,})"
},
{
"selector": {
"kind": "classMember",
"modifiers": [
"private"
]
},
"match": "_(.*)"
},
{
"selector": {
"kind": "classProperty",
"modifiers": [
"static",
"readonly"
]
},
"formats": [
"CONSTANT_CASE"
]
},
{
"selector": {
"kind": "const",
"scope": "global"
},
"match": "__(.+)__|_SPECIAL_|(.+)",
"formats": [
"CONSTANT_CASE"
]
},
{
"selector": {
"kind": "interface"
},
"match": "I(.*)|(.*?)Error",
"formats": [
"PascalCase"
]
}
// default conventions
]
}
}
}
}
}
}

以下是一些示例:

¥Hers some examples:

  • 规则报告了名为 _ 的私有类属性,因为它包含单个字符。根据第二种约定,名称应至少包含两个字符。

    ¥A private class property named _ is reported by the rule because it contains a single character. According to the second convention, the name should contain at least two characters.

  • 规则会报告变量 a_variable,因为它不符合禁止使用 snake_case 类型变量名的默认约定。首先根据第一种约定验证变量名。它会被转发到第二个约定,该约定也同样有效,因为它既不是 i 也不是 j。文件名会被捕获并传递给下一个约定。在我们的例子中,以下约定是默认约定。

    ¥A variable a_variable is reported by the rule because it doesn’t respect the default convention that forbid variable names in snake_case. The variable name is first verified against the first convention. It is forwarded to the second convention, which is also respected, because it is neither i nor j. The name is captured and is forwarded to the next convention. In our case, the next convention is the default one.

¥Regular expression syntax

match 选项采用支持以下语法的正则表达式:

¥The match option takes a regular expression that supports the following syntaxes:

  • 贪婪量词 *?+{n}{n,m}{n,}{m}

    ¥Greedy quantifiers *, ?, +, {n}, {n,m}, {n,}, {m}

  • 非贪婪量词 *???+?{n}?{n,m}?{n,}?{m}?

    ¥Non-greedy quantifiers *?, ??, +?, {n}?, {n,m}?, {n,}?, {m}?

  • 任何字符匹配器 .

    ¥Any character matcher .

  • 更改已建立语法的格式。

    ¥Character classes [a-z], [xyz], [^a-z]

  • 替代 |

    ¥Alternations |

  • 捕获组 ()

    ¥Capturing groups ()

  • 非捕获组 (?:)

    ¥Non-capturing groups (?:)

  • 不区分大小写的组 (?i:) 和区分大小写的组 (?-i:)

    ¥Case-insensitive groups (?i:) and case-sensitive groups (?-i:)

  • 包含所有特殊字符和常规字符串转义字符 \f\n\r\t\v 的有限转义字符集。请注意,你还可以使用字符类转义特殊字符。例如,\$[$] 是两个有效的模式,它们可以转义 $

    ¥A limited set of escaped characters including all special characters and regular string escape characters \f, \n, \r, \t, \v. Note that you can also escape special characters using character classes. For example, \$ and [$] are two valid patterns that escape $.

¥Related links