Add new rule EC31: Prefer lighter formats for image files#52
Add new rule EC31: Prefer lighter formats for image files#52salah-dev-inp wants to merge 2 commits intogreen-code-initiative:mainfrom
Conversation
4a61b04 to
b69c5f3
Compare
| ### Changed | ||
|
|
||
| ### Deleted | ||
| ## [1.6.2] - 2024-05-30 |
There was a problem hiding this comment.
why do you add this section title ?
please remove it and let your new line added under "Unreleased" section
| import org.sonar.api.utils.log.Loggers; | ||
|
|
||
| @Rule(key = "EC31") | ||
| @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "EC31") |
| if (tree.is(Tree.Kind.STRING_LITERAL)) { | ||
| LiteralTree literalTree = (LiteralTree) tree; | ||
| String value = literalTree.value(); | ||
|
|
There was a problem hiding this comment.
her, you can "return" empty if your string length is not > 6 (because the minimum length is 7 for your image name is : "a.png"
thus no need to execute the next if and the next for of the algorithm, for nothing.
| public PreferLighterImageFormatsCheck(PreferLighterImageFormatsCheck checker) { | ||
| } | ||
|
|
||
|
|
There was a problem hiding this comment.
add a use case test with my review feedback above => no need to check if string length isn't > 6
|
This PR has been automatically marked as stale because it has no activity for 30 days. |
|
This PR has been automatically marked as stale because it has no activity for 30 days. |
| import org.sonar.api.utils.log.Logger; | ||
| import org.sonar.api.utils.log.Loggers; | ||
|
|
||
| @Rule(key = "EC31") |
There was a problem hiding this comment.
change to "GCI31" and make modification to creedengo-rules-specifications (currently, there is no entry for GCI31 for java)
There was a problem hiding this comment.
Pull request overview
This PR introduces a new Sonar Java rule EC31 to detect string literals referencing heavier image formats (e.g., .jpg, .jpeg, .png) and encourage using lighter alternatives (e.g., .webp, .avif). It wires the new check into the plugin registration and updates the existing tests and changelog to reflect the additional rule.
Changes:
- Added new check implementation
PreferLighterImageFormats(EC31) scanningSTRING_LITERALnodes for heavy image extensions. - Registered the new rule in
JavaCheckRegistrarand updated rule-count assertions in existing tests. - Added a dedicated check test and a new test fixture source file.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java | Updates expected repository rule count to include EC31. |
| src/test/java/fr/greencodeinitiative/java/JavaCheckRegistrarTest.java | Updates expected registered check count to include EC31. |
| src/test/java/fr/greencodeinitiative/java/checks/PreferLighterImageFormatsCheckTest.java | Adds verifier-based test for the new EC31 check. |
| src/test/files/PreferLighterImageFormatsCheck.java | Adds test fixture with compliant/noncompliant string literals for image paths. |
| src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java | Registers the new check class (and adjusts imports). |
| src/main/java/fr/greencodeinitiative/java/checks/PreferLighterImageFormats.java | Implements new EC31 rule logic for detecting heavier image formats in string literals. |
| CHANGELOG.md | Documents the addition of the EC31 rule. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| 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.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); |
| public List<Tree.Kind> nodesToVisit() { | ||
| return Arrays.asList(Tree.Kind.STRING_LITERAL); | ||
| } | ||
|
|
| 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); |
| 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; | ||
| } |
No description provided.