Skip to content

Add new rule EC31: Prefer lighter formats for image files#52

Draft
salah-dev-inp wants to merge 2 commits intogreen-code-initiative:mainfrom
salah-dev-inp:EC31-prefer-lighter-formats-for-image-file
Draft

Add new rule EC31: Prefer lighter formats for image files#52
salah-dev-inp wants to merge 2 commits intogreen-code-initiative:mainfrom
salah-dev-inp:EC31-prefer-lighter-formats-for-image-file

Conversation

@salah-dev-inp
Copy link
Copy Markdown

No description provided.

@salah-dev-inp salah-dev-inp force-pushed the EC31-prefer-lighter-formats-for-image-file branch from 4a61b04 to b69c5f3 Compare May 30, 2024 12:06
Comment thread CHANGELOG.md
### Changed

### Deleted
## [1.6.2] - 2024-05-30
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you add this line ?

if (tree.is(Tree.Kind.STRING_LITERAL)) {
LiteralTree literalTree = (LiteralTree) tree;
String value = literalTree.value();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
}


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a use case test with my review feedback above => no need to check if string length isn't > 6

@github-actions
Copy link
Copy Markdown

This PR has been automatically marked as stale because it has no activity for 30 days.
Please add a comment if you want to keep the issue open. Thank you for your contributions!

@github-actions
Copy link
Copy Markdown

This PR has been automatically marked as stale because it has no activity for 30 days.
Please add a comment if you want to keep the issue open. Thank you for your contributions!

import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

@Rule(key = "EC31")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to "GCI31" and make modification to creedengo-rules-specifications (currently, there is no entry for GCI31 for java)

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) scanning STRING_LITERAL nodes for heavy image extensions.
  • Registered the new rule in JavaCheckRegistrar and 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.*;
Comment on lines +27 to +34
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);
}

Comment on lines +47 to +51
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);
Comment on lines +45 to +58
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Review in progress

Development

Successfully merging this pull request may close these issues.

3 participants