-
Notifications
You must be signed in to change notification settings - Fork 1
/
constructor-variables.js
48 lines (45 loc) 路 1.53 KB
/
constructor-variables.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
module.exports = {
meta: {
type: "problem",
docs: {
description: "Ensure all class properties in the constructor start with '_', except for specified exceptions",
category: "Stylistic Issues",
},
schema: [
{
type: "object",
properties: {
filenameExceptions: {
type: "array",
items: { type: "string" },
},
variableExceptions: {
type: "array",
items: { type: "string" },
},
},
additionalProperties: false,
},
],
},
create(context) {
const options = context.options[0] || {};
const filename = context.getFilename();
const filenameExceptions = options.filenameExceptions || [];
const variableExceptions = options.variableExceptions || [];
const isFilenameException = filenameExceptions.some((exception) => filename.endsWith(exception));
return {
"MethodDefinition[kind='constructor'] > FunctionExpression > BlockStatement > ExpressionStatement > AssignmentExpression": function(node) {
if(node.left.type === "MemberExpression" && node.left.object.type === "ThisExpression") {
const varName = node.left.property.name;
if (!varName.startsWith("_") && !isFilenameException && !variableExceptions.includes(varName)) {
context.report({
node,
message: "Class properties in the constructor should start with '_', except for specified exceptions",
});
}
}
},
};
},
};