-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
interface-name-prefix.ts
161 lines (155 loc) · 4.24 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import * as util from '../util';
type ParsedOptions =
| {
prefixWithI: 'never';
allowedPrefixes: string[];
}
| {
prefixWithI: 'always';
allowUnderscorePrefix: boolean;
};
type Options = [
| 'never'
| 'always'
| {
prefixWithI?: 'never';
allowedPrefixes?: string[];
}
| {
prefixWithI: 'always';
allowUnderscorePrefix?: boolean;
},
];
type MessageIds = 'noPrefix' | 'alwaysPrefix';
/**
* Parses a given value as options.
*/
export function parseOptions([options]: Options): ParsedOptions {
if (options === 'always') {
return { prefixWithI: 'always', allowUnderscorePrefix: false };
}
if (options !== 'never' && options.prefixWithI === 'always') {
return {
prefixWithI: 'always',
allowUnderscorePrefix: !!options.allowUnderscorePrefix,
};
}
return {
prefixWithI: 'never',
allowedPrefixes:
(typeof options === 'object' && options.allowedPrefixes) || [],
};
}
export default util.createRule<Options, MessageIds>({
name: 'interface-name-prefix',
meta: {
type: 'suggestion',
docs: {
description:
'Require that interface names should or should not prefixed with `I`',
category: 'Stylistic Issues',
// this will always be recommended as there's no reason to use this convention
// https://github.com/typescript-eslint/typescript-eslint/issues/374
recommended: 'error',
},
messages: {
noPrefix: 'Interface name must not be prefixed with "I".',
alwaysPrefix: 'Interface name must be prefixed with "I".',
},
schema: [
{
oneOf: [
{
enum: [
// Deprecated, equivalent to: { prefixWithI: 'never', allowedPrefixes: [] }
'never',
// Deprecated, equivalent to: { prefixWithI: 'always', allowUnderscorePrefix: false }
'always',
],
},
{
type: 'object',
properties: {
prefixWithI: {
type: 'string',
enum: ['never'],
},
allowedPrefixes: {
type: 'array',
items: {
type: 'string',
},
},
},
additionalProperties: false,
},
{
type: 'object',
properties: {
prefixWithI: {
type: 'string',
enum: ['always'],
},
allowUnderscorePrefix: {
type: 'boolean',
},
},
required: ['prefixWithI'], // required to select this "oneOf" alternative
additionalProperties: false,
},
],
},
],
},
defaultOptions: [{ prefixWithI: 'never', allowedPrefixes: [] }],
create(context, [options]) {
const parsedOptions = parseOptions([options]);
/**
* Checks if a string is prefixed with "I".
* @param name The string to check
*/
function isPrefixedWithI(name: string): boolean {
return /^I[A-Z]/.test(name);
}
/**
* Checks if a string is prefixed with "I" or "_I".
* @param name The string to check
*/
function isPrefixedWithIOrUnderscoreI(name: string): boolean {
return /^_?I[A-Z]/.test(name);
}
return {
TSInterfaceDeclaration(node): void {
if (parsedOptions.prefixWithI === 'never') {
if (
isPrefixedWithIOrUnderscoreI(node.id.name) &&
!parsedOptions.allowedPrefixes.some(allowedPrefix =>
node.id.name.startsWith(allowedPrefix),
)
) {
context.report({
node: node.id,
messageId: 'noPrefix',
});
}
} else {
if (parsedOptions.allowUnderscorePrefix) {
if (!isPrefixedWithIOrUnderscoreI(node.id.name)) {
context.report({
node: node.id,
messageId: 'alwaysPrefix',
});
}
} else {
if (!isPrefixedWithI(node.id.name)) {
context.report({
node: node.id,
messageId: 'alwaysPrefix',
});
}
}
}
},
};
},
});