/
interface-name-prefix.ts
61 lines (56 loc) · 1.43 KB
/
interface-name-prefix.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
import * as util from '../util';
type Options = ['never' | 'always'];
type MessageIds = 'noPrefix' | 'alwaysPrefix';
export default util.createRule<Options, MessageIds>({
name: 'interface-name-prefix',
meta: {
type: 'suggestion',
docs: {
description: 'Require that interface names be prefixed with `I`',
category: 'Stylistic Issues',
recommended: 'error',
},
messages: {
noPrefix: 'Interface name must not be prefixed with "I".',
alwaysPrefix: 'Interface name must be prefixed with "I".',
},
schema: [
{
enum: ['never', 'always'],
},
],
},
defaultOptions: ['never'],
create(context, [option]) {
const never = option !== 'always';
/**
* Checks if a string is prefixed with "I".
* @param name The string to check
*/
function isPrefixedWithI(name: string): boolean {
if (typeof name !== 'string') {
return false;
}
return /^I[A-Z]/.test(name);
}
return {
TSInterfaceDeclaration(node): void {
if (never) {
if (isPrefixedWithI(node.id.name)) {
context.report({
node: node.id,
messageId: 'noPrefix',
});
}
} else {
if (!isPrefixedWithI(node.id.name)) {
context.report({
node: node.id,
messageId: 'alwaysPrefix',
});
}
}
},
};
},
});