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

feat(eslint-plugin): [naming-convention] better error message and docs for prefix/suffix #2195

Merged
merged 3 commits into from Jun 9, 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
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