Skip to content

Commit 08aebb3

Browse files
author
Mathis Girault
committed
add new test case + lint
1 parent 8c1f3b2 commit 08aebb3

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

eslint-plugin/lib/rules/avoid-getting-size-collection-in-loop.js

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,44 @@ module.exports = {
2323
meta: {
2424
type: "suggestion",
2525
docs: {
26-
description: "Avoid getting the size/length of the collection in loops and callbacks. Assign it to a variable before the loop/callback.",
26+
description:
27+
"Avoid getting the size/length of the collection in loops and callbacks. Assign it to a variable before the loop/callback.",
2728
category: "eco-design",
2829
recommended: "warn",
2930
},
3031
messages: {
31-
avoidSizeInLoop: "Avoid getting the size/length of the collection in the loop or callback. Assign it to a variable before the loop/callback.",
32+
avoidSizeInLoop:
33+
"Avoid getting the size/length of the collection in the loop or callback. Assign it to a variable before the loop/callback.",
3234
},
3335
schema: [],
3436
},
3537
create: function (context) {
3638
const SIZE_PROPERTIES = ["length", "size"]; // We only include static analysis on size and length properties at the moment
37-
const CALLBACK_METHODS = ["filter", "find", "findIndex", "findLast", "findLastIndex", "some", "every", "flatMap", "forEach", "map", "reduce", "reduceRight", "some"];
39+
const CALLBACK_METHODS = [
40+
"filter",
41+
"find",
42+
"findIndex",
43+
"findLast",
44+
"findLastIndex",
45+
"some",
46+
"every",
47+
"flatMap",
48+
"forEach",
49+
"map",
50+
"reduce",
51+
"reduceRight",
52+
"some",
53+
];
3854

3955
/**
4056
* Checks if a node is a .length or .size property access (dot or bracket notation).
4157
* If you want to only match dot notation, keep !node.computed.
4258
*/
4359
function isSizeOrLengthMember(node) {
44-
return node.type === "MemberExpression" && SIZE_PROPERTIES.includes(node.property.name);
60+
return (
61+
node.type === "MemberExpression" &&
62+
SIZE_PROPERTIES.includes(node.property.name)
63+
);
4564
}
4665

4766
/**
@@ -77,7 +96,8 @@ module.exports = {
7796
if (
7897
node.arguments &&
7998
node.arguments.length > 0 &&
80-
(node.arguments[0].type === "FunctionExpression" || node.arguments[0].type === "ArrowFunctionExpression")
99+
(node.arguments[0].type === "FunctionExpression" ||
100+
node.arguments[0].type === "ArrowFunctionExpression")
81101
) {
82102
checkNodeRecursively(node.arguments[0].body);
83103
}

eslint-plugin/tests/lib/rules/avoid-getting-size-collection-in-loop.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,19 @@ const expectedError = {
3636

3737
ruleTester.run("avoid-getting-size-collection-in-loop", rule, {
3838
valid: [
39-
// size/length assigned before loop, not used in loop
39+
// size/length assigned before loop, not used in loop body or test
4040
`
4141
const n = arr.length;
4242
for (let i = 0; i < n; i++) {
4343
doSomething(arr[i]);
4444
}
4545
`,
46+
`
47+
const n = arr.length;
48+
for (let i = n; i < n + 5; i++) {
49+
doSomething(arr[i - 5]);
50+
}
51+
`,
4652
// unrelated property in loop condition
4753
`
4854
for (let i = 0; i < arr.customProp; i++) {
@@ -254,21 +260,24 @@ ruleTester.run("avoid-getting-size-collection-in-loop", rule, {
254260
});
255261
`,
256262
errors: [expectedError],
257-
},{
263+
},
264+
{
258265
code: `
259266
arr.map(item => {
260267
doSomething(arr.length);
261268
});
262269
`,
263270
errors: [expectedError],
264-
},{
271+
},
272+
{
265273
code: `
266274
arr.reduce(item => {
267275
doSomething(arr.length);
268276
});
269277
`,
270278
errors: [expectedError],
271-
},{
279+
},
280+
{
272281
code: `
273282
arr.reduceRight(item => {
274283
doSomething(arr.length);

0 commit comments

Comments
 (0)