Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(eslint-plugin): [naming-convention] better error message and doc…
…s for prefix/suffix (#2195)
  • Loading branch information
villelahdenvuo committed Jun 9, 2020
1 parent f80c833 commit a2ffe55
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 4 additions & 0 deletions packages/eslint-plugin/docs/rules/naming-convention.md
Expand Up @@ -150,6 +150,8 @@ The `prefix` / `suffix` options control which prefix/suffix strings must exist f

If these are provided, the identifier must start with one of the provided values. For example, if you provide `{ prefix: ['IFace', 'Class', 'Type'] }`, then the following names are valid: `IFaceFoo`, `ClassBar`, `TypeBaz`, but the name `Bang` is not valid, as it contains none of the prefixes.

**Note:** As [documented above](#format-options), the prefix is trimmed before format is validated, therefore PascalCase must be used to allow variables such as `isEnabled` using the prefix `is`.

### Selector Options

- `selector` (see "Allowed Selectors, Modifiers and Types" below).
Expand Down Expand Up @@ -277,6 +279,8 @@ Group Selectors are provided for convenience, and essentially bundle up sets of

### Enforce that boolean variables are prefixed with an allowed verb

**Note:** As [documented above](#format-options), the prefix is trimmed before format is validated, thus PascalCase must be used to allow variables such as `isEnabled`.

```json
{
"@typescript-eslint/naming-convention": [
Expand Down
14 changes: 12 additions & 2 deletions packages/eslint-plugin/src/rules/naming-convention.ts
Expand Up @@ -12,7 +12,8 @@ type MessageIds =
| 'missingUnderscore'
| 'missingAffix'
| 'satisfyCustom'
| 'doesNotMatchFormat';
| 'doesNotMatchFormat'
| 'doesNotMatchFormatTrimmed';

// #region Options Type Config

Expand Down Expand Up @@ -355,6 +356,8 @@ export default util.createRule<Options, MessageIds>({
'{{type}} name `{{name}}` must {{regexMatch}} the RegExp: {{regex}}',
doesNotMatchFormat:
'{{type}} name `{{name}}` must match one of the following formats: {{formats}}',
doesNotMatchFormatTrimmed:
'{{type}} name `{{name}}` trimmed as `{{processedName}}` must match one of the following formats: {{formats}}',
},
schema: SCHEMA,
},
Expand Down Expand Up @@ -869,18 +872,21 @@ function createValidator(
affixes,
formats,
originalName,
processedName,
position,
custom,
}: {
affixes?: string[];
formats?: PredefinedFormats[];
originalName: string;
processedName?: string;
position?: 'leading' | 'trailing' | 'prefix' | 'suffix';
custom?: NonNullable<NormalizedSelector['custom']>;
}): Record<string, unknown> {
return {
type: selectorTypeToMessageString(type),
name: originalName,
processedName,
position,
affixes: affixes?.join(', '),
formats: formats?.map(f => PredefinedFormats[f]).join(', '),
Expand Down Expand Up @@ -1052,9 +1058,13 @@ function createValidator(

context.report({
node,
messageId: 'doesNotMatchFormat',
messageId:
originalName === name
? 'doesNotMatchFormat'
: 'doesNotMatchFormatTrimmed',
data: formatReportData({
originalName,
processedName: name,
formats,
}),
});
Expand Down
6 changes: 3 additions & 3 deletions packages/eslint-plugin/tests/rules/naming-convention.test.ts
Expand Up @@ -864,7 +864,7 @@ ruleTester.run('naming-convention', rule, {
},
],
parserOptions,
errors: Array(16).fill({ messageId: 'doesNotMatchFormat' }),
errors: Array(16).fill({ messageId: 'doesNotMatchFormatTrimmed' }),
},
{
code: `
Expand All @@ -886,7 +886,7 @@ ruleTester.run('naming-convention', rule, {
},
],
parserOptions,
errors: Array(4).fill({ messageId: 'doesNotMatchFormat' }),
errors: Array(4).fill({ messageId: 'doesNotMatchFormatTrimmed' }),
},
{
code: `
Expand Down Expand Up @@ -918,7 +918,7 @@ ruleTester.run('naming-convention', rule, {
},
],
parserOptions,
errors: Array(8).fill({ messageId: 'doesNotMatchFormat' }),
errors: Array(8).fill({ messageId: 'doesNotMatchFormatTrimmed' }),
},
{
code: `
Expand Down

0 comments on commit a2ffe55

Please sign in to comment.