Back to Biomejs

useNamingConvention

src/content/docs/linter/rules/use-naming-convention.mdx

latest50.5 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="TypeScript and TSX" icon="seti:typescript"> ## Summary - Rule available since: `v1.0.0` - Diagnostic Category: [`lint/style/useNamingConvention`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Inspired from [`@typescript-eslint/naming-convention`](https://typescript-eslint.io/rules/naming-convention)

How to configure

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

Description

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

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

All 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.

js
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:

js
let a_value = 0;
<pre class="language-text"><code class="language-text">code-block.js:1:5 <a href="https://biomejs.dev/linter/rules/use-naming-convention">lint/style/useNamingConvention</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This </span><span style="color: lightgreen;"><strong>let</strong></span><span style="color: lightgreen;"> name should be in </span><span style="color: lightgreen;"><strong>camelCase or PascalCase</strong></span><span style="color: lightgreen;">.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>let a&#95;value = 0; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Rename this symbol in </span><span style="color: lightgreen;"><strong>camelCase</strong></span><span style="color: lightgreen;">.</span> <strong>1</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;">l</span><span style="color: Tomato;">e</span><span style="color: Tomato;">t</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;"><strong>a</strong></span><span style="color: Tomato;"><strong>&#95;</strong></span><span style="color: Tomato;"><strong>v</strong></span><span style="color: Tomato;"><strong>a</strong></span><span style="color: Tomato;"><strong>l</strong></span><span style="color: Tomato;"><strong>u</strong></span><span style="color: Tomato;"><strong>e</strong></span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">=</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">0</span><span style="color: Tomato;">;</span> <strong>1</strong><strong> │ </strong><span style="color: MediumSeaGreen;">+</span> <span style="color: MediumSeaGreen;">l</span><span style="color: MediumSeaGreen;">e</span><span style="color: MediumSeaGreen;">t</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;"><strong>a</strong></span><span style="color: MediumSeaGreen;"><strong>V</strong></span><span style="color: MediumSeaGreen;"><strong>a</strong></span><span style="color: MediumSeaGreen;"><strong>l</strong></span><span style="color: MediumSeaGreen;"><strong>u</strong></span><span style="color: MediumSeaGreen;"><strong>e</strong></span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">=</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">0</span><span style="color: MediumSeaGreen;">;</span> <strong>2</strong> <strong>2</strong><strong> │ </strong> </code></pre>
js
const fooYPosition = 0;
<pre class="language-text"><code class="language-text">code-block.js:1:7 <a href="https://biomejs.dev/linter/rules/use-naming-convention">lint/style/useNamingConvention</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Two consecutive uppercase characters are not allowed in camelCase because </span><span style="color: lightgreen;"><strong>strictCase</strong></span><span style="color: lightgreen;"> is set to &#96;true&#96;.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>const fooYPosition = 0; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">If you want to use consecutive uppercase characters in camelCase, then set the </span><span style="color: lightgreen;"><strong>strictCase</strong></span><span style="color: lightgreen;"> option to &#96;false&#96;. </span> <span style="color: lightgreen;">See the rule </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/rules/use-naming-convention#options">options</a></span><span style="color: lightgreen;"> for more details.</span> </code></pre>
js
function f(FIRST_PARAM) {}
<pre class="language-text"><code class="language-text">code-block.js:1:12 <a href="https://biomejs.dev/linter/rules/use-naming-convention">lint/style/useNamingConvention</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This </span><span style="color: lightgreen;"><strong>function parameter</strong></span><span style="color: lightgreen;"> name should be in </span><span style="color: lightgreen;"><strong>camelCase or PascalCase</strong></span><span style="color: lightgreen;">.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>function f(FIRST&#95;PARAM) &#123;&#125; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Rename this symbol in </span><span style="color: lightgreen;"><strong>PascalCase</strong></span><span style="color: lightgreen;">.</span> <strong>1</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;">f</span><span style="color: Tomato;">u</span><span style="color: Tomato;">n</span><span style="color: Tomato;">c</span><span style="color: Tomato;">t</span><span style="color: Tomato;">i</span><span style="color: Tomato;">o</span><span style="color: Tomato;">n</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">f</span><span style="color: Tomato;">(</span><span style="color: Tomato;"><strong>F</strong></span><span style="color: Tomato;"><strong>I</strong></span><span style="color: Tomato;"><strong>R</strong></span><span style="color: Tomato;"><strong>S</strong></span><span style="color: Tomato;"><strong>T</strong></span><span style="color: Tomato;"><strong>&#95;</strong></span><span style="color: Tomato;"><strong>P</strong></span><span style="color: Tomato;"><strong>A</strong></span><span style="color: Tomato;"><strong>R</strong></span><span style="color: Tomato;"><strong>A</strong></span><span style="color: Tomato;"><strong>M</strong></span><span style="color: Tomato;">)</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">&#123;</span><span style="color: Tomato;">&#125;</span> <strong>1</strong><strong> │ </strong><span style="color: MediumSeaGreen;">+</span> <span style="color: MediumSeaGreen;">f</span><span style="color: MediumSeaGreen;">u</span><span style="color: MediumSeaGreen;">n</span><span style="color: MediumSeaGreen;">c</span><span style="color: MediumSeaGreen;">t</span><span style="color: MediumSeaGreen;">i</span><span style="color: MediumSeaGreen;">o</span><span style="color: MediumSeaGreen;">n</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">f</span><span style="color: MediumSeaGreen;">(</span><span style="color: MediumSeaGreen;"><strong>F</strong></span><span style="color: MediumSeaGreen;"><strong>i</strong></span><span style="color: MediumSeaGreen;"><strong>r</strong></span><span style="color: MediumSeaGreen;"><strong>s</strong></span><span style="color: MediumSeaGreen;"><strong>t</strong></span><span style="color: MediumSeaGreen;"><strong>P</strong></span><span style="color: MediumSeaGreen;"><strong>a</strong></span><span style="color: MediumSeaGreen;"><strong>r</strong></span><span style="color: MediumSeaGreen;"><strong>a</strong></span><span style="color: MediumSeaGreen;"><strong>m</strong></span><span style="color: MediumSeaGreen;">)</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">&#123;</span><span style="color: MediumSeaGreen;">&#125;</span> <strong>2</strong> <strong>2</strong><strong> │ </strong> </code></pre>

Function names

  • A function name is in camelCase or PascalCase.
  • A global function can also be in UPPERCASE. This allows supporting the frameworks that require some function to use valid HTTP method names.
jsx
function trimString(s) { /*...*/ }

function Component() {
    return <div></div>;
}

export function GET() { /*...*/ }

TypeScript enum names

A 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.

ts
enum Status {
    Open,
    Close,
}

Classes

js
class Person {
    static MAX_FRIEND_COUNT = 256;

    static get SPECIAL_PERSON_INSTANCE() { /*...*/ }

    initializedProperty = 0;

    specialMethod() {}
}

TypeScript type aliases and interface

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

  • Member names of a type are in camelCase.

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

ts
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:

ts
type person = { fullName: string };
<pre class="language-text"><code class="language-text">code-block.ts:1:6 <a href="https://biomejs.dev/linter/rules/use-naming-convention">lint/style/useNamingConvention</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This </span><span style="color: lightgreen;"><strong>type alias</strong></span><span style="color: lightgreen;"> name should be in </span><span style="color: lightgreen;"><strong>PascalCase</strong></span><span style="color: lightgreen;">.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>type person = &#123; fullName: string &#125;; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Rename this symbol in </span><span style="color: lightgreen;"><strong>PascalCase</strong></span><span style="color: lightgreen;">.</span> <strong>1</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;">t</span><span style="color: Tomato;">y</span><span style="color: Tomato;">p</span><span style="color: Tomato;">e</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;"><strong>p</strong></span><span style="color: Tomato;"><strong>e</strong></span><span style="color: Tomato;"><strong>r</strong></span><span style="color: Tomato;"><strong>s</strong></span><span style="color: Tomato;"><strong>o</strong></span><span style="color: Tomato;"><strong>n</strong></span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">=</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">&#123;</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">f</span><span style="color: Tomato;">u</span><span style="color: Tomato;">l</span><span style="color: Tomato;">l</span><span style="color: Tomato;">N</span><span style="color: Tomato;">a</span><span style="color: Tomato;">m</span><span style="color: Tomato;">e</span><span style="color: Tomato;">:</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">s</span><span style="color: Tomato;">t</span><span style="color: Tomato;">r</span><span style="color: Tomato;">i</span><span style="color: Tomato;">n</span><span style="color: Tomato;">g</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">&#125;</span><span style="color: Tomato;">;</span> <strong>1</strong><strong> │ </strong><span style="color: MediumSeaGreen;">+</span> <span style="color: MediumSeaGreen;">t</span><span style="color: MediumSeaGreen;">y</span><span style="color: MediumSeaGreen;">p</span><span style="color: MediumSeaGreen;">e</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;"><strong>P</strong></span><span style="color: MediumSeaGreen;"><strong>e</strong></span><span style="color: MediumSeaGreen;"><strong>r</strong></span><span style="color: MediumSeaGreen;"><strong>s</strong></span><span style="color: MediumSeaGreen;"><strong>o</strong></span><span style="color: MediumSeaGreen;"><strong>n</strong></span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">=</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">&#123;</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">f</span><span style="color: MediumSeaGreen;">u</span><span style="color: MediumSeaGreen;">l</span><span style="color: MediumSeaGreen;">l</span><span style="color: MediumSeaGreen;">N</span><span style="color: MediumSeaGreen;">a</span><span style="color: MediumSeaGreen;">m</span><span style="color: MediumSeaGreen;">e</span><span style="color: MediumSeaGreen;">:</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">s</span><span style="color: MediumSeaGreen;">t</span><span style="color: MediumSeaGreen;">r</span><span style="color: MediumSeaGreen;">i</span><span style="color: MediumSeaGreen;">n</span><span style="color: MediumSeaGreen;">g</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">&#125;</span><span style="color: MediumSeaGreen;">;</span> <strong>2</strong> <strong>2</strong><strong> │ </strong> </code></pre>

Literal object member names

js
const alice = {
    fullName: "Alice",
}

Example of an incorrect name:

js
const alice = {
    full_name: "Alice",
}
<pre class="language-text"><code class="language-text">code-block.js:2:5 <a href="https://biomejs.dev/linter/rules/use-naming-convention">lint/style/useNamingConvention</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This </span><span style="color: lightgreen;"><strong>object property</strong></span><span style="color: lightgreen;"> name should be in </span><span style="color: lightgreen;"><strong>camelCase</strong></span><span style="color: lightgreen;">.</span> <strong>1 │ </strong>const alice = &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> full&#95;name: &quot;Alice&quot;, <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong>&#125; <strong>4 │ </strong> </code></pre>

Import and export aliases and namespaces

Import and export namespaces are in camelCase or PascalCase.

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

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

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

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

Examples of an incorrect name:

ts
import * as MY_LIB from "my-lib";
<pre class="language-text"><code class="language-text">code-block.ts:1:13 <a href="https://biomejs.dev/linter/rules/use-naming-convention">lint/style/useNamingConvention</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This </span><span style="color: lightgreen;"><strong>import namespace</strong></span><span style="color: lightgreen;"> name should be in </span><span style="color: lightgreen;"><strong>camelCase or PascalCase</strong></span><span style="color: lightgreen;">.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>import &#42; as MY&#95;LIB from &quot;my-lib&quot;; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Rename this symbol in </span><span style="color: lightgreen;"><strong>PascalCase</strong></span><span style="color: lightgreen;">.</span> <strong>1</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;">i</span><span style="color: Tomato;">m</span><span style="color: Tomato;">p</span><span style="color: Tomato;">o</span><span style="color: Tomato;">r</span><span style="color: Tomato;">t</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">&#42;</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">a</span><span style="color: Tomato;">s</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;"><strong>M</strong></span><span style="color: Tomato;"><strong>Y</strong></span><span style="color: Tomato;"><strong>&#95;</strong></span><span style="color: Tomato;"><strong>L</strong></span><span style="color: Tomato;"><strong>I</strong></span><span style="color: Tomato;"><strong>B</strong></span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">f</span><span style="color: Tomato;">r</span><span style="color: Tomato;">o</span><span style="color: Tomato;">m</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">&quot;</span><span style="color: Tomato;">m</span><span style="color: Tomato;">y</span><span style="color: Tomato;">-</span><span style="color: Tomato;">l</span><span style="color: Tomato;">i</span><span style="color: Tomato;">b</span><span style="color: Tomato;">&quot;</span><span style="color: Tomato;">;</span> <strong>1</strong><strong> │ </strong><span style="color: MediumSeaGreen;">+</span> <span style="color: MediumSeaGreen;">i</span><span style="color: MediumSeaGreen;">m</span><span style="color: MediumSeaGreen;">p</span><span style="color: MediumSeaGreen;">o</span><span style="color: MediumSeaGreen;">r</span><span style="color: MediumSeaGreen;">t</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">&#42;</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">a</span><span style="color: MediumSeaGreen;">s</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;"><strong>M</strong></span><span style="color: MediumSeaGreen;"><strong>y</strong></span><span style="color: MediumSeaGreen;"><strong>L</strong></span><span style="color: MediumSeaGreen;"><strong>i</strong></span><span style="color: MediumSeaGreen;"><strong>b</strong></span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">f</span><span style="color: MediumSeaGreen;">r</span><span style="color: MediumSeaGreen;">o</span><span style="color: MediumSeaGreen;">m</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">&quot;</span><span style="color: MediumSeaGreen;">m</span><span style="color: MediumSeaGreen;">y</span><span style="color: MediumSeaGreen;">-</span><span style="color: MediumSeaGreen;">l</span><span style="color: MediumSeaGreen;">i</span><span style="color: MediumSeaGreen;">b</span><span style="color: MediumSeaGreen;">&quot;</span><span style="color: MediumSeaGreen;">;</span> <strong>2</strong> <strong>2</strong><strong> │ </strong> </code></pre>

TypeScript type parameter names

A TypeScript type parameter name is in PascalCase.

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

TypeScript namespace names

A TypeScript namespace name is in camelCase or in PascalCase.

ts
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
js
class C {
  ["not an identifier"]() {}
}
  • Named imports
js
 import { an_IMPORT } from "mod"
  • Destructured object properties
js
const { destructed_PROP } = obj;
  • Class members marked with override:
ts
class C extends B {
  override overridden_METHOD() {}
}
  • Declarations inside an external TypeScript module
ts
declare module "myExternalModule" {
  export interface my_INTERFACE {}
}
  • Declarations inside a global declaration
ts
declare global {
  interface HTMLElement {}
}

Options

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

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

strictCase

When 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:

json
{
	"linter": {
		"rules": {
			"style": {
				"useNamingConvention": {
					"options": {
						"strictCase": true
					}
				}
			}
		}
	}
}

js
class HTTPServer {
}
<pre class="language-text"><code class="language-text">code-block.js:1:7 <a href="https://biomejs.dev/linter/rules/use-naming-convention">lint/style/useNamingConvention</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Two consecutive uppercase characters are not allowed in PascalCase because </span><span style="color: lightgreen;"><strong>strictCase</strong></span><span style="color: lightgreen;"> is set to &#96;true&#96;.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>class HTTPServer &#123; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong>&#125; <strong>3 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">If you want to use consecutive uppercase characters in PascalCase, then set the </span><span style="color: lightgreen;"><strong>strictCase</strong></span><span style="color: lightgreen;"> option to &#96;false&#96;. </span> <span style="color: lightgreen;">See the rule </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/rules/use-naming-convention#options">options</a></span><span style="color: lightgreen;"> for more details.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Rename this symbol in </span><span style="color: lightgreen;"><strong>PascalCase</strong></span><span style="color: lightgreen;">.</span> <strong>1</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;">c</span><span style="color: Tomato;">l</span><span style="color: Tomato;">a</span><span style="color: Tomato;">s</span><span style="color: Tomato;">s</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;"><strong>H</strong></span><span style="color: Tomato;"><strong>T</strong></span><span style="color: Tomato;"><strong>T</strong></span><span style="color: Tomato;"><strong>P</strong></span><span style="color: Tomato;"><strong>S</strong></span><span style="color: Tomato;"><strong>e</strong></span><span style="color: Tomato;"><strong>r</strong></span><span style="color: Tomato;"><strong>v</strong></span><span style="color: Tomato;"><strong>e</strong></span><span style="color: Tomato;"><strong>r</strong></span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">&#123;</span> <strong>1</strong><strong> │ </strong><span style="color: MediumSeaGreen;">+</span> <span style="color: MediumSeaGreen;">c</span><span style="color: MediumSeaGreen;">l</span><span style="color: MediumSeaGreen;">a</span><span style="color: MediumSeaGreen;">s</span><span style="color: MediumSeaGreen;">s</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;"><strong>H</strong></span><span style="color: MediumSeaGreen;"><strong>t</strong></span><span style="color: MediumSeaGreen;"><strong>t</strong></span><span style="color: MediumSeaGreen;"><strong>p</strong></span><span style="color: MediumSeaGreen;"><strong>S</strong></span><span style="color: MediumSeaGreen;"><strong>e</strong></span><span style="color: MediumSeaGreen;"><strong>r</strong></span><span style="color: MediumSeaGreen;"><strong>v</strong></span><span style="color: MediumSeaGreen;"><strong>e</strong></span><span style="color: MediumSeaGreen;"><strong>r</strong></span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">&#123;</span> <strong>2</strong> <strong>2</strong><strong> │ </strong> &#125; <strong>3</strong> <strong>3</strong><strong> │ </strong> </code></pre>

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

json
{
	"linter": {
		"rules": {
			"style": {
				"useNamingConvention": {
					"options": {
						"strictCase": false
					}
				}
			}
		}
	}
}

js
class HTTPServer {
}

requireAscii

When 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: true

conventions

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:

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: the kind of the declaration among:

    • any (default kind if the kind is unset)

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

    • class

    • enum

    • enumMember

    • interface

    • typeAlias

    • function: named function declarations and expressions

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

    • namespace: TypeScript namespaces

    • importNamespace

    • exportNamespace

    • importAlias: default imports and aliases of named imports

    • exportAlias: aliases of re-exported names

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

      • const
      • let
      • var
      • using
    • functionParameter

    • catchParameter

    • indexParameter: parameters of index signatures

    • typeParameter: generic type parameter

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

      • classProperty: class properties, including parameter properties
      • classMethod
      • classGetter
      • classSetter
    • objectLiteralMember: literal object properties, methods, getters, and setters (you might want to duplicate the convention for typeMember)

      • objectLiteralProperty
      • objectLiteralMethod
      • objectLiteralGetter
      • objectLiteralSetter
    • typeMember: properties, methods, getters, and setters declared in type aliases and interfaces

      • typeProperty
      • typeMethod
      • typeGetter
      • typeSetter
  • modifiers: an array of modifiers among:

    • abstract: applies to class members and classes
    • private: applies to class members
    • protected: applies to class members
    • readonly: applies to class members and type members
    • static: 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 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:

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.

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:

ts
class C {
    static readonly prop = 0;
}
<pre class="language-text"><code class="language-text">code-block.ts:2:21 <a href="https://biomejs.dev/linter/rules/use-naming-convention">lint/style/useNamingConvention</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This </span><span style="color: lightgreen;"><strong>readonly static class property</strong></span><span style="color: lightgreen;"> name should be in </span><span style="color: lightgreen;"><strong>CONSTANT&#95;CASE</strong></span><span style="color: lightgreen;">.</span> <strong>1 │ </strong>class C &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> static readonly prop = 0; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong>&#125; <strong>4 │ </strong> </code></pre>

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.

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

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.

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

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.
  • The captured name (the name without the leading _) is in camelCase.
json
{
	"linter": {
		"rules": {
			"style": {
				"useNamingConvention": {
					"options": {
						"conventions": [
							{
								"selector": {
									"kind": "classMember",
									"modifiers": [
										"private"
									]
								},
								"match": "_(.+)",
								"formats": [
									"camelCase"
								]
							}
						]
					}
				}
			}
		}
	}
}

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.

jsonc
{
	"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...

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

can be factorized to...

jsonc
{
	"linter": {
		"rules": {
			"style": {
				"useNamingConvention": {
					"options": {
						"conventions": [
							{
								"selector": {
									"kind": "classMember",
									"modifiers": [
										"private"
									]
								},
								"match": "_(.+)"
							},
							{
								"selector": {
									"kind": "classMember"
								},
								"formats": [
									"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:

jsonc
{
	"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:

jsonc
{
	"linter": {
		"rules": {
			"style": {
				"useNamingConvention": {
					"options": {
						"conventions": [
							// your conventions
							// ...

							// Otherwise, accept anything
							{
								"match": ".*"
							}
						]
					}
				}
			}
		}
	}
}

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

  1. 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. 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. A static readonly class property name is in CONSTANT_CASE.
  5. A global constant is in CONSTANT_CASE and can be enclosed by double underscores or to be named _SPECIAL_.
  6. An interface name starts with I, except for interfaces ending with Error, and is in PascalCase.
  7. All other names follow the default conventions
jsonc
{
	"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 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

The 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. Note that you can also escape special characters using character classes. For example, \$ and [$] are two valid patterns that escape $.
</TabItem> </Tabs>