no-unmodified-loop-condition.js
11.6 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/**
* @fileoverview Rule to disallow use of unmodified expressions in loop conditions
* @author Toru Nagashima
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const Traverser = require("../shared/traverser"),
astUtils = require("./utils/ast-utils");
//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
const SENTINEL_PATTERN = /(?:(?:Call|Class|Function|Member|New|Yield)Expression|Statement|Declaration)$/u;
const LOOP_PATTERN = /^(?:DoWhile|For|While)Statement$/u; // for-in/of statements don't have `test` property.
const GROUP_PATTERN = /^(?:BinaryExpression|ConditionalExpression)$/u;
const SKIP_PATTERN = /^(?:ArrowFunction|Class|Function)Expression$/u;
const DYNAMIC_PATTERN = /^(?:Call|Member|New|TaggedTemplate|Yield)Expression$/u;
/**
* @typedef {Object} LoopConditionInfo
* @property {eslint-scope.Reference} reference - The reference.
* @property {ASTNode} group - BinaryExpression or ConditionalExpression nodes
* that the reference is belonging to.
* @property {Function} isInLoop - The predicate which checks a given reference
* is in this loop.
* @property {boolean} modified - The flag that the reference is modified in
* this loop.
*/
/**
* Checks whether or not a given reference is a write reference.
* @param {eslint-scope.Reference} reference A reference to check.
* @returns {boolean} `true` if the reference is a write reference.
*/
function isWriteReference(reference) {
if (reference.init) {
const def = reference.resolved && reference.resolved.defs[0];
if (!def || def.type !== "Variable" || def.parent.kind !== "var") {
return false;
}
}
return reference.isWrite();
}
/**
* Checks whether or not a given loop condition info does not have the modified
* flag.
* @param {LoopConditionInfo} condition A loop condition info to check.
* @returns {boolean} `true` if the loop condition info is "unmodified".
*/
function isUnmodified(condition) {
return !condition.modified;
}
/**
* Checks whether or not a given loop condition info does not have the modified
* flag and does not have the group this condition belongs to.
* @param {LoopConditionInfo} condition A loop condition info to check.
* @returns {boolean} `true` if the loop condition info is "unmodified".
*/
function isUnmodifiedAndNotBelongToGroup(condition) {
return !(condition.modified || condition.group);
}
/**
* Checks whether or not a given reference is inside of a given node.
* @param {ASTNode} node A node to check.
* @param {eslint-scope.Reference} reference A reference to check.
* @returns {boolean} `true` if the reference is inside of the node.
*/
function isInRange(node, reference) {
const or = node.range;
const ir = reference.identifier.range;
return or[0] <= ir[0] && ir[1] <= or[1];
}
/**
* Checks whether or not a given reference is inside of a loop node's condition.
* @param {ASTNode} node A node to check.
* @param {eslint-scope.Reference} reference A reference to check.
* @returns {boolean} `true` if the reference is inside of the loop node's
* condition.
*/
const isInLoop = {
WhileStatement: isInRange,
DoWhileStatement: isInRange,
ForStatement(node, reference) {
return (
isInRange(node, reference) &&
!(node.init && isInRange(node.init, reference))
);
}
};
/**
* Gets the function which encloses a given reference.
* This supports only FunctionDeclaration.
* @param {eslint-scope.Reference} reference A reference to get.
* @returns {ASTNode|null} The function node or null.
*/
function getEncloseFunctionDeclaration(reference) {
let node = reference.identifier;
while (node) {
if (node.type === "FunctionDeclaration") {
return node.id ? node : null;
}
node = node.parent;
}
return null;
}
/**
* Updates the "modified" flags of given loop conditions with given modifiers.
* @param {LoopConditionInfo[]} conditions The loop conditions to be updated.
* @param {eslint-scope.Reference[]} modifiers The references to update.
* @returns {void}
*/
function updateModifiedFlag(conditions, modifiers) {
for (let i = 0; i < conditions.length; ++i) {
const condition = conditions[i];
for (let j = 0; !condition.modified && j < modifiers.length; ++j) {
const modifier = modifiers[j];
let funcNode, funcVar;
/*
* Besides checking for the condition being in the loop, we want to
* check the function that this modifier is belonging to is called
* in the loop.
* FIXME: This should probably be extracted to a function.
*/
const inLoop = condition.isInLoop(modifier) || Boolean(
(funcNode = getEncloseFunctionDeclaration(modifier)) &&
(funcVar = astUtils.getVariableByName(modifier.from.upper, funcNode.id.name)) &&
funcVar.references.some(condition.isInLoop)
);
condition.modified = inLoop;
}
}
}
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('../shared/types').Rule} */
module.exports = {
meta: {
type: "problem",
docs: {
description: "Disallow unmodified loop conditions",
recommended: false,
url: "https://eslint.org/docs/rules/no-unmodified-loop-condition"
},
schema: [],
messages: {
loopConditionNotModified: "'{{name}}' is not modified in this loop."
}
},
create(context) {
const sourceCode = context.getSourceCode();
let groupMap = null;
/**
* Reports a given condition info.
* @param {LoopConditionInfo} condition A loop condition info to report.
* @returns {void}
*/
function report(condition) {
const node = condition.reference.identifier;
context.report({
node,
messageId: "loopConditionNotModified",
data: node
});
}
/**
* Registers given conditions to the group the condition belongs to.
* @param {LoopConditionInfo[]} conditions A loop condition info to
* register.
* @returns {void}
*/
function registerConditionsToGroup(conditions) {
for (let i = 0; i < conditions.length; ++i) {
const condition = conditions[i];
if (condition.group) {
let group = groupMap.get(condition.group);
if (!group) {
group = [];
groupMap.set(condition.group, group);
}
group.push(condition);
}
}
}
/**
* Reports references which are inside of unmodified groups.
* @param {LoopConditionInfo[]} conditions A loop condition info to report.
* @returns {void}
*/
function checkConditionsInGroup(conditions) {
if (conditions.every(isUnmodified)) {
conditions.forEach(report);
}
}
/**
* Checks whether or not a given group node has any dynamic elements.
* @param {ASTNode} root A node to check.
* This node is one of BinaryExpression or ConditionalExpression.
* @returns {boolean} `true` if the node is dynamic.
*/
function hasDynamicExpressions(root) {
let retv = false;
Traverser.traverse(root, {
visitorKeys: sourceCode.visitorKeys,
enter(node) {
if (DYNAMIC_PATTERN.test(node.type)) {
retv = true;
this.break();
} else if (SKIP_PATTERN.test(node.type)) {
this.skip();
}
}
});
return retv;
}
/**
* Creates the loop condition information from a given reference.
* @param {eslint-scope.Reference} reference A reference to create.
* @returns {LoopConditionInfo|null} Created loop condition info, or null.
*/
function toLoopCondition(reference) {
if (reference.init) {
return null;
}
let group = null;
let child = reference.identifier;
let node = child.parent;
while (node) {
if (SENTINEL_PATTERN.test(node.type)) {
if (LOOP_PATTERN.test(node.type) && node.test === child) {
// This reference is inside of a loop condition.
return {
reference,
group,
isInLoop: isInLoop[node.type].bind(null, node),
modified: false
};
}
// This reference is outside of a loop condition.
break;
}
/*
* If it's inside of a group, OK if either operand is modified.
* So stores the group this reference belongs to.
*/
if (GROUP_PATTERN.test(node.type)) {
// If this expression is dynamic, no need to check.
if (hasDynamicExpressions(node)) {
break;
} else {
group = node;
}
}
child = node;
node = node.parent;
}
return null;
}
/**
* Finds unmodified references which are inside of a loop condition.
* Then reports the references which are outside of groups.
* @param {eslint-scope.Variable} variable A variable to report.
* @returns {void}
*/
function checkReferences(variable) {
// Gets references that exist in loop conditions.
const conditions = variable
.references
.map(toLoopCondition)
.filter(Boolean);
if (conditions.length === 0) {
return;
}
// Registers the conditions to belonging groups.
registerConditionsToGroup(conditions);
// Check the conditions are modified.
const modifiers = variable.references.filter(isWriteReference);
if (modifiers.length > 0) {
updateModifiedFlag(conditions, modifiers);
}
/*
* Reports the conditions which are not belonging to groups.
* Others will be reported after all variables are done.
*/
conditions
.filter(isUnmodifiedAndNotBelongToGroup)
.forEach(report);
}
return {
"Program:exit"() {
const queue = [context.getScope()];
groupMap = new Map();
let scope;
while ((scope = queue.pop())) {
queue.push(...scope.childScopes);
scope.variables.forEach(checkReferences);
}
groupMap.forEach(checkConditionsInGroup);
groupMap = null;
}
};
}
};