From 9849ce4ed3c48a6bdcecd967c4e3abef14650741 Mon Sep 17 00:00:00 2001 From: plougastela Date: Thu, 6 Apr 2023 09:50:08 +0200 Subject: [PATCH 01/15] CRJVM204 Detect unoptimized image format --- .../greencodeinitiative/java/RulesList.java | 23 +------- .../checks/DetectUnoptimizedImageFormat.java | 42 +++++++++++++ .../l10n/java/rules/java/CRJVM204.html | 15 +++++ .../l10n/java/rules/java/CRJVM204.json | 15 +++++ .../files/DetectUnoptimizedFileFormat.java | 59 +++++++++++++++++++ .../DetectUnoptimizedFileFormatComplient.java | 47 +++++++++++++++ .../DetectUnoptimizedImageFormatTest.java | 23 ++++++++ 7 files changed, 204 insertions(+), 20 deletions(-) create mode 100644 java-plugin/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java create mode 100644 java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.html create mode 100644 java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.json create mode 100644 java-plugin/src/test/files/DetectUnoptimizedFileFormat.java create mode 100644 java-plugin/src/test/files/DetectUnoptimizedFileFormatComplient.java create mode 100644 java-plugin/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java index d6a5cdbf4..76d268c01 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java @@ -24,25 +24,7 @@ import java.util.Collections; import java.util.List; -import fr.greencodeinitiative.java.checks.ArrayCopyCheck; -import fr.greencodeinitiative.java.checks.AvoidConcatenateStringsInLoop; -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.AvoidSpringRepositoryCallInLoopCheck; -import fr.greencodeinitiative.java.checks.AvoidStatementForDMLQueries; -import fr.greencodeinitiative.java.checks.AvoidUsageOfStaticCollections; -import fr.greencodeinitiative.java.checks.AvoidUsingGlobalVariablesCheck; -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.UnnecessarilyAssignValuesToVariables; -import fr.greencodeinitiative.java.checks.UseCorrectForLoop; +import fr.greencodeinitiative.java.checks.*; import org.sonar.plugins.java.api.JavaCheck; public final class RulesList { @@ -77,7 +59,8 @@ public static List> getJavaChecks() { AvoidUsingGlobalVariablesCheck.class, AvoidSetConstantInBatchUpdate.class, FreeResourcesOfAutoCloseableInterface.class, - AvoidMultipleIfElseStatement.class + AvoidMultipleIfElseStatement.class, + DetectUnoptimizedImageFormat.class )); } diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java new file mode 100644 index 000000000..43eb24d70 --- /dev/null +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java @@ -0,0 +1,42 @@ +package fr.greencodeinitiative.java.checks; + +import org.sonar.check.Priority; +import org.sonar.check.Rule; +import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; +import org.sonar.plugins.java.api.tree.LiteralTree; +import org.sonar.plugins.java.api.tree.Tree; + +import java.util.Arrays; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +@Rule( + key = "CRJVM204", + name = "Developpement", + description = IncrementCheck.MESSAGERULE, + priority = Priority.MINOR, + tags = {"bug"}) +public class DetectUnoptimizedImageFormat extends IssuableSubscriptionVisitor { + + protected static final String MESSAGERULE = "Detect unoptimized image format"; + protected static final String MESSAGEERROR = "If possible, utilisation of svg image format is recommended over other image format."; + protected static Pattern IMG_EXTENSION = Pattern.compile("\\.(bmp|ico|tiff|webp|png|jpg|jpeg|jfif|pjpeg|pjp|gif|avif|apng)"); + + @Override + public List nodesToVisit() { + return Arrays.asList(Tree.Kind.STRING_LITERAL, Tree.Kind.TEXT_BLOCK); + } + + @Override + public void visitNode(Tree tree) { + + if (tree.is(Tree.Kind.STRING_LITERAL, Tree.Kind.TEXT_BLOCK)) { + final String strValue = ((LiteralTree) tree).value(); + final Matcher matcher = IMG_EXTENSION.matcher(strValue); + if(matcher.find()) { + reportIssue(tree, MESSAGEERROR); + } + } + } +} diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.html new file mode 100644 index 000000000..f2096cf9d --- /dev/null +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.html @@ -0,0 +1,15 @@ +

"If possible, utilisation of svg image format is recommended over other image format.

+

Noncompliant Code Example

+
+    jEditorPane.setContentType("text/html");
+    jEditorPane.setText("\"Grapefruit\"");
+
+

Compliant Solution

+
+    jEditorPane.setContentType("text/html");
+    jEditorPane.setText("\"Grapefruit\"");
+
diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.json new file mode 100644 index 000000000..199e43d9c --- /dev/null +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.json @@ -0,0 +1,15 @@ +{ + "title": "Detect unoptimized image format", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "15min" + }, + "tags": [ + "eco-design", + "bug", + "ecocode" + ], + "defaultSeverity": "Minor" +} diff --git a/java-plugin/src/test/files/DetectUnoptimizedFileFormat.java b/java-plugin/src/test/files/DetectUnoptimizedFileFormat.java new file mode 100644 index 000000000..31ad318a8 --- /dev/null +++ b/java-plugin/src/test/files/DetectUnoptimizedFileFormat.java @@ -0,0 +1,59 @@ +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.LayoutManager; +import java.io.IOException; +import java.net.URL; + +import javax.swing.JEditorPane; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JScrollPane; + +public class SwingTester { + private static void createWindow() { + JFrame frame = new JFrame("Swing Tester"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + createUI(frame); + frame.setSize(560, 450); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + } + + private static void createUI(final JFrame frame) { + JPanel panel = new JPanel(); + LayoutManager layout = new FlowLayout(); + panel.setLayout(layout); + + JEditorPane jEditorPane = new JEditorPane(); + jEditorPane.setEditable(false); + + jEditorPane.setContentType("text/html"); + jEditorPane.setText("Le titre avec une partie en gras," + + " et une autre partie en bleu." + + " \"Grapefruit" + + " \"Grapefruit" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.bmp\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.tiff\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.webp\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.png\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.jpeg\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.jfif\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.pjpeg\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.pjp\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.gif\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.avif\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.apng\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} + ""); + + JScrollPane jScrollPane = new JScrollPane(jEditorPane); + jScrollPane.setPreferredSize(new Dimension(540, 400)); + + panel.add(jScrollPane); + panel.add(jScrollPane2); + frame.getContentPane().add(panel, BorderLayout.CENTER); + } +} \ No newline at end of file diff --git a/java-plugin/src/test/files/DetectUnoptimizedFileFormatComplient.java b/java-plugin/src/test/files/DetectUnoptimizedFileFormatComplient.java new file mode 100644 index 000000000..abd142116 --- /dev/null +++ b/java-plugin/src/test/files/DetectUnoptimizedFileFormatComplient.java @@ -0,0 +1,47 @@ +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.LayoutManager; +import java.io.IOException; +import java.net.URL; + +import javax.swing.JEditorPane; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JScrollPane; + +public class SwingTester { + private static void createWindow() { + JFrame frame = new JFrame("Swing Tester"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + createUI(frame); + frame.setSize(560, 450); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + } + + private static void createUI(final JFrame frame){ + JPanel panel = new JPanel(); + LayoutManager layout = new FlowLayout(); + panel.setLayout(layout); + + JEditorPane jEditorPane3 = new JEditorPane(); + jEditorPane3.setEditable(false); + + jEditorPane3.setContentType("text/html"); + jEditorPane3.setText("Le titre avec une partie en gras," + + " et une autre partie en bleu." + + " \"Grapefruit" + + ""); + + JScrollPane jScrollPane = new JScrollPane(jEditorPane); + jScrollPane.setPreferredSize(new Dimension(540,400)); + + panel.add(jScrollPane); + panel.add(jScrollPane2); + panel.add(jScrollPane3); + frame.getContentPane().add(panel, BorderLayout.CENTER); + } +} \ No newline at end of file diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java new file mode 100644 index 000000000..80f357786 --- /dev/null +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java @@ -0,0 +1,23 @@ +package fr.greencodeinitiative.java.checks; + +import org.junit.jupiter.api.Test; +import org.sonar.java.checks.verifier.CheckVerifier; + +public class DetectUnoptimizedImageFormatTest { + + @Test + void test() { + CheckVerifier.newVerifier() + .onFile("src/test/files/DetectUnoptimizedImageFormat.java") + .withCheck(new DetectUnoptimizedImageFormat()) + .verifyIssues(); + } + + @Test + void testComplient() { + CheckVerifier.newVerifier() + .onFile("src/test/files/DetectUnoptimizedFileFormatComplient.java") + .withCheck(new DetectUnoptimizedImageFormat()) + .verifyNoIssues(); + } +} From a2fbf1b674159dffd31f51f03e9fff135cab678e Mon Sep 17 00:00:00 2001 From: plougastela Date: Thu, 6 Apr 2023 11:03:33 +0200 Subject: [PATCH 02/15] CRJVM204 Detect unoptimized image format --- .../java/checks/DetectUnoptimizedImageFormat.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java index 43eb24d70..2b3190b19 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java @@ -20,7 +20,7 @@ public class DetectUnoptimizedImageFormat extends IssuableSubscriptionVisitor { protected static final String MESSAGERULE = "Detect unoptimized image format"; - protected static final String MESSAGEERROR = "If possible, utilisation of svg image format is recommended over other image format."; + protected static final String MESSAGEERROR = "If possible, the utilisation of svg image format (or html tag) is recommended over other image format."; protected static Pattern IMG_EXTENSION = Pattern.compile("\\.(bmp|ico|tiff|webp|png|jpg|jpeg|jfif|pjpeg|pjp|gif|avif|apng)"); @Override From cc526cc44e5face12f63042f1369d6647d1db420 Mon Sep 17 00:00:00 2001 From: plougastela Date: Thu, 6 Apr 2023 11:19:10 +0200 Subject: [PATCH 03/15] CRJVM204 Detect unoptimized image format --- .../files/DetectUnoptimizedFileFormat.java | 59 ------------- .../files/DetectUnoptimizedImageFormat.java | 84 +++++++++++++++++++ ...etectUnoptimizedImageFormatComplient.java} | 36 ++++++-- .../DetectUnoptimizedImageFormatTest.java | 2 +- 4 files changed, 114 insertions(+), 67 deletions(-) delete mode 100644 java-plugin/src/test/files/DetectUnoptimizedFileFormat.java create mode 100644 java-plugin/src/test/files/DetectUnoptimizedImageFormat.java rename java-plugin/src/test/files/{DetectUnoptimizedFileFormatComplient.java => DetectUnoptimizedImageFormatComplient.java} (50%) diff --git a/java-plugin/src/test/files/DetectUnoptimizedFileFormat.java b/java-plugin/src/test/files/DetectUnoptimizedFileFormat.java deleted file mode 100644 index 31ad318a8..000000000 --- a/java-plugin/src/test/files/DetectUnoptimizedFileFormat.java +++ /dev/null @@ -1,59 +0,0 @@ -import java.awt.BorderLayout; -import java.awt.Dimension; -import java.awt.FlowLayout; -import java.awt.LayoutManager; -import java.io.IOException; -import java.net.URL; - -import javax.swing.JEditorPane; -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; - -public class SwingTester { - private static void createWindow() { - JFrame frame = new JFrame("Swing Tester"); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - createUI(frame); - frame.setSize(560, 450); - frame.setLocationRelativeTo(null); - frame.setVisible(true); - } - - private static void createUI(final JFrame frame) { - JPanel panel = new JPanel(); - LayoutManager layout = new FlowLayout(); - panel.setLayout(layout); - - JEditorPane jEditorPane = new JEditorPane(); - jEditorPane.setEditable(false); - - jEditorPane.setContentType("text/html"); - jEditorPane.setText("Le titre avec une partie en gras," + - " et une autre partie en bleu." + - " \"Grapefruit" + - " \"Grapefruit" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.bmp\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.tiff\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.webp\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.png\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.jpeg\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.jfif\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.pjpeg\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.pjp\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.gif\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.avif\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.apng\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, utilisation of svg image format is recommended over other image format.}} - ""); - - JScrollPane jScrollPane = new JScrollPane(jEditorPane); - jScrollPane.setPreferredSize(new Dimension(540, 400)); - - panel.add(jScrollPane); - panel.add(jScrollPane2); - frame.getContentPane().add(panel, BorderLayout.CENTER); - } -} \ No newline at end of file diff --git a/java-plugin/src/test/files/DetectUnoptimizedImageFormat.java b/java-plugin/src/test/files/DetectUnoptimizedImageFormat.java new file mode 100644 index 000000000..c8c631820 --- /dev/null +++ b/java-plugin/src/test/files/DetectUnoptimizedImageFormat.java @@ -0,0 +1,84 @@ +import java.awt.BorderLayout; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.LayoutManager; +import java.io.IOException; +import java.net.URL; + +import javax.swing.JEditorPane; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JScrollPane; + +public class DetectUnoptimizedImageFormat { + + private String img_bmp = "test/image.bmp"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + private String img_ico = "image.ico"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + private String img_tiff = "test/path/to/image.tiff"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + private String img_webp = "test/path/to/" + "image.webp"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + private String img_jpg = "image.jpg"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + private String img_jpeg = "image.jpeg"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + private String img_jfif = "image.jfif"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + private String img_pjpeg = "image.pjpeg"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + private String img_pjp = "image.pjp"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + private String img_gif = "image.gif"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + private String img_avif = "image.avif"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + private String img_apng = "image.apng"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + + private static String getImagePath(String image) { + return "path/to/" + image; + } + + private static void testImage() { + String img1 = getImagePath("test/image.bmp"); // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + String img2 = getImagePath("image.ico"); // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + String img3 = getImagePath("image.jpg"); // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + } + + private static void createWindow() { + JFrame frame = new JFrame("Swing Tester"); + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + createUI(frame); + frame.setSize(560, 450); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + } + + private static void createUI(final JFrame frame) { + JPanel panel = new JPanel(); + LayoutManager layout = new FlowLayout(); + panel.setLayout(layout); + + JEditorPane jEditorPane = new JEditorPane(); + jEditorPane.setEditable(false); + + jEditorPane.setContentType("text/html"); + jEditorPane.setText("Le titre avec une partie en gras," + + " et une autre partie en bleu." + + " html tag) is recommended over other image format.}} + " alt=\"Grapefruit slice atop a pile of other slices\">" + + " \"Grapefruit" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.bmp\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.tiff\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.webp\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.png\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.jpeg\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.jfif\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.pjpeg\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.pjp\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.gif\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.avif\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + " src=\"/media/cc0-images/grapefruit-slice-332-332.apng\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + ""); + + JScrollPane jScrollPane = new JScrollPane(jEditorPane); + jScrollPane.setPreferredSize(new Dimension(540, 400)); + + panel.add(jScrollPane); + panel.add(jScrollPane2); + frame.getContentPane().add(panel, BorderLayout.CENTER); + } +} \ No newline at end of file diff --git a/java-plugin/src/test/files/DetectUnoptimizedFileFormatComplient.java b/java-plugin/src/test/files/DetectUnoptimizedImageFormatComplient.java similarity index 50% rename from java-plugin/src/test/files/DetectUnoptimizedFileFormatComplient.java rename to java-plugin/src/test/files/DetectUnoptimizedImageFormatComplient.java index abd142116..c0ce54011 100644 --- a/java-plugin/src/test/files/DetectUnoptimizedFileFormatComplient.java +++ b/java-plugin/src/test/files/DetectUnoptimizedImageFormatComplient.java @@ -10,7 +10,19 @@ import javax.swing.JPanel; import javax.swing.JScrollPane; -public class SwingTester { +public class DetectUnoptimizedImageFormat { + + private String img_svg = "test/image.svg"; // Complient + + private String getImagePath(String image) { + return "path/to/" + image; + } + + private static void testImage() { + String img1 = getImagePath("test/image.svg"); // Complient + String img2 = getImagePath("image.svg"); // Complient + } + private static void createWindow() { JFrame frame = new JFrame("Swing Tester"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); @@ -20,24 +32,34 @@ private static void createWindow() { frame.setVisible(true); } - private static void createUI(final JFrame frame){ + private static void createUI(final JFrame frame) { JPanel panel = new JPanel(); LayoutManager layout = new FlowLayout(); panel.setLayout(layout); - JEditorPane jEditorPane3 = new JEditorPane(); - jEditorPane3.setEditable(false); + JEditorPane jEditorPane = new JEditorPane(); + jEditorPane.setEditable(false); - jEditorPane3.setContentType("text/html"); - jEditorPane3.setText("Le titre avec une partie en gras," + + jEditorPane.setContentType("text/html"); + jEditorPane.setText("Le titre avec une partie en gras," + " et une autre partie en bleu." + " \"Grapefruit" + ""); + JEditorPane jEditorPane1 = new JEditorPane(); + jEditorPane1.setEditable(false); + + jEditorPane1.setContentType("text/html"); + jEditorPane1.setText("Le titre avec une partie en gras," + + " et une autre partie en bleu." + + " " + // Complient + " " + + ""); + JScrollPane jScrollPane = new JScrollPane(jEditorPane); - jScrollPane.setPreferredSize(new Dimension(540,400)); + jScrollPane.setPreferredSize(new Dimension(540, 400)); panel.add(jScrollPane); panel.add(jScrollPane2); diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java index 80f357786..d2bd21858 100644 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java @@ -16,7 +16,7 @@ void test() { @Test void testComplient() { CheckVerifier.newVerifier() - .onFile("src/test/files/DetectUnoptimizedFileFormatComplient.java") + .onFile("src/test/files/DetectUnoptimizedImageFormatComplient.java") .withCheck(new DetectUnoptimizedImageFormat()) .verifyNoIssues(); } From 7f427d0b831898d5438fbffb9da50070d015edfe Mon Sep 17 00:00:00 2001 From: plougastela Date: Thu, 6 Apr 2023 11:20:22 +0200 Subject: [PATCH 04/15] CRPYT203 - Detect unoptimized image format --- .../checks/DetectUnoptimizedImageFormat.java | 35 ++++++++++++++++++ .../l10n/python/rules/python/CRPYT203.html | 20 +++++++++++ .../DetectUnoptimizedImageFormatTest.java | 13 +++++++ .../checks/detectUnoptimizedImageFormat.py | 36 +++++++++++++++++++ .../detectUnoptimizedImageFormatComplient.py | 17 +++++++++ 5 files changed, 121 insertions(+) create mode 100644 python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java create mode 100644 python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/CRPYT203.html create mode 100644 python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java create mode 100644 python-plugin/src/test/resources/checks/detectUnoptimizedImageFormat.py create mode 100644 python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatComplient.py diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java new file mode 100644 index 000000000..8db89d276 --- /dev/null +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java @@ -0,0 +1,35 @@ +package fr.greencodeinitiative.python.checks; + +import com.sun.source.tree.LiteralTree; +import org.sonar.plugins.python.api.PythonSubscriptionCheck; +import org.sonar.plugins.python.api.SubscriptionContext; +import org.sonar.plugins.python.api.tree.StringLiteral; +import org.sonar.plugins.python.api.tree.Tree; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class DetectUnoptimizedImageFormat extends PythonSubscriptionCheck { + + public static final String RULE_KEY = "CRPYT203"; + + protected static final String MESSAGERULE = "Detect unoptimized image format"; + protected static final String MESSAGEERROR = "If possible, the utilisation of svg image format (or html tag) is recommended over other image format."; + protected static Pattern IMG_EXTENSION = Pattern.compile("\\.(bmp|ico|tiff|webp|png|jpg|jpeg|jfif|pjpeg|pjp|gif|avif|apng)"); + + @Override + public void initialize(Context context) { + context.registerSyntaxNodeConsumer(Tree.Kind.STRING_LITERAL, this::visitNodeString); + } + + public void visitNodeString(SubscriptionContext ctx) { + if (ctx.syntaxNode().is(Tree.Kind.STRING_LITERAL)) { + final StringLiteral stringLiteral = (StringLiteral) ctx.syntaxNode(); + final String strValue = stringLiteral.trimmedQuotesValue(); + final Matcher matcher = IMG_EXTENSION.matcher(strValue); + if(matcher.find()) { + ctx.addIssue(stringLiteral, MESSAGEERROR); + } + } + } +} diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/CRPYT203.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/CRPYT203.html new file mode 100644 index 000000000..b7ca91bdf --- /dev/null +++ b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/CRPYT203.html @@ -0,0 +1,20 @@ +

If possible, the utilisation of svg image format (or html tag) is recommended over other image format.

+

Noncompliant Code Example

+
+    public void foo() {
+        ...
+        img_jpg = "image.jpg"
+        ...
+    }
+
+
+

Compliant Solution

+
+
+    public void foo() {
+        ...
+        img_svg = "image.svg"
+        ...
+   }
+
+
diff --git a/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java new file mode 100644 index 000000000..a65679236 --- /dev/null +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java @@ -0,0 +1,13 @@ +package fr.greencodeinitiative.python.checks; + +import org.junit.Test; +import org.sonar.python.checks.utils.PythonCheckVerifier; + +public class DetectUnoptimizedImageFormatTest { + + @Test + public void test() { + PythonCheckVerifier.verify("src/test/resources/checks/detectUnoptimizedImageFormat.py", new DetectUnoptimizedImageFormat()); + PythonCheckVerifier.verifyNoIssue("src/test/resources/checks/detectUnoptimizedImageFormatComplient.py", new DetectUnoptimizedImageFormat()); + } +} diff --git a/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormat.py b/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormat.py new file mode 100644 index 000000000..33e2145e9 --- /dev/null +++ b/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormat.py @@ -0,0 +1,36 @@ + +def testImage(image) : + return "path/to/" + image + + +def testImageFormat2() : + + img_bmp = "test/image.bmp" # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + img_ico = "image.ico" # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + img_tiff = "test/path/to/image.tiff" # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + img_webp = "test/path/to/" + "image.webp" # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + img_jpg = "image.jpg" # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + img_jpeg = "image.jpeg" # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + img_jfif = "image.jfif" # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + img_pjpeg = "image.pjpeg" # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + img_pjp = "image.pjp" # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + img_gif = "image.gif" # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + img_avif = "image.avif" # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + img_apng = "image.apng" # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + image_format = testImage("image.jpg") # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + return ('' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + + '' ) \ No newline at end of file diff --git a/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatComplient.py b/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatComplient.py new file mode 100644 index 000000000..d05486a16 --- /dev/null +++ b/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatComplient.py @@ -0,0 +1,17 @@ + +def testImage(image) : + return "path/to/" + image + + +def testImageFormat2() : + + img_svg = "test/image.svg" # Complient + + image_format = testImage("image.svg") # Complient + + image_svg_html = ('' + # Complient + '' + + '') + + return ('' # Complient + + '' ) \ No newline at end of file From 0e947030aba278f2831699eb3a65b2b85393843e Mon Sep 17 00:00:00 2001 From: plougastela Date: Thu, 6 Apr 2023 13:03:41 +0200 Subject: [PATCH 05/15] CRPYT203 - Detect unoptimized image format --- .../greencodeinitiative/java/RulesList.java | 3 +- .../checks/DetectUnoptimizedImageFormat.java | 42 ---------- .../l10n/java/rules/java/CRJVM204.html | 15 ---- .../l10n/java/rules/java/CRJVM204.json | 15 ---- .../files/DetectUnoptimizedImageFormat.java | 84 ------------------- ...DetectUnoptimizedImageFormatComplient.java | 69 --------------- .../DetectUnoptimizedImageFormatTest.java | 23 ----- .../l10n/python/rules/python/CRPYT203.html | 38 +++++++-- 8 files changed, 33 insertions(+), 256 deletions(-) delete mode 100644 java-plugin/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java delete mode 100644 java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.html delete mode 100644 java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.json delete mode 100644 java-plugin/src/test/files/DetectUnoptimizedImageFormat.java delete mode 100644 java-plugin/src/test/files/DetectUnoptimizedImageFormatComplient.java delete mode 100644 java-plugin/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java index 76d268c01..8f2a47c47 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java @@ -59,8 +59,7 @@ public static List> getJavaChecks() { AvoidUsingGlobalVariablesCheck.class, AvoidSetConstantInBatchUpdate.class, FreeResourcesOfAutoCloseableInterface.class, - AvoidMultipleIfElseStatement.class, - DetectUnoptimizedImageFormat.class + AvoidMultipleIfElseStatement.class )); } diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java deleted file mode 100644 index 2b3190b19..000000000 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormat.java +++ /dev/null @@ -1,42 +0,0 @@ -package fr.greencodeinitiative.java.checks; - -import org.sonar.check.Priority; -import org.sonar.check.Rule; -import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; -import org.sonar.plugins.java.api.tree.LiteralTree; -import org.sonar.plugins.java.api.tree.Tree; - -import java.util.Arrays; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -@Rule( - key = "CRJVM204", - name = "Developpement", - description = IncrementCheck.MESSAGERULE, - priority = Priority.MINOR, - tags = {"bug"}) -public class DetectUnoptimizedImageFormat extends IssuableSubscriptionVisitor { - - protected static final String MESSAGERULE = "Detect unoptimized image format"; - protected static final String MESSAGEERROR = "If possible, the utilisation of svg image format (or html tag) is recommended over other image format."; - protected static Pattern IMG_EXTENSION = Pattern.compile("\\.(bmp|ico|tiff|webp|png|jpg|jpeg|jfif|pjpeg|pjp|gif|avif|apng)"); - - @Override - public List nodesToVisit() { - return Arrays.asList(Tree.Kind.STRING_LITERAL, Tree.Kind.TEXT_BLOCK); - } - - @Override - public void visitNode(Tree tree) { - - if (tree.is(Tree.Kind.STRING_LITERAL, Tree.Kind.TEXT_BLOCK)) { - final String strValue = ((LiteralTree) tree).value(); - final Matcher matcher = IMG_EXTENSION.matcher(strValue); - if(matcher.find()) { - reportIssue(tree, MESSAGEERROR); - } - } - } -} diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.html deleted file mode 100644 index f2096cf9d..000000000 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.html +++ /dev/null @@ -1,15 +0,0 @@ -

"If possible, utilisation of svg image format is recommended over other image format.

-

Noncompliant Code Example

-
-    jEditorPane.setContentType("text/html");
-    jEditorPane.setText("\"Grapefruit\"");
-
-

Compliant Solution

-
-    jEditorPane.setContentType("text/html");
-    jEditorPane.setText("\"Grapefruit\"");
-
diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.json b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.json deleted file mode 100644 index 199e43d9c..000000000 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/CRJVM204.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "title": "Detect unoptimized image format", - "type": "CODE_SMELL", - "status": "ready", - "remediation": { - "func": "Constant\/Issue", - "constantCost": "15min" - }, - "tags": [ - "eco-design", - "bug", - "ecocode" - ], - "defaultSeverity": "Minor" -} diff --git a/java-plugin/src/test/files/DetectUnoptimizedImageFormat.java b/java-plugin/src/test/files/DetectUnoptimizedImageFormat.java deleted file mode 100644 index c8c631820..000000000 --- a/java-plugin/src/test/files/DetectUnoptimizedImageFormat.java +++ /dev/null @@ -1,84 +0,0 @@ -import java.awt.BorderLayout; -import java.awt.Dimension; -import java.awt.FlowLayout; -import java.awt.LayoutManager; -import java.io.IOException; -import java.net.URL; - -import javax.swing.JEditorPane; -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; - -public class DetectUnoptimizedImageFormat { - - private String img_bmp = "test/image.bmp"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - private String img_ico = "image.ico"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - private String img_tiff = "test/path/to/image.tiff"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - private String img_webp = "test/path/to/" + "image.webp"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - private String img_jpg = "image.jpg"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - private String img_jpeg = "image.jpeg"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - private String img_jfif = "image.jfif"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - private String img_pjpeg = "image.pjpeg"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - private String img_pjp = "image.pjp"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - private String img_gif = "image.gif"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - private String img_avif = "image.avif"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - private String img_apng = "image.apng"; // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - - - private static String getImagePath(String image) { - return "path/to/" + image; - } - - private static void testImage() { - String img1 = getImagePath("test/image.bmp"); // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - String img2 = getImagePath("image.ico"); // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - String img3 = getImagePath("image.jpg"); // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - } - - private static void createWindow() { - JFrame frame = new JFrame("Swing Tester"); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - createUI(frame); - frame.setSize(560, 450); - frame.setLocationRelativeTo(null); - frame.setVisible(true); - } - - private static void createUI(final JFrame frame) { - JPanel panel = new JPanel(); - LayoutManager layout = new FlowLayout(); - panel.setLayout(layout); - - JEditorPane jEditorPane = new JEditorPane(); - jEditorPane.setEditable(false); - - jEditorPane.setContentType("text/html"); - jEditorPane.setText("Le titre avec une partie en gras," + - " et une autre partie en bleu." + - " html tag) is recommended over other image format.}} - " alt=\"Grapefruit slice atop a pile of other slices\">" + - " \"Grapefruit" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.bmp\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.tiff\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.webp\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.png\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.jpeg\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.jfif\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.pjpeg\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.pjp\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.gif\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.avif\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - " src=\"/media/cc0-images/grapefruit-slice-332-332.apng\" alt=\"Grapefruit slice atop a pile of other slices\">" + // Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - ""); - - JScrollPane jScrollPane = new JScrollPane(jEditorPane); - jScrollPane.setPreferredSize(new Dimension(540, 400)); - - panel.add(jScrollPane); - panel.add(jScrollPane2); - frame.getContentPane().add(panel, BorderLayout.CENTER); - } -} \ No newline at end of file diff --git a/java-plugin/src/test/files/DetectUnoptimizedImageFormatComplient.java b/java-plugin/src/test/files/DetectUnoptimizedImageFormatComplient.java deleted file mode 100644 index c0ce54011..000000000 --- a/java-plugin/src/test/files/DetectUnoptimizedImageFormatComplient.java +++ /dev/null @@ -1,69 +0,0 @@ -import java.awt.BorderLayout; -import java.awt.Dimension; -import java.awt.FlowLayout; -import java.awt.LayoutManager; -import java.io.IOException; -import java.net.URL; - -import javax.swing.JEditorPane; -import javax.swing.JFrame; -import javax.swing.JPanel; -import javax.swing.JScrollPane; - -public class DetectUnoptimizedImageFormat { - - private String img_svg = "test/image.svg"; // Complient - - private String getImagePath(String image) { - return "path/to/" + image; - } - - private static void testImage() { - String img1 = getImagePath("test/image.svg"); // Complient - String img2 = getImagePath("image.svg"); // Complient - } - - private static void createWindow() { - JFrame frame = new JFrame("Swing Tester"); - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - createUI(frame); - frame.setSize(560, 450); - frame.setLocationRelativeTo(null); - frame.setVisible(true); - } - - private static void createUI(final JFrame frame) { - JPanel panel = new JPanel(); - LayoutManager layout = new FlowLayout(); - panel.setLayout(layout); - - JEditorPane jEditorPane = new JEditorPane(); - jEditorPane.setEditable(false); - - jEditorPane.setContentType("text/html"); - jEditorPane.setText("Le titre avec une partie en gras," + - " et une autre partie en bleu." + - " \"Grapefruit" + - ""); - - JEditorPane jEditorPane1 = new JEditorPane(); - jEditorPane1.setEditable(false); - - jEditorPane1.setContentType("text/html"); - jEditorPane1.setText("Le titre avec une partie en gras," + - " et une autre partie en bleu." + - " " + // Complient - " " + - ""); - - JScrollPane jScrollPane = new JScrollPane(jEditorPane); - jScrollPane.setPreferredSize(new Dimension(540, 400)); - - panel.add(jScrollPane); - panel.add(jScrollPane2); - panel.add(jScrollPane3); - frame.getContentPane().add(panel, BorderLayout.CENTER); - } -} \ No newline at end of file diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java deleted file mode 100644 index d2bd21858..000000000 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/DetectUnoptimizedImageFormatTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package fr.greencodeinitiative.java.checks; - -import org.junit.jupiter.api.Test; -import org.sonar.java.checks.verifier.CheckVerifier; - -public class DetectUnoptimizedImageFormatTest { - - @Test - void test() { - CheckVerifier.newVerifier() - .onFile("src/test/files/DetectUnoptimizedImageFormat.java") - .withCheck(new DetectUnoptimizedImageFormat()) - .verifyIssues(); - } - - @Test - void testComplient() { - CheckVerifier.newVerifier() - .onFile("src/test/files/DetectUnoptimizedImageFormatComplient.java") - .withCheck(new DetectUnoptimizedImageFormat()) - .verifyNoIssues(); - } -} diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/CRPYT203.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/CRPYT203.html index b7ca91bdf..a588eef95 100644 --- a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/CRPYT203.html +++ b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/CRPYT203.html @@ -1,20 +1,46 @@ -

If possible, the utilisation of svg image format (or html tag) is recommended over other image format.

+

If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.

+

Because SVGs are generally smaller than other image format, they’re less taxing on your server despite needing to render on load.

+

When to use SVG : +

    +
  • Your image is used for decorative website graphics, logos, icons, graphs and diagrams, and other simple images.
  • +
  • You image require animation.
  • +
  • You image need to be responsive and scale without lack of quality.
  • +
+

+

Some advantages of using SVG : +

    +
  • SVGs are scalable and will render pixel-perfect at any resolution whereas JPEGs, PNGs and GIFs will not.
  • +
  • SVGs are vector images and therefore are usually much smaller in file-size than bitmap-based images.
  • +
  • SVGs can be embedded into the HTML which means they can be cached, edited directly using CSS and indexed for greater accessibility.
  • +
  • SVGs can be animated directly or by using CSS or JavaScript making it easy for web designers to add interactivity to a site.
  • +
+

+ +

Noncompliant Code Example

+
+    ...
+    img_jpg = "image.jpg"
+    ...
+
+

Compliant Solution

+
+    ...
+    img_svg = "image.svg"
+    ...
+

Noncompliant Code Example

     public void foo() {
         ...
-        img_jpg = "image.jpg"
+        image_format = testImage("image.jpg")
         ...
     }
-
 

Compliant Solution

-
     public void foo() {
         ...
-        img_svg = "image.svg"
+        image_format = testImage("image.svg")
         ...
    }
-
 
From 56e65bc74a4845f5406a49feb3384ae247c01d3c Mon Sep 17 00:00:00 2001 From: plougastela Date: Thu, 6 Apr 2023 13:15:31 +0200 Subject: [PATCH 06/15] CRPYT203 - Detect unoptimized image format --- .../python/checks/DetectUnoptimizedImageFormat.java | 10 +++++++++- .../python/rules/python/{CRPYT203.html => EC203.html} | 0 .../python/PythonRuleRepositoryTest.java | 4 ++-- 3 files changed, 11 insertions(+), 3 deletions(-) rename python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/{CRPYT203.html => EC203.html} (100%) diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java index 8db89d276..efbbd0b2f 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java @@ -1,6 +1,8 @@ package fr.greencodeinitiative.python.checks; import com.sun.source.tree.LiteralTree; +import org.sonar.check.Priority; +import org.sonar.check.Rule; import org.sonar.plugins.python.api.PythonSubscriptionCheck; import org.sonar.plugins.python.api.SubscriptionContext; import org.sonar.plugins.python.api.tree.StringLiteral; @@ -9,9 +11,15 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +@Rule( + key = DetectUnoptimizedImageFormat.RULE_KEY, + name = "Detect unoptimized image format", + description = DetectUnoptimizedImageFormat.MESSAGEERROR, + priority = Priority.MINOR, + tags = {"bug", "eco-design", "ecocode"}) public class DetectUnoptimizedImageFormat extends PythonSubscriptionCheck { - public static final String RULE_KEY = "CRPYT203"; + public static final String RULE_KEY = "EC203"; protected static final String MESSAGERULE = "Detect unoptimized image format"; protected static final String MESSAGEERROR = "If possible, the utilisation of svg image format (or html tag) is recommended over other image format."; diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/CRPYT203.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC203.html similarity index 100% rename from python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/CRPYT203.html rename to python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC203.html diff --git a/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonRuleRepositoryTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonRuleRepositoryTest.java index 101ff2841..f724dd805 100644 --- a/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonRuleRepositoryTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonRuleRepositoryTest.java @@ -44,8 +44,8 @@ public void init() { public void test() { assertThat(pythonRuleRepository.repositoryKey()).isEqualTo(PythonRuleRepository.REPOSITORY_KEY); assertThat(context.repositories()).hasSize(1).extracting("key").containsExactly(pythonRuleRepository.repositoryKey()); - assertThat(context.repositories().get(0).rules()).hasSize(6); - assertThat(pythonRuleRepository.checkClasses()).hasSize(6); + assertThat(context.repositories().get(0).rules()).hasSize(7); + assertThat(pythonRuleRepository.checkClasses()).hasSize(7); } From 37d029c3d0b7e21cf0f1c8434c3df2bb0ee80e69 Mon Sep 17 00:00:00 2001 From: plougastela Date: Thu, 6 Apr 2023 13:20:15 +0200 Subject: [PATCH 07/15] CRPYT203 - Detect unoptimized image format --- .../python/checks/DetectUnoptimizedImageFormat.java | 9 ++++----- .../python/checks/DetectUnoptimizedImageFormatTest.java | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java index efbbd0b2f..644879979 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java @@ -13,17 +13,16 @@ @Rule( key = DetectUnoptimizedImageFormat.RULE_KEY, - name = "Detect unoptimized image format", + name = DetectUnoptimizedImageFormat.MESSAGERULE, description = DetectUnoptimizedImageFormat.MESSAGEERROR, priority = Priority.MINOR, tags = {"bug", "eco-design", "ecocode"}) public class DetectUnoptimizedImageFormat extends PythonSubscriptionCheck { - public static final String RULE_KEY = "EC203"; - + protected static final String RULE_KEY = "EC203"; protected static final String MESSAGERULE = "Detect unoptimized image format"; protected static final String MESSAGEERROR = "If possible, the utilisation of svg image format (or html tag) is recommended over other image format."; - protected static Pattern IMG_EXTENSION = Pattern.compile("\\.(bmp|ico|tiff|webp|png|jpg|jpeg|jfif|pjpeg|pjp|gif|avif|apng)"); + protected static Pattern IMGEXTENSION = Pattern.compile("\\.(bmp|ico|tiff|webp|png|jpg|jpeg|jfif|pjpeg|pjp|gif|avif|apng)"); @Override public void initialize(Context context) { @@ -34,7 +33,7 @@ public void visitNodeString(SubscriptionContext ctx) { if (ctx.syntaxNode().is(Tree.Kind.STRING_LITERAL)) { final StringLiteral stringLiteral = (StringLiteral) ctx.syntaxNode(); final String strValue = stringLiteral.trimmedQuotesValue(); - final Matcher matcher = IMG_EXTENSION.matcher(strValue); + final Matcher matcher = IMGEXTENSION.matcher(strValue); if(matcher.find()) { ctx.addIssue(stringLiteral, MESSAGEERROR); } diff --git a/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java index a65679236..814d280cf 100644 --- a/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java @@ -3,7 +3,7 @@ import org.junit.Test; import org.sonar.python.checks.utils.PythonCheckVerifier; -public class DetectUnoptimizedImageFormatTest { +class DetectUnoptimizedImageFormatTest { @Test public void test() { From 170ae16a788d0bdeb2754e58aad51105d515a777 Mon Sep 17 00:00:00 2001 From: plougastela Date: Thu, 6 Apr 2023 13:21:12 +0200 Subject: [PATCH 08/15] CRPYT203 - Detect unoptimized image format --- .../python/checks/DetectUnoptimizedImageFormat.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java index 644879979..098d00fcc 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java @@ -22,7 +22,7 @@ public class DetectUnoptimizedImageFormat extends PythonSubscriptionCheck { protected static final String RULE_KEY = "EC203"; protected static final String MESSAGERULE = "Detect unoptimized image format"; protected static final String MESSAGEERROR = "If possible, the utilisation of svg image format (or html tag) is recommended over other image format."; - protected static Pattern IMGEXTENSION = Pattern.compile("\\.(bmp|ico|tiff|webp|png|jpg|jpeg|jfif|pjpeg|pjp|gif|avif|apng)"); + protected static final Pattern IMGEXTENSION = Pattern.compile("\\.(bmp|ico|tiff|webp|png|jpg|jpeg|jfif|pjpeg|pjp|gif|avif|apng)"); @Override public void initialize(Context context) { From c708d03077ed22cac3bd5dd5672b3d1ccfd0b30f Mon Sep 17 00:00:00 2001 From: plougastela Date: Thu, 6 Apr 2023 13:26:04 +0200 Subject: [PATCH 09/15] CRPYT203 - Detect unoptimized image format --- .../fr/greencodeinitiative/l10n/python/rules/python/EC203.html | 1 + 1 file changed, 1 insertion(+) diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC203.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC203.html index a588eef95..78ef52f8e 100644 --- a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC203.html +++ b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC203.html @@ -16,6 +16,7 @@

+

Noncompliant Code Example

     ...

From f64dbef9ab2a4a3310ae93055e79233814376502 Mon Sep 17 00:00:00 2001
From: plougastela 
Date: Thu, 6 Apr 2023 13:46:36 +0200
Subject: [PATCH 10/15] CRPYT203 - Detect unoptimized image format

---
 .../python/PythonRuleRepository.java          | 10 +++----
 .../l10n/python/rules/python/EC203.html       | 26 +++++++++++++++++++
 2 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java
index 2a6f0225c..fb49a37f8 100644
--- a/python-plugin/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java
+++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java
@@ -29,12 +29,7 @@
 import java.util.List;
 import java.util.Map;
 
-import fr.greencodeinitiative.python.checks.AvoidFullSQLRequest;
-import fr.greencodeinitiative.python.checks.AvoidGettersAndSetters;
-import fr.greencodeinitiative.python.checks.AvoidGlobalVariableInFunctionCheck;
-import fr.greencodeinitiative.python.checks.AvoidSQLRequestInLoop;
-import fr.greencodeinitiative.python.checks.AvoidTryCatchFinallyCheck;
-import fr.greencodeinitiative.python.checks.NoFunctionCallWhenDeclaringForLoop;
+import fr.greencodeinitiative.python.checks.*;
 import org.apache.commons.lang.StringUtils;
 import org.sonar.api.server.rule.RulesDefinition;
 import org.sonar.api.server.rule.RulesDefinitionAnnotationLoader;
@@ -92,7 +87,8 @@ public List checkClasses() {
             AvoidSQLRequestInLoop.class,
             AvoidTryCatchFinallyCheck.class,
             NoFunctionCallWhenDeclaringForLoop.class,
-            AvoidFullSQLRequest.class
+            AvoidFullSQLRequest.class,
+            DetectUnoptimizedImageFormat.class
     );
   }
 
diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC203.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC203.html
index 78ef52f8e..65516edfc 100644
--- a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC203.html
+++ b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC203.html
@@ -29,6 +29,7 @@ 

Compliant Solution

img_svg = "image.svg" ...
+

Noncompliant Code Example

     public void foo() {
@@ -45,3 +46,28 @@ 

Compliant Solution

... }
+ +

Noncompliant Code Example

+
+    public void foo() {
+        ...
+        return '<html><img src="xx/xx/image.bmp"></html>'
+        ...
+    }
+
+

Compliant Solution

+
+    public void foo() {
+        ...
+        return '<html><img src="xx/xx/image.svg"></html>'
+        ...
+   }
+
+     public void foo2() {
+        ...
+        return ('<html><svg width="100" height="100">' +
+                '<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />' +
+                '</svg></html>')
+        ...
+   }
+
From 9aad7e72e652d5cf2bb13ab5fdadea1232cfbefc Mon Sep 17 00:00:00 2001 From: plougastela Date: Thu, 6 Apr 2023 13:57:04 +0200 Subject: [PATCH 11/15] CRPYT203 - Detect unoptimized image format --- .../python/checks/DetectUnoptimizedImageFormatTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java index 814d280cf..a65679236 100644 --- a/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java @@ -3,7 +3,7 @@ import org.junit.Test; import org.sonar.python.checks.utils.PythonCheckVerifier; -class DetectUnoptimizedImageFormatTest { +public class DetectUnoptimizedImageFormatTest { @Test public void test() { From 5eedcf92274448ddc6def1d74695c168c89a76b2 Mon Sep 17 00:00:00 2001 From: plougastela Date: Thu, 6 Apr 2023 14:00:32 +0200 Subject: [PATCH 12/15] CRPYT203 - Detect unoptimized image format --- .../python/checks/DetectUnoptimizedImageFormat.java | 1 - 1 file changed, 1 deletion(-) diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java index 098d00fcc..c680040da 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java @@ -1,6 +1,5 @@ package fr.greencodeinitiative.python.checks; -import com.sun.source.tree.LiteralTree; import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.python.api.PythonSubscriptionCheck; From 2c91015912c33e449a3e9bd9a883e104e8fe9ba6 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Sat, 20 May 2023 10:50:49 +0200 Subject: [PATCH 13/15] [PR 156] corrections from review notes --- CHANGELOG.md | 1 + RULES.md | 1 + .../python/checks/DetectUnoptimizedImageFormat.java | 2 +- .../python/checks/DetectUnoptimizedImageFormatTest.java | 2 +- ...plient.py => detectUnoptimizedImageFormatCompliant.py} | 8 ++++---- 5 files changed, 8 insertions(+), 6 deletions(-) rename python-plugin/src/test/resources/checks/{detectUnoptimizedImageFormatComplient.py => detectUnoptimizedImageFormatCompliant.py} (61%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24cc3c0de..f7cdf3969 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#113](https://github.com/green-code-initiative/ecoCode/issues/113) new PYTHON rule : Use unoptimized vector images - [#127](https://github.com/green-code-initiative/ecoCode/issues/127) Add Python rule EC404: Usage of generator comprehension instead of list comprehension in for loop declaration +- [#156](https://github.com/green-code-initiative/ecoCode/issues/127) Add Python rule EC404: Usage of generator comprehension instead of list comprehension in for loop declaration ### Changed diff --git a/RULES.md b/RULES.md index dbbc7871e..1837f959c 100644 --- a/RULES.md +++ b/RULES.md @@ -48,3 +48,4 @@ Some are applicable for different technologies. | EC5 | Usage of preparedStatement instead of Statement | SQL will only commit the query once, whereas if you used only one statement, it would commit the query every time and thus induce unnecessary calculations by the CPU and therefore superfluous energy consumption. | | βœ… | 🚫 | 🚫 | 🚫 | 🚫 | | EC27 | Usage of system.arraycopy to copy arrays | Programs spend most of the time in loops. These can be resource consuming, especially when they integrate heavy processing (IO access). Moreover, the size of the data and processing inside the loops will not allow full use of hardware mechanisms such as the cache or compiler optimization mechanisms. | | βœ… | 🚫 | 🚫 | 🚫 | 🚫 | | EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | βœ… | 🚫 | +| EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | πŸš€ | πŸš€ | πŸš€ | βœ… | πŸš€ | diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java index c680040da..5cd3c48c7 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java @@ -15,7 +15,7 @@ name = DetectUnoptimizedImageFormat.MESSAGERULE, description = DetectUnoptimizedImageFormat.MESSAGEERROR, priority = Priority.MINOR, - tags = {"bug", "eco-design", "ecocode"}) + tags = {"eco-design", "ecocode", "performance", "user-experience"}) public class DetectUnoptimizedImageFormat extends PythonSubscriptionCheck { protected static final String RULE_KEY = "EC203"; diff --git a/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java index a65679236..7c735f59f 100644 --- a/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java @@ -8,6 +8,6 @@ public class DetectUnoptimizedImageFormatTest { @Test public void test() { PythonCheckVerifier.verify("src/test/resources/checks/detectUnoptimizedImageFormat.py", new DetectUnoptimizedImageFormat()); - PythonCheckVerifier.verifyNoIssue("src/test/resources/checks/detectUnoptimizedImageFormatComplient.py", new DetectUnoptimizedImageFormat()); + PythonCheckVerifier.verifyNoIssue("src/test/resources/checks/detectUnoptimizedImageFormatCompliant.py", new DetectUnoptimizedImageFormat()); } } diff --git a/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatComplient.py b/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatCompliant.py similarity index 61% rename from python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatComplient.py rename to python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatCompliant.py index d05486a16..886c41a3e 100644 --- a/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatComplient.py +++ b/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatCompliant.py @@ -5,13 +5,13 @@ def testImage(image) : def testImageFormat2() : - img_svg = "test/image.svg" # Complient + img_svg = "test/image.svg" # Compliant - image_format = testImage("image.svg") # Complient + image_format = testImage("image.svg") # Compliant - image_svg_html = ('' + # Complient + image_svg_html = ('' + # Compliant '' + '') - return ('' # Complient + return ('' # Compliant + '' ) \ No newline at end of file From de95f4fc3e816d976b3fd0e184c77c4d1ba02628 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Sat, 20 May 2023 11:21:00 +0200 Subject: [PATCH 14/15] [Rule 203] correction CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7cdf3969..cb03809f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [#113](https://github.com/green-code-initiative/ecoCode/issues/113) new PYTHON rule : Use unoptimized vector images - [#127](https://github.com/green-code-initiative/ecoCode/issues/127) Add Python rule EC404: Usage of generator comprehension instead of list comprehension in for loop declaration -- [#156](https://github.com/green-code-initiative/ecoCode/issues/127) Add Python rule EC404: Usage of generator comprehension instead of list comprehension in for loop declaration +- [#192](https://github.com/green-code-initiative/ecoCode/pull/192) Add Python rule EC203: Detect unoptimized file formats ### Changed From d184350748ff508352369bcc0293ea6d6e1ecc44 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Mon, 22 May 2023 17:01:03 +0200 Subject: [PATCH 15/15] [Rule 203] correction files format --- .../src/test/resources/checks/avoidFullSQLRequest.py | 2 -- .../src/test/resources/checks/avoidGettersAndSetters.py | 2 +- .../src/test/resources/checks/avoidTryCatchFinallyCheck.py | 2 +- .../src/test/resources/checks/detectUnoptimizedImageFormat.py | 2 +- .../resources/checks/detectUnoptimizedImageFormatCompliant.py | 2 +- .../resources/checks/noFunctionCallWhenDeclaringForLoop.py | 4 +--- 6 files changed, 5 insertions(+), 9 deletions(-) diff --git a/python-plugin/src/test/resources/checks/avoidFullSQLRequest.py b/python-plugin/src/test/resources/checks/avoidFullSQLRequest.py index 217cb6c9a..66c6adb4c 100644 --- a/python-plugin/src/test/resources/checks/avoidFullSQLRequest.py +++ b/python-plugin/src/test/resources/checks/avoidFullSQLRequest.py @@ -8,5 +8,3 @@ def displayMessage(argument1): requestCompiliant = ' SeLeCt user FrOm myTable' displayMessage(requestNonCompiliant) displayMessage(requestCompiliant) - - diff --git a/python-plugin/src/test/resources/checks/avoidGettersAndSetters.py b/python-plugin/src/test/resources/checks/avoidGettersAndSetters.py index fd72053ad..538e79a79 100644 --- a/python-plugin/src/test/resources/checks/avoidGettersAndSetters.py +++ b/python-plugin/src/test/resources/checks/avoidGettersAndSetters.py @@ -23,4 +23,4 @@ def is_major(self): return self.age >= 18 def get_weight(self): # Noncompliant {{Avoid creating getter and setter methods in classes}} - return self.weight \ No newline at end of file + return self.weight diff --git a/python-plugin/src/test/resources/checks/avoidTryCatchFinallyCheck.py b/python-plugin/src/test/resources/checks/avoidTryCatchFinallyCheck.py index 1e24bb491..cbd081e0e 100644 --- a/python-plugin/src/test/resources/checks/avoidTryCatchFinallyCheck.py +++ b/python-plugin/src/test/resources/checks/avoidTryCatchFinallyCheck.py @@ -26,4 +26,4 @@ def boo(): if os.path.isfile(path): fh = open(path, 'r') print(fh.read()) - fh.close \ No newline at end of file + fh.close diff --git a/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormat.py b/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormat.py index 33e2145e9..37ddac218 100644 --- a/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormat.py +++ b/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormat.py @@ -33,4 +33,4 @@ def testImageFormat2() : + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} + '' # Noncompliant {{If possible, the utilisation of svg image format (or html tag) is recommended over other image format.}} - + '' ) \ No newline at end of file + + '' ) diff --git a/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatCompliant.py b/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatCompliant.py index 886c41a3e..205f125e7 100644 --- a/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatCompliant.py +++ b/python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatCompliant.py @@ -14,4 +14,4 @@ def testImageFormat2() : '') return ('' # Compliant - + '' ) \ No newline at end of file + + '' ) diff --git a/python-plugin/src/test/resources/checks/noFunctionCallWhenDeclaringForLoop.py b/python-plugin/src/test/resources/checks/noFunctionCallWhenDeclaringForLoop.py index a3755d3e3..97cb01e63 100644 --- a/python-plugin/src/test/resources/checks/noFunctionCallWhenDeclaringForLoop.py +++ b/python-plugin/src/test/resources/checks/noFunctionCallWhenDeclaringForLoop.py @@ -6,6 +6,4 @@ def my_function(): my_function() pass - - -my_function() \ No newline at end of file +my_function()