/
class-name-casing.ts
99 lines (93 loc) · 2.63 KB
/
class-name-casing.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';
export default util.createRule({
name: 'class-name-casing',
meta: {
type: 'suggestion',
docs: {
description: 'Require PascalCased class and interface names',
tslintRuleName: 'class-name',
category: 'Best Practices',
recommended: 'error',
},
messages: {
notPascalCased: "{{friendlyName}} '{{name}}' must be PascalCased.",
},
schema: [],
},
defaultOptions: [],
create(context) {
/**
* Determine if the identifier name is PascalCased
* @param name The identifier name
*/
function isPascalCase(name: string): boolean {
return /^[A-Z][0-9A-Za-z]*$/.test(name);
}
/**
* Report a class declaration as invalid
* @param decl The declaration
* @param id The name of the declaration
*/
function report(decl: TSESTree.Node, id: TSESTree.Identifier): void {
let friendlyName;
switch (decl.type) {
case AST_NODE_TYPES.ClassDeclaration:
case AST_NODE_TYPES.ClassExpression:
friendlyName = decl.abstract ? 'Abstract class' : 'Class';
break;
case AST_NODE_TYPES.TSInterfaceDeclaration:
friendlyName = 'Interface';
break;
default:
friendlyName = decl.type;
}
context.report({
node: id,
messageId: 'notPascalCased',
data: {
friendlyName,
name: id.name,
},
});
}
return {
'ClassDeclaration, TSInterfaceDeclaration, ClassExpression'(
node:
| TSESTree.ClassDeclaration
| TSESTree.TSInterfaceDeclaration
| TSESTree.ClassExpression,
) {
// class expressions (i.e. export default class {}) are OK
if (node.id && !isPascalCase(node.id.name)) {
report(node, node.id);
}
},
"VariableDeclarator[init.type='ClassExpression']"(
node: TSESTree.VariableDeclarator,
) {
if (
node.id.type === AST_NODE_TYPES.ArrayPattern ||
node.id.type === AST_NODE_TYPES.ObjectPattern
) {
// TODO - handle the BindingPattern case maybe?
/*
// this example makes me barf, but it's valid code
var { bar } = class {
static bar() { return 2 }
}
*/
} else {
const id = node.id;
const nodeInit = node.init as TSESTree.ClassExpression;
if (id && !nodeInit.id && !isPascalCase(id.name)) {
report(nodeInit, id);
}
}
},
};
},
});