diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1ce62a8..e26a53e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,9 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
-### Changed
-### Deleted
+## [1.6.2] - 2024-05-30
+
+### Added
+
+- EC31 rule : Prefer lighter formats for image files
## [1.6.1] - 2024-05-15
diff --git a/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java b/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java
index f4ef343..02fea6b 100644
--- a/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java
+++ b/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java
@@ -20,21 +20,7 @@
import java.util.Collections;
import java.util.List;
-import fr.greencodeinitiative.java.checks.ArrayCopyCheck;
-import fr.greencodeinitiative.java.checks.AvoidFullSQLRequest;
-import fr.greencodeinitiative.java.checks.AvoidGettingSizeCollectionInLoop;
-import fr.greencodeinitiative.java.checks.AvoidMultipleIfElseStatement;
-import fr.greencodeinitiative.java.checks.AvoidRegexPatternNotStatic;
-import fr.greencodeinitiative.java.checks.AvoidSQLRequestInLoop;
-import fr.greencodeinitiative.java.checks.AvoidSetConstantInBatchUpdate;
-import fr.greencodeinitiative.java.checks.AvoidSpringRepositoryCallInLoopOrStreamCheck;
-import fr.greencodeinitiative.java.checks.AvoidStatementForDMLQueries;
-import fr.greencodeinitiative.java.checks.AvoidUsageOfStaticCollections;
-import fr.greencodeinitiative.java.checks.FreeResourcesOfAutoCloseableInterface;
-import fr.greencodeinitiative.java.checks.IncrementCheck;
-import fr.greencodeinitiative.java.checks.InitializeBufferWithAppropriateSize;
-import fr.greencodeinitiative.java.checks.NoFunctionCallWhenDeclaringForLoop;
-import fr.greencodeinitiative.java.checks.OptimizeReadFileExceptions;
+import fr.greencodeinitiative.java.checks.*;
import org.sonar.plugins.java.api.CheckRegistrar;
import org.sonar.plugins.java.api.JavaCheck;
import org.sonarsource.api.sonarlint.SonarLintSide;
@@ -62,7 +48,8 @@ public class JavaCheckRegistrar implements CheckRegistrar {
InitializeBufferWithAppropriateSize.class,
AvoidSetConstantInBatchUpdate.class,
FreeResourcesOfAutoCloseableInterface.class,
- AvoidMultipleIfElseStatement.class
+ AvoidMultipleIfElseStatement.class,
+ PreferLighterImageFormats.class
);
/**
diff --git a/src/main/java/fr/greencodeinitiative/java/checks/PreferLighterImageFormats.java b/src/main/java/fr/greencodeinitiative/java/checks/PreferLighterImageFormats.java
new file mode 100644
index 0000000..ce001bd
--- /dev/null
+++ b/src/main/java/fr/greencodeinitiative/java/checks/PreferLighterImageFormats.java
@@ -0,0 +1,62 @@
+/*
+ * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
+ * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+package fr.greencodeinitiative.java.checks;
+
+import java.util.Arrays;
+import java.util.List;
+import org.sonar.check.Rule;
+import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
+import org.sonar.plugins.java.api.tree.Tree;
+import org.sonar.plugins.java.api.tree.LiteralTree;
+import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
+
+@Rule(key = "EC31")
+@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "EC31")
+public class PreferLighterImageFormats extends IssuableSubscriptionVisitor {
+
+ private static final Logger LOGGER = Loggers.get(PreferLighterImageFormats.class);
+ private static final String MESSAGE = "Consider using lighter image formats like .webp or .avif instead of .jpg, .jpeg or .png";
+
+ private static final List HEAVY_FORMATS = Arrays.asList(".jpg", ".jpeg", ".png");
+
+ @Override
+ public List nodesToVisit() {
+ return Arrays.asList(Tree.Kind.STRING_LITERAL);
+ }
+
+ public void visitNode(Tree tree) {
+ if (tree.is(Tree.Kind.STRING_LITERAL)) {
+ LiteralTree literalTree = (LiteralTree) tree;
+ String value = literalTree.value();
+
+ // Remove the quotes around the literal value
+ if (value.length() > 2 && value.startsWith("\"") && value.endsWith("\"")) {
+ value = value.substring(1, value.length() - 1);
+ }
+
+ for (String format : HEAVY_FORMATS) {
+ if (value.endsWith(format)) {
+ reportIssue(literalTree, MESSAGE);
+ break;
+ }
+ }
+ }
+ }
+}
diff --git a/src/test/files/PreferLighterImageFormatsCheck.java b/src/test/files/PreferLighterImageFormatsCheck.java
new file mode 100644
index 0000000..6d7085a
--- /dev/null
+++ b/src/test/files/PreferLighterImageFormatsCheck.java
@@ -0,0 +1,56 @@
+/*
+ * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
+ * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+package fr.greencodeinitiative.java.checks;
+
+import java.util.logging.Logger;
+
+class PreferLighterImageFormatsCheck {
+
+ private static final Logger LOGGER = Logger.getLogger(PreferLighterImageFormatsCheck.class.getName());
+
+ public PreferLighterImageFormatsCheck(PreferLighterImageFormatsCheck checker) {
+ }
+
+
+ public void testDirectAssignments() {
+ String imagePath1 = "images/photo.jpg"; // Noncompliant
+ String imagePath2 = "images/graphic.png"; // Noncompliant
+ String imagePath3 = "images/photo.webp"; // Compliant
+ String imagePath4 = "images/graphic.avif"; // Compliant
+ }
+
+ public void testPathsInArray() {
+ String[] imagePaths = {
+ "assets/image1.jpg", // Noncompliant
+ "assets/image2.png", // Noncompliant
+ "assets/image3.webp", // Compliant
+ "assets/image4.avif" // Compliant
+ };
+
+ for (String imagePath : imagePaths) {
+ LOGGER.info("Image path: " + imagePath);
+ }
+ }
+
+ public void testPathsInMethods() {
+ logImagePath("icons/icon1.jpg"); // Noncompliant
+ logImagePath("icons/icon2.png"); // Noncompliant
+ logImagePath("icons/icon3.webp"); // Compliant
+ logImagePath("icons/icon4.avif"); // Compliant
+ }
+}
diff --git a/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java b/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java
index 02270ca..5933a93 100644
--- a/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java
+++ b/src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java
@@ -31,7 +31,7 @@ void checkNumberRules() {
final JavaCheckRegistrar registrar = new JavaCheckRegistrar();
registrar.register(context);
- assertThat(context.checkClasses()).hasSize(15);
+ assertThat(context.checkClasses()).hasSize(16);
assertThat(context.testCheckClasses()).isEmpty();
}
diff --git a/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java b/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java
index ad1b536..4eb941c 100644
--- a/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java
+++ b/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java
@@ -46,7 +46,7 @@ void init() {
RulesDefinition.Context context = new RulesDefinition.Context();
rulesDefinition.define(context);
repository = context.repository(rulesDefinition.repositoryKey());
- rulesSize = 15;
+ rulesSize = 16;
}
@Test
diff --git a/src/test/java/fr/greencodeinitiative/java/checks/PreferLighterImageFormatsCheckTest.java b/src/test/java/fr/greencodeinitiative/java/checks/PreferLighterImageFormatsCheckTest.java
new file mode 100644
index 0000000..373a5df
--- /dev/null
+++ b/src/test/java/fr/greencodeinitiative/java/checks/PreferLighterImageFormatsCheckTest.java
@@ -0,0 +1,33 @@
+/*
+ * ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
+ * Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+package fr.greencodeinitiative.java.checks;
+
+import org.junit.jupiter.api.Test;
+import org.sonar.java.checks.verifier.CheckVerifier;
+
+class PreferLighterImageFormatsCheckTest {
+
+ @Test
+ void test() {
+ CheckVerifier.newVerifier()
+ .onFile("src/test/files/PreferLighterImageFormatsCheck.java")
+ .withCheck(new PreferLighterImageFormats())
+ .verifyIssues();
+ }
+}
+