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

Skip to content

Commit 1cb1cb5

Browse files
authored
docs(eslint-plugin): [naming-convention] document ignoring quoted properties (typescript-eslint#2071)
1 parent 071e5a0 commit 1cb1cb5

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ Group Selectors are provided for convenience, and essentially bundle up sets of
307307

308308
### Enforce that type parameters (generics) are prefixed with `T`
309309

310+
This allows you to emulate the old `generic-type-naming` rule.
311+
310312
```json
311313
{
312314
"@typescript-eslint/naming-convention": [
@@ -322,6 +324,8 @@ Group Selectors are provided for convenience, and essentially bundle up sets of
322324

323325
### Enforce that interface names do not begin with an `I`
324326

327+
This allows you to emulate the old `interface-name-prefix` rule.
328+
325329
```json
326330
{
327331
"@typescript-eslint/naming-convention": [
@@ -339,6 +343,49 @@ Group Selectors are provided for convenience, and essentially bundle up sets of
339343
}
340344
```
341345

346+
### Ignore properties that require quotes
347+
348+
Sometimes you have to use a quoted name that breaks the convention (for example, HTTP headers).
349+
If this is a common thing in your codebase, then you can use the `filter` option in one of two ways:
350+
351+
You can use the `filter` option to ignore specific names only:
352+
353+
```jsonc
354+
{
355+
"@typescript-eslint/naming-convention": [
356+
"error",
357+
{
358+
"selector": "property",
359+
"format": ["strictCamelCase"],
360+
"filter": {
361+
// you can expand this regex to add more allowed names
362+
"regex": "^(Property-Name-One|Property-Name-Two)$",
363+
"match": false
364+
}
365+
}
366+
]
367+
}
368+
```
369+
370+
You can use the `filter` option to ignore names that require quoting:
371+
372+
```jsonc
373+
{
374+
"@typescript-eslint/naming-convention": [
375+
"error",
376+
{
377+
"selector": "property",
378+
"format": ["strictCamelCase"],
379+
"filter": {
380+
// you can expand this regex as you find more cases that require quoting that you want to allow
381+
"regex": "[- ]",
382+
"match": false
383+
}
384+
}
385+
]
386+
}
387+
```
388+
342389
### Enforce the codebase follows ESLint's `camelcase` conventions
343390

344391
```json

packages/eslint-plugin/tests/rules/naming-convention.test.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,40 @@ ruleTester.run('naming-convention', rule, {
758758
},
759759
],
760760
},
761+
{
762+
code: `
763+
const foo = {
764+
'Property-Name': 'asdf',
765+
};
766+
`,
767+
options: [
768+
{
769+
format: ['strictCamelCase'],
770+
selector: 'default',
771+
filter: {
772+
regex: /-/.source,
773+
match: false,
774+
},
775+
},
776+
],
777+
},
778+
{
779+
code: `
780+
const foo = {
781+
'Property-Name': 'asdf',
782+
};
783+
`,
784+
options: [
785+
{
786+
format: ['strictCamelCase'],
787+
selector: 'default',
788+
filter: {
789+
regex: /^(Property-Name)$/.source,
790+
match: false,
791+
},
792+
},
793+
],
794+
},
761795
],
762796
invalid: [
763797
...createInvalidTestCases(cases),
@@ -965,5 +999,34 @@ ruleTester.run('naming-convention', rule, {
965999
},
9661000
],
9671001
},
1002+
{
1003+
code: `
1004+
const foo = {
1005+
'Property Name': 'asdf',
1006+
};
1007+
`,
1008+
options: [
1009+
{
1010+
format: ['strictCamelCase'],
1011+
selector: 'default',
1012+
filter: {
1013+
regex: /-/.source,
1014+
match: false,
1015+
},
1016+
},
1017+
],
1018+
errors: [
1019+
{
1020+
line: 3,
1021+
messageId: 'doesNotMatchFormat',
1022+
data: {
1023+
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
1024+
type: 'Property',
1025+
name: 'Property Name',
1026+
formats: 'strictCamelCase',
1027+
},
1028+
},
1029+
],
1030+
},
9681031
],
9691032
});

0 commit comments

Comments
 (0)