-
Notifications
You must be signed in to change notification settings - Fork 1
/
variable-names.js
135 lines (113 loc) · 3.58 KB
/
variable-names.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
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "enforce that variables defined within a method start with '_', except for parameters",
category: "Stylistic Issues",
recommended: false
},
schema: [{
type: "object",
properties: {
variableExceptions: {
type: "array",
items: { type: "string" },
uniqueItems: true
}
},
additionalProperties: false
}]
},
create(context) {
const options = context.options[0] || {};
const variableExceptions = options.variableExceptions || [];
function checkDeclaration(node) {
if (!node) {
return;
}
switch(node.type) {
// In case of a variable declaration, it checks if the variable name \
// starts with "_" or is an exception
case "VariableDeclaration": {
node.declarations.forEach((declaration) => {
if (declaration.id.name && !declaration.id.name.startsWith("_") && !variableExceptions.includes(declaration.id.name)) {
context.report({
node: declaration,
message: `Variables defined within a method should start with "_" ({${declaration.id.name}})`,
});
}
});
break;
}
// BlockStatement and Program nodes contain lists of statements \
// (node.body), so we check each statement
case "BlockStatement":
case "Program": {
node.body.forEach(checkDeclaration);
break;
}
// IfStatement nodes have a consequent (then), and an \
// optional alternate (else) to be checked
case "IfStatement": {
checkDeclaration(node.consequent);
if (node.alternate) {
checkDeclaration(node.alternate);
}
break;
}
// Loops nodes contain a body with statements to be checked
case "ForStatement":
case "WhileStatement":
case "DoWhileStatement":
case "ForInStatement":
case "ForOfStatement": {
if (node.body) {
checkDeclaration(node.body);
}
// Check the loop condition itself
checkDeclaration(node.left);
break;
}
// SwitchStatement nodes contain an array of SwitchCase nodes that \
// represent each case in the switch statement
case "SwitchStatement": {
node.cases.forEach(checkDeclaration);
break;
}
// SwitchCase nodes contain a consequent which is an array of \
// Statements to be checked
case "SwitchCase": {
node.consequent.forEach(checkDeclaration);
break;
}
default: {
break;
}
}
}
return {
// For each property in an object, if it's a function / arrow function, \
// check its body (usefull for Vue.js components, in which methods and \
// computed are defined as properties of a parent object)
Property(node) {
if (node.value.type === "FunctionExpression" ||
node.value.type === "ArrowFunctionExpression") {
checkDeclaration(node);
}
},
MethodDefinition(node) {
checkDeclaration(node.value.body);
},
ArrowFunctionExpression(node) {
if (node.body.type === "BlockStatement") {
checkDeclaration(node.body);
}
},
FunctionExpression(node) {
if (node.body.type === "BlockStatement") {
checkDeclaration(node.body);
}
}
};
}
};