From 3fc5c63f87bfd9d95f7e51fddc7ef16a6c3c5662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Koz=C5=82owski?= Date: Sun, 19 Mar 2023 14:36:26 +0100 Subject: [PATCH] fix(eslint-plugin): add TSPropertySignature with TSFunctionType annotation to typeMethod selector (#6645) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(eslint-plugin): add TSPropertySignature with TSFunctionType annotation to typeMethod selector * add a test for typeProperty readonly modifier --------- Co-authored-by: Michał Kozłowski --- .../src/rules/naming-convention.ts | 34 +++++++++------- .../naming-convention/cases/method.test.ts | 4 ++ .../naming-convention.test.ts | 40 +++++++++++++++++++ 3 files changed, 64 insertions(+), 14 deletions(-) diff --git a/packages/eslint-plugin/src/rules/naming-convention.ts b/packages/eslint-plugin/src/rules/naming-convention.ts index f5a59614851..1c6c0ae31d1 100644 --- a/packages/eslint-plugin/src/rules/naming-convention.ts +++ b/packages/eslint-plugin/src/rules/naming-convention.ts @@ -394,20 +394,21 @@ export default util.createRule({ }, }, - 'TSPropertySignature[computed = false]': { - validator: validators.typeProperty, - handler: ( - node: TSESTree.TSPropertySignatureNonComputedName, - validator, - ): void => { - const modifiers = new Set([Modifiers.public]); - if (node.readonly) { - modifiers.add(Modifiers.readonly); - } + 'TSPropertySignature[computed = false][typeAnnotation.typeAnnotation.type != "TSFunctionType"]': + { + validator: validators.typeProperty, + handler: ( + node: TSESTree.TSPropertySignatureNonComputedName, + validator, + ): void => { + const modifiers = new Set([Modifiers.public]); + if (node.readonly) { + modifiers.add(Modifiers.readonly); + } - handleMember(validator, node, modifiers); + handleMember(validator, node, modifiers); + }, }, - }, // #endregion property @@ -460,10 +461,15 @@ export default util.createRule({ }, }, - 'TSMethodSignature[computed = false]': { + [[ + 'TSMethodSignature[computed = false]', + 'TSPropertySignature[computed = false][typeAnnotation.typeAnnotation.type = "TSFunctionType"]', + ].join(', ')]: { validator: validators.typeMethod, handler: ( - node: TSESTree.TSMethodSignatureNonComputedName, + node: + | TSESTree.TSMethodSignatureNonComputedName + | TSESTree.TSPropertySignatureNonComputedName, validator, ): void => { const modifiers = new Set([Modifiers.public]); diff --git a/packages/eslint-plugin/tests/rules/naming-convention/cases/method.test.ts b/packages/eslint-plugin/tests/rules/naming-convention/cases/method.test.ts index b4973f23297..29e9373d3b2 100644 --- a/packages/eslint-plugin/tests/rules/naming-convention/cases/method.test.ts +++ b/packages/eslint-plugin/tests/rules/naming-convention/cases/method.test.ts @@ -32,8 +32,12 @@ createTestCases([ code: [ 'interface Ignored { %(): string }', 'interface Ignored { "%"(): string }', + 'interface Ignored { %: () => string }', + 'interface Ignored { "%": () => string }', 'type Ignored = { %(): string }', 'type Ignored = { "%"(): string }', + 'type Ignored = { %: () => string }', + 'type Ignored = { "%": () => string }', ], options: { selector: 'typeMethod', diff --git a/packages/eslint-plugin/tests/rules/naming-convention/naming-convention.test.ts b/packages/eslint-plugin/tests/rules/naming-convention/naming-convention.test.ts index b55ce321f00..df222f2dcb3 100644 --- a/packages/eslint-plugin/tests/rules/naming-convention/naming-convention.test.ts +++ b/packages/eslint-plugin/tests/rules/naming-convention/naming-convention.test.ts @@ -313,6 +313,46 @@ ruleTester.run('naming-convention', rule, { { selector: 'variable', format: ['camelCase'] }, ], }, + // treat properties with function expressions as typeMethod + { + code: ` + interface SOME_INTERFACE { + SomeMethod: () => void; + + some_property: string; + } + `, + options: [ + { + selector: 'default', + format: ['UPPER_CASE'], + }, + { + selector: 'typeMethod', + format: ['PascalCase'], + }, + { + selector: 'typeProperty', + format: ['snake_case'], + }, + ], + }, + { + code: ` + type Ignored = { + ignored_due_to_modifiers: string; + readonly FOO: string; + }; + `, + parserOptions, + options: [ + { + selector: 'typeProperty', + modifiers: ['readonly'], + format: ['UPPER_CASE'], + }, + ], + }, { code: ` const camelCaseVar = 1;