Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions packages/eslint-plugin/src/rules/naming-convention.ts
Original file line number Diff line number Diff line change
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"]':
Copy link
Contributor Author

@kozlovvski kozlovvski Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if this typeAnnotation.typeAnnotation.type part should be moved to some variable. It is later used in L466 in typeMethod selector

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know, we've been leaning away from using these fancier queries, and instead towards simpler, more readable ones. eslint-community/eslint-plugin-eslint-plugin#339

Not requesting changes here as we haven't taken action on that yet & this file already has a lot of complex queries. Just food for thought. 😄

{
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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