/
no-keyword-prefix.js
202 lines (177 loc) · 4.75 KB
/
no-keyword-prefix.js
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
'use strict';
const isShorthandPropertyAssignmentPatternLeft = require('./utils/is-shorthand-property-assignment-pattern-left.js');
const MESSAGE_ID = 'noKeywordPrefix';
const messages = {
[MESSAGE_ID]: 'Do not prefix identifiers with keyword `{{keyword}}`.',
};
const prepareOptions = ({
disallowedPrefixes,
checkProperties = true,
onlyCamelCase = true,
} = {}) => ({
disallowedPrefixes: (disallowedPrefixes || [
'new',
'class',
]),
checkProperties,
onlyCamelCase,
});
function findKeywordPrefix(name, options) {
return options.disallowedPrefixes.find(keyword => {
const suffix = options.onlyCamelCase ? '[A-Z]' : '.';
const regex = new RegExp(`^${keyword}${suffix}`);
return name.match(regex);
});
}
function checkMemberExpression(report, node, options) {
const {name, parent} = node;
const keyword = findKeywordPrefix(name, options);
const effectiveParent = parent.type === 'MemberExpression' ? parent.parent : parent;
if (!options.checkProperties) {
return;
}
if (parent.object.type === 'Identifier' && parent.object.name === name && Boolean(keyword)) {
report(node, keyword);
} else if (
effectiveParent.type === 'AssignmentExpression'
&& Boolean(keyword)
&& (effectiveParent.right.type !== 'MemberExpression' || effectiveParent.left.type === 'MemberExpression')
&& effectiveParent.left.property.name === name
) {
report(node, keyword);
}
}
function checkObjectPattern(report, node, options) {
const {name, parent} = node;
const keyword = findKeywordPrefix(name, options);
/* istanbul ignore next: Can't find a case to cover this line */
if (parent.shorthand && parent.value.left && Boolean(keyword)) {
report(node, keyword);
}
const assignmentKeyEqualsValue = parent.key.name === parent.value.name;
if (Boolean(keyword) && parent.computed) {
report(node, keyword);
}
// Prevent checking righthand side of destructured object
if (parent.key === node && parent.value !== node) {
return true;
}
const valueIsInvalid = parent.value.name && Boolean(keyword);
// Ignore destructuring if the option is set, unless a new identifier is created
if (valueIsInvalid && !assignmentKeyEqualsValue) {
report(node, keyword);
}
return false;
}
// Core logic copied from:
// https://github.com/eslint/eslint/blob/master/lib/rules/camelcase.js
const create = context => {
const options = prepareOptions(context.options[0]);
// Contains reported nodes to avoid reporting twice on destructuring with shorthand notation
const reported = [];
const ALLOWED_PARENT_TYPES = new Set(['CallExpression', 'NewExpression']);
function report(node, keyword) {
if (!reported.includes(node)) {
reported.push(node);
context.report({
node,
messageId: MESSAGE_ID,
data: {
name: node.name,
keyword,
},
});
}
}
return {
Identifier: node => {
const {name, parent} = node;
const keyword = findKeywordPrefix(name, options);
const effectiveParent = parent.type === 'MemberExpression' ? parent.parent : parent;
if (parent.type === 'MemberExpression') {
checkMemberExpression(report, node, options);
} else if (
parent.type === 'Property'
|| parent.type === 'AssignmentPattern'
) {
if (parent.parent && parent.parent.type === 'ObjectPattern') {
const finished = checkObjectPattern(report, node, options);
if (finished) {
return;
}
}
if (
!options.checkProperties
) {
return;
}
// Don't check right hand side of AssignmentExpression to prevent duplicate warnings
if (
Boolean(keyword)
&& !ALLOWED_PARENT_TYPES.has(effectiveParent.type)
&& !(parent.right === node)
&& !isShorthandPropertyAssignmentPatternLeft(node)
) {
report(node, keyword);
}
// Check if it's an import specifier
} else if (
[
'ImportSpecifier',
'ImportNamespaceSpecifier',
'ImportDefaultSpecifier',
].includes(parent.type)
) {
// Report only if the local imported identifier is invalid
if (
Boolean(keyword)
&& parent.local
&& parent.local.name === name
) {
report(node, keyword);
}
// Report anything that is invalid that isn't a CallExpression
} else if (
Boolean(keyword)
&& !ALLOWED_PARENT_TYPES.has(effectiveParent.type)
) {
report(node, keyword);
}
},
};
};
const schema = [
{
type: 'object',
additionalProperties: false,
properties: {
disallowedPrefixes: {
type: 'array',
items: [
{
type: 'string',
},
],
minItems: 0,
uniqueItems: true,
},
checkProperties: {
type: 'boolean',
},
onlyCamelCase: {
type: 'boolean',
},
},
},
];
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Disallow identifiers starting with `new` or `class`.',
},
schema,
messages,
},
};