From 116da9ce23d980461c317dba64d2107d681f5c0d Mon Sep 17 00:00:00 2001 From: lonyele Date: Tue, 22 Feb 2022 09:03:28 +0900 Subject: [PATCH 1/5] fix: cover case that requires quotes --- .../rules/naming-convention-utils/validator.ts | 15 ++++++++++----- .../tests/rules/naming-convention.test.ts | 12 ++++++++++++ 2 files changed, 22 insertions(+), 5 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..f974690e3fa 100644 --- a/packages/eslint-plugin/tests/rules/naming-convention.test.ts +++ b/packages/eslint-plugin/tests/rules/naming-convention.test.ts @@ -2224,5 +2224,17 @@ ruleTester.run('naming-convention', rule, { ], errors: Array(13).fill({ messageId: 'doesNotMatchFormat' }), }, + { + code: ` + type Foo = { + 'foo Bar': string; + }; + + interface Bar { + 'boo-----foo': string; + } + `, + errors: Array(2).fill({ messageId: 'doesNotMatchFormat' }), + }, ], }); From db21512cb9716c967df2884c2ef2e5f76ccde856 Mon Sep 17 00:00:00 2001 From: lonyele Date: Sun, 27 Feb 2022 14:05:08 +0900 Subject: [PATCH 2/5] test: cover more cases --- .../eslint-plugin/tests/rules/naming-convention.test.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/tests/rules/naming-convention.test.ts b/packages/eslint-plugin/tests/rules/naming-convention.test.ts index f974690e3fa..d5f003d5dcc 100644 --- a/packages/eslint-plugin/tests/rules/naming-convention.test.ts +++ b/packages/eslint-plugin/tests/rules/naming-convention.test.ts @@ -2228,13 +2228,20 @@ ruleTester.run('naming-convention', rule, { code: ` type Foo = { 'foo Bar': string; + '': string; + '0': string; + 'foo': string; + 'foo-bar': string; + '#foo-bar': string; }; interface Bar { 'boo-----foo': string; } + + `, - errors: Array(2).fill({ messageId: 'doesNotMatchFormat' }), + errors: Array(6).fill({ messageId: 'doesNotMatchFormat' }), }, ], }); From 6da7dd0d4b3fd89df08b98b99abbe452166c0d1f Mon Sep 17 00:00:00 2001 From: lonyele Date: Sun, 27 Feb 2022 14:32:35 +0900 Subject: [PATCH 3/5] chore: fix lint style --- .../eslint-plugin/tests/rules/naming-convention.test.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/naming-convention.test.ts b/packages/eslint-plugin/tests/rules/naming-convention.test.ts index d5f003d5dcc..37bbbfd2076 100644 --- a/packages/eslint-plugin/tests/rules/naming-convention.test.ts +++ b/packages/eslint-plugin/tests/rules/naming-convention.test.ts @@ -2230,16 +2230,14 @@ ruleTester.run('naming-convention', rule, { 'foo Bar': string; '': string; '0': string; - 'foo': string; + foo: string; 'foo-bar': string; '#foo-bar': string; }; - + interface Bar { 'boo-----foo': string; } - - `, errors: Array(6).fill({ messageId: 'doesNotMatchFormat' }), }, From 9d12d86065db2e8b0e94c81aeb58e5087b431a8a Mon Sep 17 00:00:00 2001 From: lonyele Date: Tue, 1 Mar 2022 13:26:26 +0900 Subject: [PATCH 4/5] test: use noFormat for better testing case --- .../eslint-plugin/tests/rules/naming-convention.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/naming-convention.test.ts b/packages/eslint-plugin/tests/rules/naming-convention.test.ts index 37bbbfd2076..078171a97b8 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', @@ -2225,16 +2225,16 @@ ruleTester.run('naming-convention', rule, { errors: Array(13).fill({ messageId: 'doesNotMatchFormat' }), }, { - code: ` + code: noFormat` type Foo = { 'foo Bar': string; '': string; '0': string; - foo: string; + 'foo': string; 'foo-bar': string; '#foo-bar': string; }; - + interface Bar { 'boo-----foo': string; } From 524960324e95a7f1246552cfc1cf04fc946f1397 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Mon, 28 Feb 2022 20:32:30 -0800 Subject: [PATCH 5/5] nit add comment to test --- packages/eslint-plugin/tests/rules/naming-convention.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/eslint-plugin/tests/rules/naming-convention.test.ts b/packages/eslint-plugin/tests/rules/naming-convention.test.ts index 078171a97b8..656dade01d7 100644 --- a/packages/eslint-plugin/tests/rules/naming-convention.test.ts +++ b/packages/eslint-plugin/tests/rules/naming-convention.test.ts @@ -2239,6 +2239,7 @@ ruleTester.run('naming-convention', rule, { 'boo-----foo': string; } `, + // 6, not 7 because 'foo' is valid errors: Array(6).fill({ messageId: 'doesNotMatchFormat' }), }, ],