Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(eslint-plugin): [naming-convention] document ignoring quoted properties #2071

Merged
merged 1 commit into from May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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',
},
},
],
},
],
});