Skip to content

Commit

Permalink
fix(eslint-plugin): add TSPropertySignature with TSFunctionType annot…
Browse files Browse the repository at this point in the history
…ation to typeMethod selector (#6645)

* fix(eslint-plugin): add TSPropertySignature with TSFunctionType annotation to typeMethod selector

* add a test for typeProperty readonly modifier

---------

Co-authored-by: Michał Kozłowski <michal.kozlowski@start-up.house>
  • Loading branch information
kozlovvski and Michał Kozłowski committed Mar 19, 2023
1 parent 59a5447 commit 3fc5c63
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 14 deletions.
34 changes: 20 additions & 14 deletions packages/eslint-plugin/src/rules/naming-convention.ts
Expand Up @@ -394,20 +394,21 @@ export default util.createRule<Options, MessageIds>({
},
},

'TSPropertySignature[computed = false]': {
validator: validators.typeProperty,
handler: (
node: TSESTree.TSPropertySignatureNonComputedName,
validator,
): void => {
const modifiers = new Set<Modifiers>([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>([Modifiers.public]);
if (node.readonly) {
modifiers.add(Modifiers.readonly);
}

handleMember(validator, node, modifiers);
handleMember(validator, node, modifiers);
},
},
},

// #endregion property

Expand Down Expand Up @@ -460,10 +461,15 @@ export default util.createRule<Options, MessageIds>({
},
},

'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>([Modifiers.public]);
Expand Down
Expand Up @@ -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',
Expand Down
Expand Up @@ -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;
Expand Down

0 comments on commit 3fc5c63

Please sign in to comment.