Skip to content

Commit 49cf571

Browse files
author
Mathis Girault
committed
additionnal changes for sonar integration
1 parent 8798690 commit 49cf571

File tree

7 files changed

+74
-2
lines changed

7 files changed

+74
-2
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Should avoid getting the collection size in loop
2+
3+
## Why is this an issue ?
4+
5+
Accessing the size or length of a collection (such as arrays, maps, or sets) inside a loop can lead to unnecessary performance overhead, especially if the collection size is recalculated on each iteration. This rule warns when the size of a collection is accessed within a loop condition or body, encouraging developers to cache the size before the loop.
6+
7+
### Examples of **incorrect** code
8+
9+
```javascript
10+
for (let i = 0; i < array.length; i++) {
11+
doSomething(array[i]);
12+
}
13+
```
14+
15+
### Examples of **correct** code
16+
17+
```javascript
18+
const len = array.length;
19+
for (let i = 0; i < len; i++) {
20+
doSomething(array[i]);
21+
}
22+
```

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// Requirements
2323
//------------------------------------------------------------------------------
2424

25-
const rule = require("../../../lib/rules/getting-collection-size-in-collection");
25+
const rule = require("../../../lib/rules/avoid-getting-size-collection-in-loop");
2626
const RuleTester = require("eslint").RuleTester;
2727

2828
//------------------------------------------------------------------------------
@@ -34,7 +34,7 @@ const expectedError = {
3434
messageId: "avoidSizeInLoop",
3535
};
3636

37-
ruleTester.run("getting-collection-size-in-collection", rule, {
37+
ruleTester.run("avoid-getting-size-collection-in-loop", rule, {
3838
valid: [
3939
// size/length assigned before loop, not used in loop
4040
`

sonar-plugin/src/main/java/org/greencodeinitiative/creedengo/javascript/CheckList.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
3737
AvoidAutoPlay.class,
3838
AvoidBrightnessOverride.class,
3939
AvoidCSSAnimations.class,
40+
AvoidGettingSizeCollectionInLoop.class,
4041
AvoidHighAccuracyGeolocation.class,
4142
AvoidKeepAwake.class,
4243
LimitDbQueryResult.class,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Creedengo JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs
3+
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.greencodeinitiative.creedengo.javascript.checks;
19+
20+
import org.greencodeinitiative.creedengo.javascript.DeprecatedEcoCodeRule;
21+
import org.sonar.check.Rule;
22+
import org.sonar.plugins.javascript.api.EslintBasedCheck;
23+
import org.sonar.plugins.javascript.api.JavaScriptRule;
24+
import org.sonar.plugins.javascript.api.TypeScriptRule;
25+
26+
@JavaScriptRule
27+
@TypeScriptRule
28+
@Rule(key = AvoidGettingSizeCollectionInLoop.RULE_KEY)
29+
public class AvoidGettingSizeCollectionInLoop implements EslintBasedCheck {
30+
31+
public static final String RULE_KEY = "GCI3";
32+
33+
@Override
34+
public String eslintKey() {
35+
return "@creedengo/avoid-getting-size-collection-in-loop";
36+
}
37+
38+
}

sonar-plugin/src/main/resources/org/greencodeinitiative/creedengo/profiles/javascript_profile.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "Creedengo",
33
"ruleKeys": [
4+
"GCI3",
45
"GCI9",
56
"GCI11",
67
"GCI12",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Non-compliant: the collection size is accessed (twice) in the loop
2+
for (let i = 0; i < array.length; i++) {
3+
process("test"); // Noncompliant
4+
}
5+
6+
// Compliant: no collection size access in loop
7+
const arrayLength = array.length; // Fetch the length once
8+
for (let i = 0; i < arrayLength; i++) {
9+
console.log("yay"); // Compliant
10+
}

0 commit comments

Comments
 (0)