From 3ea09477debec9f1593e4d3857e153570b488f4d Mon Sep 17 00:00:00 2001 From: Taeheon Kim Date: Tue, 1 Mar 2022 22:50:34 +0900 Subject: [PATCH] fix(eslint-plugin): [naming-convention] cover case that requires quotes (#4582) * fix: cover case that requires quotes * test: cover more cases * chore: fix lint style * test: use noFormat for better testing case * nit add comment to test Co-authored-by: Brad Zacher --- .../naming-convention-utils/validator.ts | 15 +++++++++----- .../tests/rules/naming-convention.test.ts | 20 ++++++++++++++++++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/packages/eslint-plugin/src/rules/naming-convention-utils/validator.ts b/packages/eslint-plugin/src/rules/naming-convention-utils/validator.ts index 8e8c8711a59..43550b681c0 100644 --- a/packages/eslint-plugin/src/rules/naming-convention-utils/validator.ts +++ b/packages/eslint-plugin/src/rules/naming-convention-utils/validator.ts @@ -127,7 +127,9 @@ function createValidator( return; } - if (!validatePredefinedFormat(config, name, node, originalName)) { + if ( + !validatePredefinedFormat(config, name, node, originalName, modifiers) + ) { // fail return; } @@ -376,16 +378,19 @@ function createValidator( name: string, node: TSESTree.Identifier | TSESTree.PrivateIdentifier | TSESTree.Literal, originalName: string, + modifiers: Set, ): boolean { const formats = config.format; if (formats === null || formats.length === 0) { return true; } - for (const format of formats) { - const checker = PredefinedFormatToCheckFunction[format]; - if (checker(name)) { - return true; + if (!modifiers.has(Modifiers.requiresQuotes)) { + for (const format of formats) { + const checker = PredefinedFormatToCheckFunction[format]; + if (checker(name)) { + return true; + } } } diff --git a/packages/eslint-plugin/tests/rules/naming-convention.test.ts b/packages/eslint-plugin/tests/rules/naming-convention.test.ts index ef123500880..656dade01d7 100644 --- a/packages/eslint-plugin/tests/rules/naming-convention.test.ts +++ b/packages/eslint-plugin/tests/rules/naming-convention.test.ts @@ -6,7 +6,7 @@ import { Selector, selectorTypeToMessageString, } from '../../src/rules/naming-convention-utils'; -import { getFixturesRootDir, RuleTester } from '../RuleTester'; +import { getFixturesRootDir, noFormat, RuleTester } from '../RuleTester'; const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', @@ -2224,5 +2224,23 @@ ruleTester.run('naming-convention', rule, { ], errors: Array(13).fill({ messageId: 'doesNotMatchFormat' }), }, + { + code: noFormat` + type Foo = { + 'foo Bar': string; + '': string; + '0': string; + 'foo': string; + 'foo-bar': string; + '#foo-bar': string; + }; + + interface Bar { + 'boo-----foo': string; + } + `, + // 6, not 7 because 'foo' is valid + errors: Array(6).fill({ messageId: 'doesNotMatchFormat' }), + }, ], });