Skip to content

Commit

Permalink
docs(eslint-plugin): [naming-convention] document ignoring quoted pro…
Browse files Browse the repository at this point in the history
…perties (#2071)
  • Loading branch information
bradzacher committed May 22, 2020
1 parent 071e5a0 commit 1cb1cb5
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
47 changes: 47 additions & 0 deletions packages/eslint-plugin/docs/rules/naming-convention.md
Expand Up @@ -307,6 +307,8 @@ Group Selectors are provided for convenience, and essentially bundle up sets of

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

This allows you to emulate the old `generic-type-naming` rule.

```json
{
"@typescript-eslint/naming-convention": [
Expand All @@ -322,6 +324,8 @@ Group Selectors are provided for convenience, and essentially bundle up sets of

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

This allows you to emulate the old `interface-name-prefix` rule.

```json
{
"@typescript-eslint/naming-convention": [
Expand All @@ -339,6 +343,49 @@ Group Selectors are provided for convenience, and essentially bundle up sets of
}
```

### Ignore properties that require quotes

Sometimes you have to use a quoted name that breaks the convention (for example, HTTP headers).
If this is a common thing in your codebase, then you can use the `filter` option in one of two ways:

You can use the `filter` option to ignore specific names only:

```jsonc
{
"@typescript-eslint/naming-convention": [
"error",
{
"selector": "property",
"format": ["strictCamelCase"],
"filter": {
// you can expand this regex to add more allowed names
"regex": "^(Property-Name-One|Property-Name-Two)$",
"match": false
}
}
]
}
```

You can use the `filter` option to ignore names that require quoting:

```jsonc
{
"@typescript-eslint/naming-convention": [
"error",
{
"selector": "property",
"format": ["strictCamelCase"],
"filter": {
// you can expand this regex as you find more cases that require quoting that you want to allow
"regex": "[- ]",
"match": false
}
}
]
}
```

### Enforce the codebase follows ESLint's `camelcase` conventions

```json
Expand Down
63 changes: 63 additions & 0 deletions packages/eslint-plugin/tests/rules/naming-convention.test.ts
Expand Up @@ -758,6 +758,40 @@ ruleTester.run('naming-convention', rule, {
},
],
},
{
code: `
const foo = {
'Property-Name': 'asdf',
};
`,
options: [
{
format: ['strictCamelCase'],
selector: 'default',
filter: {
regex: /-/.source,
match: false,
},
},
],
},
{
code: `
const foo = {
'Property-Name': 'asdf',
};
`,
options: [
{
format: ['strictCamelCase'],
selector: 'default',
filter: {
regex: /^(Property-Name)$/.source,
match: false,
},
},
],
},
],
invalid: [
...createInvalidTestCases(cases),
Expand Down Expand Up @@ -965,5 +999,34 @@ ruleTester.run('naming-convention', rule, {
},
],
},
{
code: `
const foo = {
'Property Name': 'asdf',
};
`,
options: [
{
format: ['strictCamelCase'],
selector: 'default',
filter: {
regex: /-/.source,
match: false,
},
},
],
errors: [
{
line: 3,
messageId: 'doesNotMatchFormat',
data: {
// eslint-disable-next-line @typescript-eslint/internal/prefer-ast-types-enum
type: 'Property',
name: 'Property Name',
formats: 'strictCamelCase',
},
},
],
},
],
});

0 comments on commit 1cb1cb5

Please sign in to comment.