useNamingConvention
このコンテンツはまだ日本語訳がありません。
Diagnostic Category: lint/style/useNamingConvention
Since: v1.0.0
Sources:
- Inspired from:
@typescript-eslint/naming-convention
Enforce naming conventions for everything across a codebase.
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
Section titled Naming conventionsAll names can be prefixed and suffixed by underscores _
and dollar signs $
.
Variable and parameter names
Section titled Variable and parameter namesAll variables and function parameters are in camelCase
or PascalCase
.
Catch parameters are in camelCase
.
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.
Examples of incorrect names:
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 │
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.
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 camelCase.
1 │ - function·f(FIRST_PARAM)·{}
1 │ + function·f(firstParam)·{}
2 2 │
Function names
Section titled Function names- A
function
name is incamelCase
orPascalCase
. - A global
function
can also be inUPPERCASE
. This allows supporting the frameworks that require some function to use valid HTTP method names.
TypeScript enum
names
Section titled TypeScript enum namesA TypeScript enum
name is in PascalCase
.
enum
members are by default in PascalCase
.
However, you can configure the case of enum
members.
See options for more details.
Classes
Section titled Classes-
A class name is in
PascalCase
. -
Static property and static getter names are in
camelCase
orCONSTANT_CASE
. -
Class property and method names are in
camelCase
.
TypeScript type
aliases and interface
Section titled TypeScript type aliases and interface-
A
type
alias or an interface name are inPascalCase
. -
Member names of a type are in
camelCase
. -
readonly
property and getter names can also be inCONSTANT_CASE
.
Examples of an incorrect type alias:
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
Section titled Literal object member names- Literal object members are in
camelCase
.
Example of an incorrect name:
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
Section titled Import and export aliases and namespacesImport and export namespaces are in camelCase
or PascalCase
.
import
and export
aliases are in camelCase
, PascalCase
, or CONSTANT_CASE
:
Examples of an incorrect name:
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 camelCase.
1 │ - import·*·as·MY_LIB·from·“my-lib”;
1 │ + import·*·as·myLib·from·“my-lib”;
2 2 │
TypeScript type parameter names
Section titled TypeScript type parameter namesA TypeScript type parameter name is in PascalCase
.
TypeScript namespace
names
Section titled TypeScript namespace namesA TypeScript namespace
name is in camelCase
or in PascalCase
.
Ignored declarations
Section titled Ignored declarationsNote that some declarations are always ignored. You cannot apply a convention to them. This is the case for:
- Member names that are not identifiers
- Named imports
- Destructured object properties
- Class members marked with
override
:
- Declarations inside an external TypeScript module
Options
Section titled OptionsThe rule provides several options that are detailed in the following subsections.
strictCase
Section titled strictCaseWhen this option is set to true
, it forbids consecutive uppercase characters in camelCase
and PascalCase
.
Default: true
For instance, HTTPServer
or aHTTPServer
are not permitted for strictCase: true
.
These names should be renamed to HttpServer
and aHttpServer
:
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 │
When strictCase
is set to false
, consecutive uppercase characters are allowed.
For example, HTTPServer
and aHTTPServer
would be considered valid then:
requireAscii
Section titled requireAsciiWhen true
, names must only consist of ASCII characters only,
forbidding names like café
or 안녕하세요
that include non-ASCII characters.
When requireAscii
is set to false
, names may include non-ASCII characters.
For example, café
and 안녕하세요
would be considered valid then.
Default: false
This option will be turned on by default in Biome 2.0.
enumMemberCase
Section titled enumMemberCaseBy default, the rule enforces the naming convention followed by the TypeScript Compiler team:
an enum
member is in PascalCase
.
You can enforce another convention by setting enumMemberCase
option.
The supported cases are: PascalCase
, CONSTANT_CASE
, and camelCase
.
This option will be deprecated in the future.
Use the conventions
option instead.
conventions (Since v1.8.0)
Section titled conventions (Since v1.8.0)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
).
For example, you can enforce the use of CONSTANT_CASE
for global const
declarations:
A selector describes which declarations the convention applies to. You can select a declaration based on several criteria:
-
kind
: the kind of the declaration among:any
(default kind if the kind is unset)typeLike
: classes, enums, type aliases, and interfacesclass
enum
interface
typeAlias
function
: named function declarations and expressionsnamespaceLike
: TypeScript namespaces, import and export namespaces (import * as namespace from
)namespace
: TypeScript namespacesimportNamespace
exportNamespace
importAlias
: default imports and aliases of named importsexportAlias
: aliases of re-exported namesvariable
: const, let, using, and var declarationsconst
let
var
using
functionParameter
catchParameter
indexParameter
: parameters of index signaturestypeParameter
: generic type parameterclassMember
: class properties, parameter properties, methods, getters, and settersclassProperty
: class properties, including parameter propertiesclassMethod
classGetter
classSetter
objectLiteralMember
: literal object properties, methods, getters, and settersobjectLiteralProperty
objectLiteralMethod
objectLiteralGetter
objectLiteralSetter
typeMember
: properties, methods, getters, and setters declared in type aliases and interfacestypeProperty
typeMethod
typeGetter
typeSetter
-
modifiers
: an array of modifiers among:abstract
: applies to class members and classesprivate
: applies to class membersprotected
: applies to class membersreadonly
: applies to class members and type membersstatic
: applies to class members
-
scope
: where the declaration appears. Allowed values:any
: anywhere (default value if the scope is unset)global
: the global scope (also includes the namespace scopes)
For each declaration,
the conventions
array is traversed 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:
match
: a regular expression that the name of the declaration must match.formats
: the string case that the name must follow. The supported cases are:PascalCase
,CONSTANT_CASE
,camelCase
, andsnake_case
.
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 check the following conventions:
- A private property starts with
_
and consists of at least two characters - The captured name (the name without the leading
_
) is incamelCase
.
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.
In the following example, we require that private class members start with _
and all class members are in [“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:
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.
You can reset all default conventions by adding a convention at the end of the array that accepts anything:
Let’s take a more complex example with the following conventions:
- Accept variable names
i
,j
, and check all other names against the next conventions. - All identifiers must contain at least two characters.
- We require
private
class members to start with an underscore_
. - We require
static readonly
class properties to be inCONSTANT_CASE
. Aprivate static readonly
property must also start with an underscore as dictated by the previous convention. - We require global constants to be in
CONSTANT_CASE
and we allow these constants to be enclosed by double underscores or to be named_SPECIAL_
. - We require interfaces to start with
I
, except for interfaces ending withError
, and to be inPascalCase
. - All other names follow the default conventions
Regular expression syntax
Section titled Regular expression syntaxThe match
option takes a regular expression that supports the following syntaxes:
- Greedy quantifiers
*
,?
,+
,{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
(?:)
- Case-insensitive groups
(?i:)
and case-sensitive groups(?-i:)
- A limited set of escaped characters including all special characters
and regular string escape characters
\f
,\n
,\r
,\t
,\v