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

Skip to content

Commit 0c5eb66

Browse files
committed
feat: class, interface, typeAlias, enum, typeParameter
1 parent 9be1776 commit 0c5eb66

File tree

2 files changed

+149
-3
lines changed

2 files changed

+149
-3
lines changed

packages/eslint-plugin/src/rules/naming-convention.ts

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ enum Selectors {
4141
// memberLike
4242
property = 1 << 3,
4343
parameterProperty = 1 << 4,
44-
enumMember = 1 << 5,
45-
method = 1 << 6,
46-
accessor = 1 << 7,
44+
method = 1 << 5,
45+
accessor = 1 << 6,
46+
enumMember = 1 << 7,
4747

4848
// typeLike
4949
class = 1 << 8,
@@ -555,6 +555,85 @@ export default util.createRule<Options, MessageIds>({
555555
},
556556

557557
// #endregion enumMember
558+
559+
// #region class
560+
561+
'ClassDeclaration, ClassExpression'(
562+
node: TSESTree.ClassDeclaration | TSESTree.ClassDeclaration,
563+
): void {
564+
const validator = validators.class;
565+
if (!validator) {
566+
return;
567+
}
568+
569+
const id = node.id;
570+
if (id === null) {
571+
return;
572+
}
573+
574+
const modifiers = new Set<Modifiers>();
575+
if (node.abstract) {
576+
modifiers.add(Modifiers.abstract);
577+
}
578+
579+
validator(id, modifiers);
580+
},
581+
582+
// #endregion class
583+
584+
// #region interface
585+
586+
TSInterfaceDeclaration(node): void {
587+
const validator = validators.interface;
588+
if (!validator) {
589+
return;
590+
}
591+
592+
validator(node.id);
593+
},
594+
595+
// #endregion interface
596+
597+
// #region typeAlias
598+
599+
TSTypeAliasDeclaration(node): void {
600+
const validator = validators.typeAlias;
601+
if (!validator) {
602+
return;
603+
}
604+
605+
validator(node.id);
606+
},
607+
608+
// #endregion typeAlias
609+
610+
// #region enum
611+
612+
TSEnumDeclaration(node): void {
613+
const validator = validators.enum;
614+
if (!validator) {
615+
return;
616+
}
617+
618+
validator(node.id);
619+
},
620+
621+
// #endregion enum
622+
623+
// #region typeParameter
624+
625+
'TSTypeParameterDeclaration > TSTypeParameter'(
626+
node: TSESTree.TSTypeParameter,
627+
): void {
628+
const validator = validators.typeParameter;
629+
if (!validator) {
630+
return;
631+
}
632+
633+
validator(node.name);
634+
},
635+
636+
// #endregion typeParameter
558637
};
559638
},
560639
});

packages/eslint-plugin/tests/rules/naming-convention.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,16 @@ const cases: Cases = [
358358
'interface Ignored { %: string }',
359359
'type Ignored = { %: string }',
360360
'class Ignored { private % = 1 }',
361+
'class Ignored { constructor(private %) {} }',
362+
'class Ignored { private %() {} }',
363+
'const ignored = { %() {} };',
364+
'class Ignored { private get %() {} }',
365+
'enum Ignored { % }',
366+
'abstract class % {}',
367+
'interface % { }',
368+
'type % = { };',
369+
'enum % {}',
370+
'interface Ignored<%> extends Ignored<string> {}',
361371
],
362372
options: {
363373
selector: 'default',
@@ -532,6 +542,63 @@ const cases: Cases = [
532542
},
533543
},
534544
// #endregion enumMember
545+
546+
// #region class
547+
{
548+
code: ['class % {}', 'abstract class % {}', 'const ignored = class % {}'],
549+
options: {
550+
selector: 'class',
551+
},
552+
},
553+
{
554+
code: ['abstract class % {}; class ignoredDueToModifier {}'],
555+
options: {
556+
selector: 'class',
557+
modifiers: ['abstract'],
558+
},
559+
},
560+
// #endregion class
561+
562+
// #region interface
563+
{
564+
code: ['interface % {}'],
565+
options: {
566+
selector: 'interface',
567+
},
568+
},
569+
// #endregion interface
570+
571+
// #region typeAlias
572+
{
573+
code: ['type % = {};', 'type % = 1;'],
574+
options: {
575+
selector: 'typeAlias',
576+
},
577+
},
578+
// #endregion typeAlias
579+
580+
// #region enum
581+
{
582+
code: ['enum % {}'],
583+
options: {
584+
selector: 'enum',
585+
},
586+
},
587+
// #endregion enum
588+
589+
// #region typeParameter
590+
{
591+
code: [
592+
'class Ignored<%> {}',
593+
'function ignored<%>() {}',
594+
'type Ignored<%> = { ignored: % };',
595+
'interface Ignored<%> extends Ignored<string> {}',
596+
],
597+
options: {
598+
selector: 'typeParameter',
599+
},
600+
},
601+
// #endregion typeParameter
535602
];
536603

537604
ruleTester.run('naming-convention', rule, {

0 commit comments

Comments
 (0)