Skip to content

Commit

Permalink
feat: class, interface, typeAlias, enum, typeParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Dec 11, 2019
1 parent 9be1776 commit 0c5eb66
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 3 deletions.
85 changes: 82 additions & 3 deletions packages/eslint-plugin/src/rules/naming-convention.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ enum Selectors {
// memberLike
property = 1 << 3,
parameterProperty = 1 << 4,
enumMember = 1 << 5,
method = 1 << 6,
accessor = 1 << 7,
method = 1 << 5,
accessor = 1 << 6,
enumMember = 1 << 7,

// typeLike
class = 1 << 8,
Expand Down Expand Up @@ -555,6 +555,85 @@ export default util.createRule<Options, MessageIds>({
},

// #endregion enumMember

// #region class

'ClassDeclaration, ClassExpression'(
node: TSESTree.ClassDeclaration | TSESTree.ClassDeclaration,
): void {
const validator = validators.class;
if (!validator) {
return;
}

const id = node.id;
if (id === null) {
return;
}

const modifiers = new Set<Modifiers>();
if (node.abstract) {
modifiers.add(Modifiers.abstract);
}

validator(id, modifiers);
},

// #endregion class

// #region interface

TSInterfaceDeclaration(node): void {
const validator = validators.interface;
if (!validator) {
return;
}

validator(node.id);
},

// #endregion interface

// #region typeAlias

TSTypeAliasDeclaration(node): void {
const validator = validators.typeAlias;
if (!validator) {
return;
}

validator(node.id);
},

// #endregion typeAlias

// #region enum

TSEnumDeclaration(node): void {
const validator = validators.enum;
if (!validator) {
return;
}

validator(node.id);
},

// #endregion enum

// #region typeParameter

'TSTypeParameterDeclaration > TSTypeParameter'(
node: TSESTree.TSTypeParameter,
): void {
const validator = validators.typeParameter;
if (!validator) {
return;
}

validator(node.name);
},

// #endregion typeParameter
};
},
});
Expand Down
67 changes: 67 additions & 0 deletions packages/eslint-plugin/tests/rules/naming-convention.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,16 @@ const cases: Cases = [
'interface Ignored { %: string }',
'type Ignored = { %: string }',
'class Ignored { private % = 1 }',
'class Ignored { constructor(private %) {} }',
'class Ignored { private %() {} }',
'const ignored = { %() {} };',
'class Ignored { private get %() {} }',
'enum Ignored { % }',
'abstract class % {}',
'interface % { }',
'type % = { };',
'enum % {}',
'interface Ignored<%> extends Ignored<string> {}',
],
options: {
selector: 'default',
Expand Down Expand Up @@ -532,6 +542,63 @@ const cases: Cases = [
},
},
// #endregion enumMember

// #region class
{
code: ['class % {}', 'abstract class % {}', 'const ignored = class % {}'],
options: {
selector: 'class',
},
},
{
code: ['abstract class % {}; class ignoredDueToModifier {}'],
options: {
selector: 'class',
modifiers: ['abstract'],
},
},
// #endregion class

// #region interface
{
code: ['interface % {}'],
options: {
selector: 'interface',
},
},
// #endregion interface

// #region typeAlias
{
code: ['type % = {};', 'type % = 1;'],
options: {
selector: 'typeAlias',
},
},
// #endregion typeAlias

// #region enum
{
code: ['enum % {}'],
options: {
selector: 'enum',
},
},
// #endregion enum

// #region typeParameter
{
code: [
'class Ignored<%> {}',
'function ignored<%>() {}',
'type Ignored<%> = { ignored: % };',
'interface Ignored<%> extends Ignored<string> {}',
],
options: {
selector: 'typeParameter',
},
},
// #endregion typeParameter
];

ruleTester.run('naming-convention', rule, {
Expand Down

0 comments on commit 0c5eb66

Please sign in to comment.