You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/eslint-plugin/docs/rules/naming-convention.md
+36-26Lines changed: 36 additions & 26 deletions
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ There are many different rules that have existed over time, but they have had th
10
10
This rule allows you to enforce conventions for any identifier, using granular selectors to create a fine-grained style guide.
11
11
By default, it enforces nothing.
12
12
13
-
### Note - this rule only needs type information in specfic cases, detailed below
13
+
### Note - this rule only needs type information in specific cases, detailed below
14
14
15
15
## Options
16
16
@@ -35,9 +35,10 @@ type Options = {
35
35
36
36
// selector options
37
37
selector:Selector;
38
+
filter?:string;
39
+
// the allowed values for these are dependent on the selector - see below
38
40
modifiers?:Modifiers<Selector>[];
39
41
types?:Types<Selector>[];
40
-
filter?:string;
41
42
}[];
42
43
43
44
const defaultOptions:Options= [];
@@ -54,7 +55,7 @@ When the format of an identifier is checked, it is checked in the following orde
54
55
1. validate suffix
55
56
1. validate format
56
57
57
-
At each step, if the identifier matches the options, it the matching part will be removed.
58
+
At each step, if the identifier matches the option, the matching part will be removed.
58
59
For example, if you provide the following formating option: `{ leadingUnderscore: 'allow', prefix: ['I'], format: ['StrictPascalCase'] }`, for the identifier `_IMyInterface`, then the following checks will occur:
59
60
60
61
1.`name = _IMyInterface`
@@ -66,6 +67,8 @@ For example, if you provide the following formating option: `{ leadingUnderscore
66
67
1. validate suffix - no check
67
68
1. validate format - pass
68
69
70
+
One final note is that if the name were to become empty via this trimming process, it is considered to match all `format`s. An example of where this might be useful is for generic type parameters, where you want all names to be prefixed with `T`, but also want to allow for the single character `T` name.
71
+
69
72
#### `format`
70
73
71
74
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:
@@ -83,7 +86,7 @@ The `leadingUnderscore` / `trailingUnderscore` options control whether leading/t
83
86
84
87
-`forbid` - a leading/trailing underscore is not allowed at all.
85
88
-`allow` - existence of a leading/trailing underscore is not explicitly enforced.
86
-
-`require` - a leading/trailing underscores must be included.
89
+
-`require` - a leading/trailing underscore must be included.
87
90
88
91
#### `prefix` / `suffix`
89
92
@@ -93,20 +96,40 @@ If these are provided, the identifier must start with one of the provided values
93
96
94
97
### Selector Options
95
98
96
-
The selector options determine which names that the formatting options will apply to.
97
-
Each value for `selector` has a set of `types` and `modifiers` that are allowed to be used with it, which are explained below.
99
+
-`selector` (see "Allowed Selectors, Modifiers and Types" below).
100
+
-`filter` accepts a regular expression (anything accepted into `new RegExp(filter)`). It allows you to limit the scope of this configuration to names that match this regex.
101
+
-`modifiers` allows you to specify which modifiers to granularly apply to, such as the accessibility (`private`/`public`/`protected`), or if the thing is `static`, etc.
102
+
- The name must match _all_ of the modifiers.
103
+
- For example, if you provide `{ modifiers: ['private', 'static', 'readonly'] }`, then it will only match something that is `private static readonly`, and something that is just `private` will not match.
104
+
-`types` allows you to specify which types to match. This option supports simple, primitive types only (`boolean`, `string`, `number`, `array`, `function`). This lets you do things like enforce that `boolean` variables are prefixed with a verb.
105
+
-**_NOTE - Using this option will require that you lint with type information._**
106
+
-`boolean` matches any type assignable to `boolean | null | undefined`
107
+
-`string` matches any type assignable to `string | null | undefined`
108
+
-`number` matches any type assignable to `number | null | undefined`
109
+
-`array` matches any type assignable to `Array<unknown> | null | undefined`
110
+
-`function` matches any type asignable to `Function | null | undefined`
98
111
99
-
`modifiers` allows you to specify which modifiers to granularly apply to, such as the accessibility (`private`/`public`/`protected`), or if the thing is `static`, etc. The name must match _all_ of the modifiers. For example, if you provide `{ modifiers: ['private', 'static', 'readonly'] }`, then it will only match something that is `private static readonly`, and something that is just `private` will not match.
112
+
The ordering of selectors does not matter. The implementation will automatically sort the selectors to ensure they match from most-specific to least specific. It will keep checking selectors in that order until it finds one that matches the name.
100
113
101
-
`types` allows you to specify which types to match. This option supports simple, primitive types only (`boolean`, `string`, `number`, `function`, `array`). This lets you do things like enforce that `boolean` variables are prefixed with a verb.
102
-
**_NOTE - Using this option will require that you lint with type information._**
114
+
For example, if you provide the following config:
103
115
104
-
`filter` accepts a regular expression (anything accepted into `new RegExp(filter)`). It allows you to limit the scope of this configuration to names that match this regex.
@@ -149,6 +172,8 @@ Individual Selectors match specific, well-defined sets. There is no overlap betw
149
172
- Allowed `modifiers`: none.
150
173
- Allowed `types`: none.
151
174
175
+
##### Group Selectors
176
+
152
177
Group Selectors are provided for convenience, and essentially bundle up sets of individual selectors.
153
178
154
179
-`default` - matches everything.
@@ -164,21 +189,6 @@ Group Selectors are provided for convenience, and essentially bundle up sets of
164
189
- Allowed `modifiers`: `abstract`.
165
190
- Allowed `types`: none.
166
191
167
-
The ordering of selectors does not matter. The implementation will automatically sort the selectors to ensure they match from most-specific to least specific. It will keep checking selectors in that order until it finds one that matches the name.
0 commit comments