From f0ab627c08289103b566ae6d4a64e5a725110840 Mon Sep 17 00:00:00 2001 From: Ville Lahdenvuo Date: Tue, 9 Jun 2020 09:51:27 +0200 Subject: [PATCH 1/3] docs(eslint-plugin): note about prefix trimming Add a note about prefix being trimmed before format is validated to make it more noticeable. --- packages/eslint-plugin/docs/rules/naming-convention.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/eslint-plugin/docs/rules/naming-convention.md b/packages/eslint-plugin/docs/rules/naming-convention.md index 43874c6b0089..ec21837594f2 100644 --- a/packages/eslint-plugin/docs/rules/naming-convention.md +++ b/packages/eslint-plugin/docs/rules/naming-convention.md @@ -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). @@ -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": [ From 22256ab4a65d1660557243c89b42fe5a74b2a53c Mon Sep 17 00:00:00 2001 From: Ville Lahdenvuo Date: Tue, 9 Jun 2020 16:15:54 +0200 Subject: [PATCH 2/3] fix(eslint-plugin): [naming-convention] better error message when using prefix/suffix option --- packages/eslint-plugin/src/rules/naming-convention.ts | 11 +++++++++-- .../tests/rules/naming-convention.test.ts | 6 +++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin/src/rules/naming-convention.ts b/packages/eslint-plugin/src/rules/naming-convention.ts index d5ea49463bd3..e3e8631dbdbc 100644 --- a/packages/eslint-plugin/src/rules/naming-convention.ts +++ b/packages/eslint-plugin/src/rules/naming-convention.ts @@ -12,7 +12,8 @@ type MessageIds = | 'missingUnderscore' | 'missingAffix' | 'satisfyCustom' - | 'doesNotMatchFormat'; + | 'doesNotMatchFormat' + | 'doesNotMatchFormatTrimmed'; // #region Options Type Config @@ -355,6 +356,8 @@ export default util.createRule({ '{{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, }, @@ -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; }): Record { return { type: selectorTypeToMessageString(type), name: originalName, + processedName, position, affixes: affixes?.join(', '), formats: formats?.map(f => PredefinedFormats[f]).join(', '), @@ -1052,9 +1058,10 @@ function createValidator( context.report({ node, - messageId: 'doesNotMatchFormat', + messageId: originalName === name ? 'doesNotMatchFormat' : 'doesNotMatchFormatTrimmed', data: formatReportData({ originalName, + processedName: name, formats, }), }); diff --git a/packages/eslint-plugin/tests/rules/naming-convention.test.ts b/packages/eslint-plugin/tests/rules/naming-convention.test.ts index 13f67f6c9887..e907f26e8a66 100644 --- a/packages/eslint-plugin/tests/rules/naming-convention.test.ts +++ b/packages/eslint-plugin/tests/rules/naming-convention.test.ts @@ -864,7 +864,7 @@ ruleTester.run('naming-convention', rule, { }, ], parserOptions, - errors: Array(16).fill({ messageId: 'doesNotMatchFormat' }), + errors: Array(16).fill({ messageId: 'doesNotMatchFormatTrimmed' }), }, { code: ` @@ -886,7 +886,7 @@ ruleTester.run('naming-convention', rule, { }, ], parserOptions, - errors: Array(4).fill({ messageId: 'doesNotMatchFormat' }), + errors: Array(4).fill({ messageId: 'doesNotMatchFormatTrimmed' }), }, { code: ` @@ -918,7 +918,7 @@ ruleTester.run('naming-convention', rule, { }, ], parserOptions, - errors: Array(8).fill({ messageId: 'doesNotMatchFormat' }), + errors: Array(8).fill({ messageId: 'doesNotMatchFormatTrimmed' }), }, { code: ` From bcc27d3515291be4f30d0415835a6dc025a6190b Mon Sep 17 00:00:00 2001 From: Ville Lahdenvuo Date: Tue, 9 Jun 2020 16:50:33 +0200 Subject: [PATCH 3/3] chore: format --- packages/eslint-plugin/src/rules/naming-convention.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/naming-convention.ts b/packages/eslint-plugin/src/rules/naming-convention.ts index e3e8631dbdbc..bdbedc625ea1 100644 --- a/packages/eslint-plugin/src/rules/naming-convention.ts +++ b/packages/eslint-plugin/src/rules/naming-convention.ts @@ -1058,7 +1058,10 @@ function createValidator( context.report({ node, - messageId: originalName === name ? 'doesNotMatchFormat' : 'doesNotMatchFormatTrimmed', + messageId: + originalName === name + ? 'doesNotMatchFormat' + : 'doesNotMatchFormatTrimmed', data: formatReportData({ originalName, processedName: name,