1 | # Enforces naming conventions for class members by visibility. (member-naming)
|
2 |
|
3 | It can be helpful to enforce naming conventions for `private` (and sometimes `protected`) members of an object. For example, prefixing private properties with a `_` allows them to be easily discerned when being inspected by tools that do not have knowledge of TypeScript (such as most debuggers).
|
4 |
|
5 | ## Rule Details
|
6 |
|
7 | This rule allows you to enforce conventions for class property names by their visibility. By default, it enforces nothing.
|
8 |
|
9 | ## Options
|
10 |
|
11 | You can specify a regular expression to test the names of properties for each visibility level: `public`, `protected`, `private`.
|
12 |
|
13 | Examples of **correct** code with `{ "private": "^_" }` specified:
|
14 |
|
15 | ```ts
|
16 | class HappyClass {
|
17 | private _foo: string;
|
18 | private _bar = 123;
|
19 | private _fizz() {}
|
20 | }
|
21 | ```
|
22 |
|
23 | Examples of **incorrect** code with `{ "private": "^_" }` specified:
|
24 |
|
25 | ```ts
|
26 | class SadClass {
|
27 | private foo: string;
|
28 | private bar = 123;
|
29 | private fizz() {}
|
30 | }
|
31 | ```
|
32 |
|
33 | ## When Not To Use It
|
34 |
|
35 | If you do not want to enforce per-visibility naming rules for member properties.
|
36 |
|
37 | ## Further Reading
|
38 |
|
39 | * ESLint's [`camelcase` rule](https://eslint.org/docs/rules/camelcase)
|