Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7b68c47

Browse files
committed
docs: add examples
1 parent aa949d6 commit 7b68c47

File tree

1 file changed

+121
-1
lines changed

1 file changed

+121
-1
lines changed

packages/eslint-plugin/docs/rules/naming-convention.md

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ If these are provided, the identifier must start with one of the provided values
101101
- `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.
102102
- The name must match _all_ of the modifiers.
103103
- 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.
104+
- `types` allows you to specify which types to match. This option supports simple, primitive types only (`boolean`, `string`, `number`, `array`, `function`).
105+
- The name must match _one_ of the types.
105106
- **_NOTE - Using this option will require that you lint with type information._**
107+
- For example, this lets you do things like enforce that `boolean` variables are prefixed with a verb.
106108
- `boolean` matches any type assignable to `boolean | null | undefined`
107109
- `string` matches any type assignable to `string | null | undefined`
108110
- `number` matches any type assignable to `number | null | undefined`
@@ -189,6 +191,124 @@ Group Selectors are provided for convenience, and essentially bundle up sets of
189191
- Allowed `modifiers`: `abstract`.
190192
- Allowed `types`: none.
191193

194+
## Examples
195+
196+
### Enforce that all variables, functions and properties follow are camelCase
197+
198+
```json
199+
{
200+
"@typescript-eslint/naming-conventions": [
201+
"error",
202+
{ "selector": "variableLike", "format": ["camelCase"] }
203+
]
204+
}
205+
```
206+
207+
### Enforce that private members are prefixed with an underscore
208+
209+
```json
210+
{
211+
"@typescript-eslint/naming-conventions": [
212+
"error",
213+
{
214+
"selector": "memberLike",
215+
"modifier": ["private"],
216+
"format": ["camelCase"],
217+
"leadingUnderscore": "require"
218+
}
219+
]
220+
}
221+
```
222+
223+
### Enforce that boolean variables are prefixed with an allowed verb
224+
225+
```json
226+
{
227+
"@typescript-eslint/naming-conventions": [
228+
"error",
229+
{
230+
"selector": "variable",
231+
"types": ["boolean"],
232+
"format": ["PascalCase"],
233+
"prefix": ["is", "should", "has", "can", "did", "will"]
234+
}
235+
]
236+
}
237+
```
238+
239+
### Enforce that all variables are either in camelCase or UPPER_CASE
240+
241+
```json
242+
{
243+
"@typescript-eslint/naming-conventions": [
244+
"error",
245+
{
246+
"selector": "variable",
247+
"format": ["camelCase", "UPPER_CASE"]
248+
}
249+
]
250+
}
251+
```
252+
253+
### Enforce that type parameters (generics) are prefixed with `T`
254+
255+
```json
256+
{
257+
"@typescript-eslint/naming-conventions": [
258+
"error",
259+
{
260+
"selector": "typeParameter",
261+
"format": ["PascalCase"],
262+
"prefix": ["T"]
263+
}
264+
]
265+
}
266+
```
267+
268+
### Enforce the codebase follows eslint's `camelcase` conventions
269+
270+
```json
271+
{
272+
"@typescript-eslint/naming-conventions": [
273+
"error",
274+
{
275+
"selector": "default",
276+
"format": ["camelCase"]
277+
},
278+
279+
{
280+
"selector": "variableLike",
281+
"format": ["camelCase"]
282+
},
283+
{
284+
"selector": "variable",
285+
"format": ["camelCase", "UPPER_CASE"]
286+
},
287+
{
288+
"selector": "parameter",
289+
"format": ["camelCase"],
290+
"leadingUnderscore": "allow"
291+
},
292+
293+
{
294+
"selector": "memberLike",
295+
"format": ["camelCase"]
296+
},
297+
{
298+
"selector": "memberLike",
299+
"modifiers": ["private"],
300+
"format": ["camelCase"],
301+
"leadingUnderscore": "require"
302+
},
303+
304+
{
305+
"selector": "typeLike",
306+
"format": ["PascalCase"]
307+
}
308+
]
309+
}
310+
```
311+
192312
## When Not To Use It
193313

194314
If you do not want to enforce naming conventions for anything.

0 commit comments

Comments
 (0)