From cdcb3f2ae38871643a0e6c67585076d2ffd79f4b Mon Sep 17 00:00:00 2001 From: jycr Date: Wed, 12 Apr 2023 03:28:25 +0200 Subject: [PATCH 1/7] green-code-initiative/ecoCode#92 feat(rule): Creates module `ecocode-rules-specifications` --- ecocode-rules-specifications/README.md | 15 +++ ecocode-rules-specifications/pom.xml | 139 +++++++++++++++++++++++++ pom.xml | 1 + 3 files changed, 155 insertions(+) create mode 100644 ecocode-rules-specifications/README.md create mode 100644 ecocode-rules-specifications/pom.xml diff --git a/ecocode-rules-specifications/README.md b/ecocode-rules-specifications/README.md new file mode 100644 index 000000000..1ee24ba52 --- /dev/null +++ b/ecocode-rules-specifications/README.md @@ -0,0 +1,15 @@ +# ecoCode rules specification repository + +## Description + +This project contains the specifications of the ecoCode rules. + +All the existing rules can be found in the [rules folder](src/main/rules). + +## Description language + +The description of the rules uses the ASCIIDOC format (with [Markdown compatibility](https://docs.asciidoctor.org/asciidoc/latest/syntax-quick-reference/#markdown-compatibility)) in order to allow the inclusion of other pages (this feature is not available in standard with Markdown). + +See: +* [AsciiDoc Syntax Quick Reference](https://docs.asciidoctor.org/asciidoc/latest/syntax-quick-reference/) +* [Compare AsciiDoc to Markdown](https://docs.asciidoctor.org/asciidoc/latest/asciidoc-vs-markdown/) diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml new file mode 100644 index 000000000..a85c3934d --- /dev/null +++ b/ecocode-rules-specifications/pom.xml @@ -0,0 +1,139 @@ + + + 4.0.0 + + + io.ecocode + ecocode-parent + 1.3.1-SNAPSHOT + + + ecocode-rules-specifications + + ecoCode Rules Specifications repository + Repository that contains the specifications of every static-analysis rules available in ecoCode plugins. + https://github.com/green-code-initiative/ecoCode/tree/main/ecocode-rules-specifications + + + + org.sonarsource.sonarqube + sonar-plugin-api + provided + + + org.sonarsource.analyzer-commons + sonar-analyzer-commons + + + + org.junit.jupiter + junit-jupiter + test + + + + org.assertj + assertj-core + test + + + + + + + org.jacoco + jacoco-maven-plugin + + + prepare-agent + + prepare-agent + + + + report + + report + + + + + + + org.asciidoctor + asciidoctor-maven-plugin + 2.2.4 + + + convert-to-html + generate-resources + + process-asciidoc + + + ${project.basedir}/src/main/rules + ${project.build.directory}/rules + + coderay + style + + true + false + true + + + ERROR + + + + + + + + + org.apache.maven.plugins + maven-antrun-plugin + 3.1.0 + + + process-resources + + run + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 3.6.0 + + + + true + + + + + diff --git a/pom.xml b/pom.xml index 7b1750006..0a13b79b0 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,7 @@ + ecocode-rules-specifications python-plugin java-plugin javascript-plugin From a8d9f34cd3040b790aa7c7ddf882400fa932a3e2 Mon Sep 17 00:00:00 2001 From: jycr Date: Wed, 12 Apr 2023 03:28:58 +0200 Subject: [PATCH 2/7] green-code-initiative/ecoCode#92 refactor(rule/java): moves Java rules into `ecocode-rules-specifications` module --- ecocode-rules-specifications/pom.xml | 23 +++++++ .../src/main/assembly/java.xml | 18 +++++ .../src/main/rules/EC1}/EC1.json | 0 .../src/main/rules/EC1/java/EC1.asciidoc | 0 .../src/main/rules/EC2}/EC2.json | 0 .../src/main/rules/EC2/java/EC2.asciidoc | 0 .../src/main/rules/EC27}/EC27.json | 0 .../src/main/rules/EC27/java/EC27.asciidoc | 0 .../src/main/rules/EC28}/EC28.json | 0 .../src/main/rules/EC28/java/EC28.asciidoc | 0 .../src/main/rules/EC3}/EC3.json | 0 .../src/main/rules/EC3/java/EC3.asciidoc | 0 .../src/main/rules/EC32}/EC32.json | 0 .../src/main/rules/EC32/java/EC32.asciidoc | 0 .../src/main/rules/EC4}/EC4.json | 0 .../src/main/rules/EC4/java/EC4.asciidoc | 0 .../src/main/rules/EC5}/EC5.json | 0 .../src/main/rules/EC5/java/EC5.asciidoc | 0 .../src/main/rules/EC53}/EC53.json | 0 .../src/main/rules/EC53/java/EC53.asciidoc | 0 .../src/main/rules/EC63}/EC63.json | 0 .../src/main/rules/EC63/java/EC63.asciidoc | 0 .../src/main/rules/EC67}/EC67.json | 0 .../src/main/rules/EC67/java/EC67.asciidoc | 0 .../src/main/rules/EC69}/EC69.json | 0 .../src/main/rules/EC69/java/EC69.asciidoc | 0 .../src/main/rules/EC72}/EC72.json | 0 .../src/main/rules/EC72/java/EC72.asciidoc | 0 .../src/main/rules/EC74}/EC74.json | 0 .../src/main/rules/EC74/java/EC74.asciidoc | 0 .../src/main/rules/EC75}/EC75.json | 0 .../src/main/rules/EC75/java/EC75.asciidoc | 0 .../src/main/rules/EC76}/EC76.json | 0 .../src/main/rules/EC76/java/EC76.asciidoc | 0 .../src/main/rules/EC77}/EC77.json | 0 .../src/main/rules/EC77/java/EC77.asciidoc | 0 .../src/main/rules/EC78}/EC78.json | 0 .../src/main/rules/EC78/java/EC78.asciidoc | 0 .../src/main/rules/EC79}/EC79.json | 0 .../src/main/rules/EC79/java/EC79.asciidoc | 0 java-plugin/pom.xml | 13 ++-- .../java/JavaCheckRegistrar.java | 45 ++++++++++++- .../java/JavaRulesDefinition.java | 32 +++------ .../greencodeinitiative/java/RulesList.java | 66 ------------------- .../java/JavaPluginTest.java | 12 ++-- .../java/JavaRulesDefinitionTest.java | 65 +++++++++++++----- 46 files changed, 156 insertions(+), 118 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/assembly/java.xml rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC1}/EC1.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC1.html => ecocode-rules-specifications/src/main/rules/EC1/java/EC1.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC2}/EC2.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC2.html => ecocode-rules-specifications/src/main/rules/EC2/java/EC2.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC27}/EC27.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC27.html => ecocode-rules-specifications/src/main/rules/EC27/java/EC27.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC28}/EC28.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC28.html => ecocode-rules-specifications/src/main/rules/EC28/java/EC28.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC3}/EC3.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC3.html => ecocode-rules-specifications/src/main/rules/EC3/java/EC3.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC32}/EC32.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC32.html => ecocode-rules-specifications/src/main/rules/EC32/java/EC32.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC4}/EC4.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC4.html => ecocode-rules-specifications/src/main/rules/EC4/java/EC4.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC5}/EC5.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC5.html => ecocode-rules-specifications/src/main/rules/EC5/java/EC5.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC53}/EC53.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC53.html => ecocode-rules-specifications/src/main/rules/EC53/java/EC53.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC63}/EC63.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC63.html => ecocode-rules-specifications/src/main/rules/EC63/java/EC63.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC67}/EC67.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC67.html => ecocode-rules-specifications/src/main/rules/EC67/java/EC67.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC69}/EC69.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC69.html => ecocode-rules-specifications/src/main/rules/EC69/java/EC69.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC72}/EC72.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC72.html => ecocode-rules-specifications/src/main/rules/EC72/java/EC72.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC74}/EC74.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC74.html => ecocode-rules-specifications/src/main/rules/EC74/java/EC74.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC75}/EC75.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC75.html => ecocode-rules-specifications/src/main/rules/EC75/java/EC75.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC76}/EC76.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC76.html => ecocode-rules-specifications/src/main/rules/EC76/java/EC76.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC77}/EC77.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC77.html => ecocode-rules-specifications/src/main/rules/EC77/java/EC77.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC78}/EC78.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC78.html => ecocode-rules-specifications/src/main/rules/EC78/java/EC78.asciidoc (100%) rename {java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java => ecocode-rules-specifications/src/main/rules/EC79}/EC79.json (100%) rename java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC79.html => ecocode-rules-specifications/src/main/rules/EC79/java/EC79.asciidoc (100%) delete mode 100644 java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index a85c3934d..99ed2b78d 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -110,6 +110,17 @@ run + + + + + + + + + + + @@ -129,6 +140,18 @@ maven-assembly-plugin 3.6.0 + + assembly-java + prepare-package + + single + + + + ${project.basedir}/src/main/assembly/java.xml + + + true diff --git a/ecocode-rules-specifications/src/main/assembly/java.xml b/ecocode-rules-specifications/src/main/assembly/java.xml new file mode 100644 index 000000000..dc9d8bd3c --- /dev/null +++ b/ecocode-rules-specifications/src/main/assembly/java.xml @@ -0,0 +1,18 @@ + + java + + jar + + false + + + ${project.build.outputDirectory} + + io/ecocode/rules/java/*.* + + + + + diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC1.json b/ecocode-rules-specifications/src/main/rules/EC1/EC1.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC1.json rename to ecocode-rules-specifications/src/main/rules/EC1/EC1.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC1.html b/ecocode-rules-specifications/src/main/rules/EC1/java/EC1.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC1.html rename to ecocode-rules-specifications/src/main/rules/EC1/java/EC1.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC2.json b/ecocode-rules-specifications/src/main/rules/EC2/EC2.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC2.json rename to ecocode-rules-specifications/src/main/rules/EC2/EC2.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC2.html b/ecocode-rules-specifications/src/main/rules/EC2/java/EC2.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC2.html rename to ecocode-rules-specifications/src/main/rules/EC2/java/EC2.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC27.json b/ecocode-rules-specifications/src/main/rules/EC27/EC27.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC27.json rename to ecocode-rules-specifications/src/main/rules/EC27/EC27.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC27.html b/ecocode-rules-specifications/src/main/rules/EC27/java/EC27.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC27.html rename to ecocode-rules-specifications/src/main/rules/EC27/java/EC27.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC28.json b/ecocode-rules-specifications/src/main/rules/EC28/EC28.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC28.json rename to ecocode-rules-specifications/src/main/rules/EC28/EC28.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC28.html b/ecocode-rules-specifications/src/main/rules/EC28/java/EC28.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC28.html rename to ecocode-rules-specifications/src/main/rules/EC28/java/EC28.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC3.json b/ecocode-rules-specifications/src/main/rules/EC3/EC3.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC3.json rename to ecocode-rules-specifications/src/main/rules/EC3/EC3.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC3.html b/ecocode-rules-specifications/src/main/rules/EC3/java/EC3.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC3.html rename to ecocode-rules-specifications/src/main/rules/EC3/java/EC3.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC32.json b/ecocode-rules-specifications/src/main/rules/EC32/EC32.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC32.json rename to ecocode-rules-specifications/src/main/rules/EC32/EC32.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC32.html b/ecocode-rules-specifications/src/main/rules/EC32/java/EC32.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC32.html rename to ecocode-rules-specifications/src/main/rules/EC32/java/EC32.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC4.json b/ecocode-rules-specifications/src/main/rules/EC4/EC4.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC4.json rename to ecocode-rules-specifications/src/main/rules/EC4/EC4.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC4.html b/ecocode-rules-specifications/src/main/rules/EC4/java/EC4.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC4.html rename to ecocode-rules-specifications/src/main/rules/EC4/java/EC4.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC5.json b/ecocode-rules-specifications/src/main/rules/EC5/EC5.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC5.json rename to ecocode-rules-specifications/src/main/rules/EC5/EC5.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC5.html b/ecocode-rules-specifications/src/main/rules/EC5/java/EC5.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC5.html rename to ecocode-rules-specifications/src/main/rules/EC5/java/EC5.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC53.json b/ecocode-rules-specifications/src/main/rules/EC53/EC53.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC53.json rename to ecocode-rules-specifications/src/main/rules/EC53/EC53.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC53.html b/ecocode-rules-specifications/src/main/rules/EC53/java/EC53.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC53.html rename to ecocode-rules-specifications/src/main/rules/EC53/java/EC53.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC63.json b/ecocode-rules-specifications/src/main/rules/EC63/EC63.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC63.json rename to ecocode-rules-specifications/src/main/rules/EC63/EC63.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC63.html b/ecocode-rules-specifications/src/main/rules/EC63/java/EC63.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC63.html rename to ecocode-rules-specifications/src/main/rules/EC63/java/EC63.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC67.json b/ecocode-rules-specifications/src/main/rules/EC67/EC67.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC67.json rename to ecocode-rules-specifications/src/main/rules/EC67/EC67.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC67.html b/ecocode-rules-specifications/src/main/rules/EC67/java/EC67.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC67.html rename to ecocode-rules-specifications/src/main/rules/EC67/java/EC67.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC69.json b/ecocode-rules-specifications/src/main/rules/EC69/EC69.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC69.json rename to ecocode-rules-specifications/src/main/rules/EC69/EC69.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC69.html b/ecocode-rules-specifications/src/main/rules/EC69/java/EC69.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC69.html rename to ecocode-rules-specifications/src/main/rules/EC69/java/EC69.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC72.json b/ecocode-rules-specifications/src/main/rules/EC72/EC72.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC72.json rename to ecocode-rules-specifications/src/main/rules/EC72/EC72.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC72.html b/ecocode-rules-specifications/src/main/rules/EC72/java/EC72.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC72.html rename to ecocode-rules-specifications/src/main/rules/EC72/java/EC72.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC74.json b/ecocode-rules-specifications/src/main/rules/EC74/EC74.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC74.json rename to ecocode-rules-specifications/src/main/rules/EC74/EC74.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC74.html b/ecocode-rules-specifications/src/main/rules/EC74/java/EC74.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC74.html rename to ecocode-rules-specifications/src/main/rules/EC74/java/EC74.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC75.json b/ecocode-rules-specifications/src/main/rules/EC75/EC75.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC75.json rename to ecocode-rules-specifications/src/main/rules/EC75/EC75.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC75.html b/ecocode-rules-specifications/src/main/rules/EC75/java/EC75.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC75.html rename to ecocode-rules-specifications/src/main/rules/EC75/java/EC75.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC76.json b/ecocode-rules-specifications/src/main/rules/EC76/EC76.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC76.json rename to ecocode-rules-specifications/src/main/rules/EC76/EC76.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC76.html b/ecocode-rules-specifications/src/main/rules/EC76/java/EC76.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC76.html rename to ecocode-rules-specifications/src/main/rules/EC76/java/EC76.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC77.json b/ecocode-rules-specifications/src/main/rules/EC77/EC77.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC77.json rename to ecocode-rules-specifications/src/main/rules/EC77/EC77.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC77.html b/ecocode-rules-specifications/src/main/rules/EC77/java/EC77.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC77.html rename to ecocode-rules-specifications/src/main/rules/EC77/java/EC77.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC78.json b/ecocode-rules-specifications/src/main/rules/EC78/EC78.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC78.json rename to ecocode-rules-specifications/src/main/rules/EC78/EC78.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC78.html b/ecocode-rules-specifications/src/main/rules/EC78/java/EC78.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC78.html rename to ecocode-rules-specifications/src/main/rules/EC78/java/EC78.asciidoc diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC79.json b/ecocode-rules-specifications/src/main/rules/EC79/EC79.json similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC79.json rename to ecocode-rules-specifications/src/main/rules/EC79/EC79.json diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC79.html b/ecocode-rules-specifications/src/main/rules/EC79/java/EC79.asciidoc similarity index 100% rename from java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC79.html rename to ecocode-rules-specifications/src/main/rules/EC79/java/EC79.asciidoc diff --git a/java-plugin/pom.xml b/java-plugin/pom.xml index 0a956e82e..2f4e2476b 100644 --- a/java-plugin/pom.xml +++ b/java-plugin/pom.xml @@ -16,21 +16,24 @@ https://github.com/green-code-initiative/ecoCode/tree/main/java-plugin + + ${project.groupId} + ecocode-rules-specifications + ${project.version} + java + org.sonarsource.java sonar-java-plugin sonar-plugin + provided org.sonarsource.sonarqube sonar-plugin-api - - - - org.sonarsource.analyzer-commons - sonar-analyzer-commons + provided diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java index 5705e6ec5..5ca219399 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaCheckRegistrar.java @@ -16,8 +16,28 @@ */ package fr.greencodeinitiative.java; +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 org.sonar.plugins.java.api.CheckRegistrar; import org.sonar.plugins.java.api.JavaCheck; import org.sonarsource.api.sonarlint.SonarLintSide; @@ -30,6 +50,27 @@ */ @SonarLintSide public class JavaCheckRegistrar implements CheckRegistrar { + private static final List> ANNOTATED_RULE_CLASSES = List.of( + ArrayCopyCheck.class, + IncrementCheck.class, + AvoidConcatenateStringsInLoop.class, + AvoidUsageOfStaticCollections.class, + AvoidGettingSizeCollectionInLoop.class, + AvoidRegexPatternNotStatic.class, + NoFunctionCallWhenDeclaringForLoop.class, + AvoidStatementForDMLQueries.class, + AvoidSpringRepositoryCallInLoopCheck.class, + AvoidSQLRequestInLoop.class, + AvoidFullSQLRequest.class, + UseCorrectForLoop.class, + UnnecessarilyAssignValuesToVariables.class, + OptimizeReadFileExceptions.class, + InitializeBufferWithAppropriateSize.class, + AvoidUsingGlobalVariablesCheck.class, + AvoidSetConstantInBatchUpdate.class, + FreeResourcesOfAutoCloseableInterface.class, + AvoidMultipleIfElseStatement.class + ); /** * Register the classes that will be used to instantiate checks during analysis. @@ -44,13 +85,13 @@ public void register(RegistrarContext registrarContext) { * Lists all the main checks provided by the plugin */ public static List> checkClasses() { - return RulesList.getJavaChecks(); + return ANNOTATED_RULE_CLASSES; } /** * Lists all the test checks provided by the plugin */ public static List> testCheckClasses() { - return RulesList.getJavaTestChecks(); + return Collections.emptyList(); } } diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java index 56324e89e..e8f7cccd3 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/JavaRulesDefinition.java @@ -16,30 +16,22 @@ */ package fr.greencodeinitiative.java; +import java.util.ArrayList; + import org.sonar.api.SonarRuntime; import org.sonar.api.server.rule.RulesDefinition; import org.sonarsource.analyzer.commons.RuleMetadataLoader; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Objects; -import java.util.Set; - /** * Declare rule metadata in server repository of rules. * That allows to list the rules in the page "Rules". */ public class JavaRulesDefinition implements RulesDefinition { + private static final String RESOURCE_BASE_PATH = "io/ecocode/rules/java"; - // don't change that because the path is hard coded in CheckVerifier - private static final String RESOURCE_BASE_PATH = "fr/greencodeinitiative/l10n/java/rules/java"; - - - // Add the rule keys of the rules which need to be considered as template-rules - private static final Set RULE_TEMPLATES_KEY = Collections.emptySet(); - public static final String NAME = "ecoCode"; - public static final String LANGUAGE = "java"; - public static final String REPOSITORY_KEY = "ecocode-java"; + private static final String NAME = "ecoCode"; + private static final String LANGUAGE = "java"; + static final String REPOSITORY_KEY = "ecocode-java"; private final SonarRuntime sonarRuntime; @@ -53,17 +45,11 @@ public void define(Context context) { RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH, sonarRuntime); - ruleMetadataLoader.addRulesByAnnotatedClass(repository, new ArrayList<>(RulesList.getChecks())); - - setTemplates(repository); - + ruleMetadataLoader.addRulesByAnnotatedClass(repository, new ArrayList<>(JavaCheckRegistrar.checkClasses())); repository.done(); } - private static void setTemplates(NewRepository repository) { - RULE_TEMPLATES_KEY.stream() - .map(repository::rule) - .filter(Objects::nonNull) - .forEach(rule -> rule.setTemplate(true)); + public String repositoryKey() { + return REPOSITORY_KEY; } } diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java deleted file mode 100644 index 850e141c7..000000000 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/RulesList.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (C) 2023 Green Code Initiative - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser 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 - * Lesser 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; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -import fr.greencodeinitiative.java.checks.*; -import org.sonar.plugins.java.api.JavaCheck; - -public final class RulesList { - - private RulesList() { - } - - public static List> getChecks() { - List> checks = new ArrayList<>(); - checks.addAll(getJavaChecks()); - checks.addAll(getJavaTestChecks()); - return Collections.unmodifiableList(checks); - } - - public static List> getJavaChecks() { - return Collections.unmodifiableList(Arrays.asList( - ArrayCopyCheck.class, - IncrementCheck.class, - AvoidConcatenateStringsInLoop.class, - AvoidUsageOfStaticCollections.class, - AvoidGettingSizeCollectionInLoop.class, - AvoidRegexPatternNotStatic.class, - NoFunctionCallWhenDeclaringForLoop.class, - AvoidStatementForDMLQueries.class, - AvoidSpringRepositoryCallInLoopCheck.class, - AvoidSQLRequestInLoop.class, - AvoidFullSQLRequest.class, - UseCorrectForLoop.class, - UnnecessarilyAssignValuesToVariables.class, - OptimizeReadFileExceptions.class, - InitializeBufferWithAppropriateSize.class, - AvoidUsingGlobalVariablesCheck.class, - AvoidSetConstantInBatchUpdate.class, - FreeResourcesOfAutoCloseableInterface.class, - AvoidMultipleIfElseStatement.class - )); - } - - public static List> getJavaTestChecks() { - return Collections.emptyList(); - } -} diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaPluginTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaPluginTest.java index c925ece4e..3d09e5b0e 100644 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaPluginTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaPluginTest.java @@ -16,6 +16,7 @@ */ package fr.greencodeinitiative.java; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.sonar.api.Plugin; import org.sonar.api.SonarRuntime; @@ -24,14 +25,17 @@ import static org.mockito.Mockito.mock; class JavaPluginTest { + private Plugin.Context context; - @Test - void testName() { + @BeforeEach + void init() { SonarRuntime sonarRuntime = mock(SonarRuntime.class); - Plugin.Context context = new Plugin.Context(sonarRuntime); - + context = new Plugin.Context(sonarRuntime); new JavaPlugin().define(context); + } + @Test + void test() { assertThat(context.getExtensions()).hasSize(2); } diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java index 20aabc21c..bb6f796d3 100644 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/JavaRulesDefinitionTest.java @@ -24,38 +24,65 @@ import org.sonar.api.rules.RuleType; import org.sonar.api.server.debt.DebtRemediationFunction.Type; import org.sonar.api.server.rule.RulesDefinition; -import org.sonar.api.server.rule.RulesDefinition.Param; -import org.sonar.api.server.rule.RulesDefinition.Repository; import org.sonar.api.server.rule.RulesDefinition.Rule; import org.sonar.api.utils.Version; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; class JavaRulesDefinitionTest { private RulesDefinition.Repository repository; + private RulesDefinition.Context context; @BeforeEach void init() { - SonarRuntime sonarRuntime = mock(SonarRuntime.class); + // TODO: Remove this check after Git repo split + /* + On an IDE (like IntelliJ), if the developer runs the unit tests without building/generating the Maven goals on the + "ecocode-rules-specifications" module before, the unit tests will not see the generated HTML descriptions (from ASCIIDOC files). + The developer must therefore configure his IDE to build the `ecocode-rules-specifications` module before launching the Tests. + + When the `java-plugin` submodule is in a specific Git repository, `ecocode-rules-specifications` will be fetched from a classic + external Maven dependency. There will therefore no longer be any need to perform this specific configuration. + */ + if (JavaRulesDefinition.class.getResource("/io/ecocode/rules/java/EC4.json") == null) { + String message = "'ecocode-rules-specification' resources corrupted. Please check build of 'ecocode-rules-specification' module"; + if (System.getProperties().keySet().stream().anyMatch(k -> k.toString().startsWith("idea."))) { + message += "\n\nOn 'IntelliJ IDEA':" + + "\n1. go to settings :" + + "\n > Build, Execution, Deployment > Build Tools > Maven > Runner" + + "\n2. check option:" + + "\n > Delegate IDE build/run actions to Maven" + + "\n3. Click on menu: " + + "\n > Build > Build Project" + ; + } + fail(message); + } + + final SonarRuntime sonarRuntime = mock(SonarRuntime.class); doReturn(Version.create(0, 0)).when(sonarRuntime).getApiVersion(); JavaRulesDefinition rulesDefinition = new JavaRulesDefinition(sonarRuntime); RulesDefinition.Context context = new RulesDefinition.Context(); rulesDefinition.define(context); - repository = context.repository(JavaRulesDefinition.REPOSITORY_KEY); + repository = context.repository(rulesDefinition.repositoryKey()); } @Test - void test() { - assertThat(repository.name()).isEqualTo(JavaRulesDefinition.NAME); - assertThat(repository.language()).isEqualTo(JavaRulesDefinition.LANGUAGE); - assertThat(repository.rules()).hasSize(RulesList.getChecks().size()); - assertThat(repository.rules().stream().filter(Rule::template)).isEmpty(); + @DisplayName("Test repository metadata") + void testMetadata() { + assertThat(repository.name()).isEqualTo("ecoCode"); + assertThat(repository.language()).isEqualTo("java"); + assertThat(repository.key()).isEqualTo("ecocode-java"); + assertThat(repository.rules()).hasSize(19); + } - assertRuleProperties(repository); - assertAllRuleParametersHaveDescription(repository); + @Test + void testRegistredRules() { + assertThat(repository.rules()).hasSize(19); } @Test @@ -68,7 +95,8 @@ void testRuleKeyPrefix() { assertions.assertAll(); } - private static void assertRuleProperties(Repository repository) { + @Test + void assertRuleProperties() { Rule rule = repository.rule("EC67"); assertThat(rule).isNotNull(); assertThat(rule.name()).isEqualTo("Use ++i instead of i++"); @@ -76,12 +104,13 @@ private static void assertRuleProperties(Repository repository) { assertThat(rule.type()).isEqualTo(RuleType.CODE_SMELL); } - private static void assertAllRuleParametersHaveDescription(Repository repository) { - for (Rule rule : repository.rules()) { - for (Param param : rule.params()) { - assertThat(param.description()).as("description for " + param.key()).isNotEmpty(); - } - } + @Test + void testAllRuleParametersHaveDescription() { + SoftAssertions assertions = new SoftAssertions(); + repository.rules().stream() + .flatMap(rule -> rule.params().stream()) + .forEach(param -> assertions.assertThat(param.description()).as("description for " + param.key()).isNotEmpty()); + assertions.assertAll(); } } From 080e79bf6ca14e101dec6ade8a91f19c450f7499 Mon Sep 17 00:00:00 2001 From: jycr Date: Fri, 14 Apr 2023 18:22:49 +0200 Subject: [PATCH 3/7] green-code-initiative/ecoCode#92 refactor(rule/php): moves PHP rules into `ecocode-rules-specifications` module --- ecocode-rules-specifications/pom.xml | 19 ++++ .../src/main/assembly/php.xml | 18 ++++ .../src/main/rules/EC22/EC22.json | 15 ++++ .../src/main/rules/EC22/php/EC22.asciidoc | 0 .../src/main/rules/EC3/php/EC3.asciidoc | 0 .../src/main/rules/EC34/EC34.json | 15 ++++ .../src/main/rules/EC34/php/EC34.asciidoc | 0 .../src/main/rules/EC4/php/EC4.asciidoc | 0 .../src/main/rules/EC66/EC66.json | 15 ++++ .../src/main/rules/EC66/php/EC66.asciidoc | 0 .../src/main/rules/EC67/php/EC67.asciidoc | 0 .../src/main/rules/EC69/php/EC69.asciidoc | 0 .../src/main/rules/EC72/php/EC72.asciidoc | 0 .../src/main/rules/EC74/php/EC74.asciidoc | 0 php-plugin/pom.xml | 28 +++++- .../php/PhpRuleRepository.java | 73 ++++----------- .../php/PhpPluginTest.java | 16 ++-- .../php/PhpRuleRepositoryTest.java | 90 ++++++++++++++----- 18 files changed, 202 insertions(+), 87 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/assembly/php.xml create mode 100644 ecocode-rules-specifications/src/main/rules/EC22/EC22.json rename php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC22.html => ecocode-rules-specifications/src/main/rules/EC22/php/EC22.asciidoc (100%) rename php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC3.html => ecocode-rules-specifications/src/main/rules/EC3/php/EC3.asciidoc (100%) create mode 100644 ecocode-rules-specifications/src/main/rules/EC34/EC34.json rename php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC34.html => ecocode-rules-specifications/src/main/rules/EC34/php/EC34.asciidoc (100%) rename php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC4.html => ecocode-rules-specifications/src/main/rules/EC4/php/EC4.asciidoc (100%) create mode 100644 ecocode-rules-specifications/src/main/rules/EC66/EC66.json rename php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC66.html => ecocode-rules-specifications/src/main/rules/EC66/php/EC66.asciidoc (100%) rename php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC67.html => ecocode-rules-specifications/src/main/rules/EC67/php/EC67.asciidoc (100%) rename php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC69.html => ecocode-rules-specifications/src/main/rules/EC69/php/EC69.asciidoc (100%) rename php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC72.html => ecocode-rules-specifications/src/main/rules/EC72/php/EC72.asciidoc (100%) rename php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC74.html => ecocode-rules-specifications/src/main/rules/EC74/php/EC74.asciidoc (100%) diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index 99ed2b78d..8c53609a4 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -119,6 +119,13 @@ + + + + + + + @@ -152,6 +159,18 @@ + + assembly-php + prepare-package + + single + + + + ${project.basedir}/src/main/assembly/php.xml + + + true diff --git a/ecocode-rules-specifications/src/main/assembly/php.xml b/ecocode-rules-specifications/src/main/assembly/php.xml new file mode 100644 index 000000000..83eb07521 --- /dev/null +++ b/ecocode-rules-specifications/src/main/assembly/php.xml @@ -0,0 +1,18 @@ + + php + + jar + + false + + + ${project.build.outputDirectory} + + io/ecocode/rules/php/*.* + + + + + diff --git a/ecocode-rules-specifications/src/main/rules/EC22/EC22.json b/ecocode-rules-specifications/src/main/rules/EC22/EC22.json new file mode 100644 index 000000000..9d3ed8c36 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC22/EC22.json @@ -0,0 +1,15 @@ +{ + "title": "Use of methods for basic operations", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "eco-design", + "performance", + "ecocode" + ], + "defaultSeverity": "Minor" +} diff --git a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC22.html b/ecocode-rules-specifications/src/main/rules/EC22/php/EC22.asciidoc similarity index 100% rename from php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC22.html rename to ecocode-rules-specifications/src/main/rules/EC22/php/EC22.asciidoc diff --git a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC3.html b/ecocode-rules-specifications/src/main/rules/EC3/php/EC3.asciidoc similarity index 100% rename from php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC3.html rename to ecocode-rules-specifications/src/main/rules/EC3/php/EC3.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules/EC34/EC34.json b/ecocode-rules-specifications/src/main/rules/EC34/EC34.json new file mode 100644 index 000000000..4191cbb71 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC34/EC34.json @@ -0,0 +1,15 @@ +{ + "title": "Avoid using try-catch-finally statement", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "eco-design", + "performance", + "ecocode" + ], + "defaultSeverity": "Minor" +} diff --git a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC34.html b/ecocode-rules-specifications/src/main/rules/EC34/php/EC34.asciidoc similarity index 100% rename from php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC34.html rename to ecocode-rules-specifications/src/main/rules/EC34/php/EC34.asciidoc diff --git a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC4.html b/ecocode-rules-specifications/src/main/rules/EC4/php/EC4.asciidoc similarity index 100% rename from php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC4.html rename to ecocode-rules-specifications/src/main/rules/EC4/php/EC4.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules/EC66/EC66.json b/ecocode-rules-specifications/src/main/rules/EC66/EC66.json new file mode 100644 index 000000000..6ed6137dc --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC66/EC66.json @@ -0,0 +1,15 @@ +{ + "title": "Avoid using double quote (\"), prefer using simple quote (')", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "eco-design", + "performance", + "ecocode" + ], + "defaultSeverity": "Minor" +} diff --git a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC66.html b/ecocode-rules-specifications/src/main/rules/EC66/php/EC66.asciidoc similarity index 100% rename from php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC66.html rename to ecocode-rules-specifications/src/main/rules/EC66/php/EC66.asciidoc diff --git a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC67.html b/ecocode-rules-specifications/src/main/rules/EC67/php/EC67.asciidoc similarity index 100% rename from php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC67.html rename to ecocode-rules-specifications/src/main/rules/EC67/php/EC67.asciidoc diff --git a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC69.html b/ecocode-rules-specifications/src/main/rules/EC69/php/EC69.asciidoc similarity index 100% rename from php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC69.html rename to ecocode-rules-specifications/src/main/rules/EC69/php/EC69.asciidoc diff --git a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC72.html b/ecocode-rules-specifications/src/main/rules/EC72/php/EC72.asciidoc similarity index 100% rename from php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC72.html rename to ecocode-rules-specifications/src/main/rules/EC72/php/EC72.asciidoc diff --git a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC74.html b/ecocode-rules-specifications/src/main/rules/EC74/php/EC74.asciidoc similarity index 100% rename from php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC74.html rename to ecocode-rules-specifications/src/main/rules/EC74/php/EC74.asciidoc diff --git a/php-plugin/pom.xml b/php-plugin/pom.xml index 29f2e31b3..c034e7e3b 100644 --- a/php-plugin/pom.xml +++ b/php-plugin/pom.xml @@ -16,16 +16,24 @@ https://github.com/green-code-initiative/ecoCode/tree/main/php-plugin + + ${project.groupId} + ecocode-rules-specifications + ${project.version} + php + org.sonarsource.php sonar-php-plugin sonar-plugin + provided org.sonarsource.sonarqube sonar-plugin-api + provided @@ -35,6 +43,19 @@ + + + org.sonarsource.java + java-checks-testkit + test + + + + org.junit.jupiter + junit-jupiter + test + + org.assertj assertj-core @@ -62,10 +83,13 @@ true ${sonarqube.version} ${java.version} - ${sonarqube.version} - ${java.version} + + + org.apache.maven.plugins + maven-shade-plugin + org.apache.maven.plugins maven-dependency-plugin diff --git a/php-plugin/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java index d0fd50e53..9c37e58e3 100644 --- a/php-plugin/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/PhpRuleRepository.java @@ -16,14 +16,7 @@ */ package fr.greencodeinitiative.php; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; import java.util.List; -import java.util.Map; import fr.greencodeinitiative.php.checks.AvoidGettingSizeCollectionInLoopCheck; import fr.greencodeinitiative.php.checks.AvoidDoubleQuoteCheck; @@ -34,48 +27,29 @@ import fr.greencodeinitiative.php.checks.IncrementCheck; import fr.greencodeinitiative.php.checks.NoFunctionCallWhenDeclaringForLoop; import fr.greencodeinitiative.php.checks.UseOfMethodsForBasicOperations; -import org.sonar.api.rules.RuleType; +import org.sonar.api.SonarRuntime; import org.sonar.api.server.rule.RulesDefinition; -import org.sonar.api.server.rule.RulesDefinitionAnnotationLoader; import org.sonar.plugins.php.api.visitors.PHPCustomRuleRepository; +import org.sonarsource.analyzer.commons.RuleMetadataLoader; public class PhpRuleRepository implements RulesDefinition, PHPCustomRuleRepository { - public static final String LANGUAGE = "php"; - public static final String NAME = "ecoCode"; - public static final String RESOURCE_BASE_PATH = "/fr/greencodeinitiative/l10n/php/rules/custom/"; - public static final String REPOSITORY_KEY = "ecocode-php"; + private static final String LANGUAGE = "php"; + private static final String NAME = "ecoCode"; + private static final String RESOURCE_BASE_PATH = "io/ecocode/rules/php"; + private static final String REPOSITORY_KEY = "ecocode-php"; - @Override - public void define(Context context) { - NewRepository repository = context.createRepository(repositoryKey(), LANGUAGE).setName(NAME); - - new RulesDefinitionAnnotationLoader().load(repository, checkClasses().toArray(new Class[] {})); - - // technical debt - Map remediationCosts = new HashMap<>(); - remediationCosts.put(AvoidSQLRequestInLoopCheck.RULE_KEY, "10min"); - remediationCosts.put(AvoidFullSQLRequestCheck.RULE_KEY, "20min"); - repository.rules().forEach(rule -> { - rule.setType(RuleType.CODE_SMELL); - String debt = remediationCosts.get(rule.key()); - - // TODO DDC : create support to use org.apache.commons.lang.StringUtils -// if (StringUtils.isBlank(debt)) { - if (debt == null || debt.trim().equals("")) { - // default debt to 5min for issue correction - rule.setDebtRemediationFunction( - rule.debtRemediationFunctions().constantPerIssue("5min")); - } else { - rule.setDebtRemediationFunction( - rule.debtRemediationFunctions().constantPerIssue(debt)); - } - }); + private final SonarRuntime sonarRuntime; - // HTML description - repository.rules().forEach(rule -> - rule.setHtmlDescription(loadResource(RESOURCE_BASE_PATH + rule.key() + ".html"))); + public PhpRuleRepository(SonarRuntime sonarRuntime) { + this.sonarRuntime = sonarRuntime; + } + @Override + public void define(Context context) { + NewRepository repository = context.createRepository(REPOSITORY_KEY, LANGUAGE).setName(NAME); + RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH, sonarRuntime); + ruleMetadataLoader.addRulesByAnnotatedClass(repository, checkClasses()); repository.done(); } @@ -98,21 +72,4 @@ public List> checkClasses() { UseOfMethodsForBasicOperations.class ); } - - private String loadResource(String path) { - URL resource = getClass().getResource(path); - if (resource == null) { - throw new IllegalStateException("Resource not found: " + path); - } - ByteArrayOutputStream result = new ByteArrayOutputStream(); - try (InputStream in = resource.openStream()) { - byte[] buffer = new byte[1024]; - for (int len = in.read(buffer); len != -1; len = in.read(buffer)) { - result.write(buffer, 0, len); - } - return new String(result.toByteArray(), StandardCharsets.UTF_8); - } catch (IOException e) { - throw new IllegalStateException("Failed to read resource: " + path, e); - } - } } diff --git a/php-plugin/src/test/java/fr/greencodeinitiative/php/PhpPluginTest.java b/php-plugin/src/test/java/fr/greencodeinitiative/php/PhpPluginTest.java index 33736bc4c..9139c7c4e 100644 --- a/php-plugin/src/test/java/fr/greencodeinitiative/php/PhpPluginTest.java +++ b/php-plugin/src/test/java/fr/greencodeinitiative/php/PhpPluginTest.java @@ -16,20 +16,26 @@ */ package fr.greencodeinitiative.php; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.sonar.api.Plugin; import org.sonar.api.SonarRuntime; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -public class PhpPluginTest { +class PhpPluginTest { + private Plugin.Context context; - @Test - public void test() { + @BeforeEach + void init() { SonarRuntime sonarRuntime = mock(SonarRuntime.class); - Plugin.Context context = new Plugin.Context(sonarRuntime); + context = new Plugin.Context(sonarRuntime); new PHPPlugin().define(context); + } + + @Test + void test() { assertThat(context.getExtensions()).hasSize(1); } diff --git a/php-plugin/src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java b/php-plugin/src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java index 70ef4cae8..32495c4e1 100644 --- a/php-plugin/src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java +++ b/php-plugin/src/test/java/fr/greencodeinitiative/php/PhpRuleRepositoryTest.java @@ -16,42 +16,88 @@ */ package fr.greencodeinitiative.php; -import static org.assertj.core.api.Assertions.assertThat; import org.assertj.core.api.SoftAssertions; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.sonar.api.SonarRuntime; import org.sonar.api.server.rule.RulesDefinition; +import org.sonar.api.utils.Version; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +class PhpRuleRepositoryTest { -public class PhpRuleRepositoryTest { + private RulesDefinition.Repository repository; - private PhpRuleRepository phpRuleRepository; - private RulesDefinition.Context context; + @BeforeEach + void init() { + // TODO: Remove this check after Git repo split + /* + On an IDE (like IntelliJ), if the developer runs the unit tests without building/generating the Maven goals on the + "ecocode-rules-specifications" module before, the unit tests will not see the generated HTML descriptions (from ASCIIDOC files). + The developer must therefore configure his IDE to build the `ecocode-rules-specifications` module before launching the Tests. + + When the `php-plugin` submodule is in a specific Git repository, `ecocode-rules-specifications` will be fetched from a classic + external Maven dependency. There will therefore no longer be any need to perform this specific configuration. + */ + if (PhpRuleRepository.class.getResource("/io/ecocode/rules/php/EC4.json") == null) { + String message = "'ecocode-rules-specification' resources corrupted. Please check build of 'ecocode-rules-specification' module"; + if (System.getProperties().keySet().stream().anyMatch(k -> k.toString().startsWith("idea."))) { + message += "\n\nOn 'IntelliJ IDEA':" + + "\n1. go to settings :" + + "\n > Build, Execution, Deployment > Build Tools > Maven > Runner" + + "\n2. check option:" + + "\n > Delegate IDE build/run actions to Maven" + + "\n3. Click on menu: " + + "\n > Build > Build Project" + ; + } + fail(message); + } + + final SonarRuntime sonarRuntime = mock(SonarRuntime.class); + doReturn(Version.create(0, 0)).when(sonarRuntime).getApiVersion(); + PhpRuleRepository rulesDefinition = new PhpRuleRepository(sonarRuntime); + RulesDefinition.Context context = new RulesDefinition.Context(); + rulesDefinition.define(context); + repository = context.repository(rulesDefinition.repositoryKey()); + } - @Before - public void init() { - phpRuleRepository = new PhpRuleRepository(); - context = new RulesDefinition.Context(); - phpRuleRepository.define(context); + @Test + @DisplayName("Test repository metadata") + void testMetadata() { + assertThat(repository.name()).isEqualTo("ecoCode"); + assertThat(repository.language()).isEqualTo("php"); + assertThat(repository.key()).isEqualTo("ecocode-php"); } @Test - public void test() { - assertThat(phpRuleRepository.repositoryKey()).isEqualTo(PhpRuleRepository.REPOSITORY_KEY); - assertThat(context.repositories()).hasSize(1).extracting("key").containsExactly(phpRuleRepository.repositoryKey()); - assertThat(context.repositories().get(0).rules()).hasSize(9); - assertThat(phpRuleRepository.checkClasses()).hasSize(9); + void testRegistredRules() { + assertThat(repository.rules()).hasSize(9); } - /** - * Check all rule keys must be prefixed by 'EC' - */ - @Test() - public void testRuleKeyPrefix() { - RulesDefinition.Repository repository = context.repository(PhpRuleRepository.REPOSITORY_KEY); + @Test + @DisplayName("All rule keys must be prefixed by 'EC'") + void testRuleKeyPrefix() { SoftAssertions assertions = new SoftAssertions(); repository.rules().forEach( rule -> assertions.assertThat(rule.key()).startsWith("EC") ); assertions.assertAll(); } + + @Test + void testAllRuleParametersHaveDescription() { + SoftAssertions assertions = new SoftAssertions(); + repository.rules().stream() + .flatMap(rule -> rule.params().stream()) + .forEach(param -> assertions.assertThat(param.description()) + .as("description for " + param.key()) + .isNotEmpty()); + assertions.assertAll(); + } } From 9f95b447709196c63b2466c10d43218d96f0a070 Mon Sep 17 00:00:00 2001 From: jycr Date: Fri, 14 Apr 2023 18:25:32 +0200 Subject: [PATCH 4/7] green-code-initiative/ecoCode#92 refactor(rule/python): moves Python rules into `ecocode-rules-specifications` module --- ecocode-rules-specifications/pom.xml | 19 ++++ .../src/main/assembly/python.xml | 18 ++++ .../src/main/rules/EC10/EC10.json | 14 +++ .../src/main/rules/EC10/python/EC10.asciidoc | 0 .../src/main/rules/EC203/EC203.json | 16 ++++ .../main/rules/EC203/python/EC203.asciidoc | 0 .../src/main/rules/EC34/python/EC34.asciidoc | 0 .../src/main/rules/EC4/python/EC4.asciidoc | 0 .../src/main/rules/EC404/EC404.json | 15 ++++ .../main/rules/EC404/python/EC404.asciidoc | 0 .../src/main/rules/EC66/python/EC66.asciidoc | 0 .../src/main/rules/EC69/python/EC69.asciidoc | 0 .../src/main/rules/EC7/EC7.json | 15 ++++ .../src/main/rules/EC7/python/EC7.asciidoc | 0 .../src/main/rules/EC72/python/EC72.asciidoc | 0 .../src/main/rules/EC74/python/EC74.asciidoc | 0 python-plugin/pom.xml | 13 +++ .../python/PythonRuleRepository.java | 67 +++----------- .../python/PythonPluginTest.java | 16 ++-- .../python/PythonRuleRepositoryTest.java | 87 ++++++++++++++----- 20 files changed, 198 insertions(+), 82 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/assembly/python.xml create mode 100644 ecocode-rules-specifications/src/main/rules/EC10/EC10.json rename python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC10.html => ecocode-rules-specifications/src/main/rules/EC10/python/EC10.asciidoc (100%) create mode 100644 ecocode-rules-specifications/src/main/rules/EC203/EC203.json rename python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC203.html => ecocode-rules-specifications/src/main/rules/EC203/python/EC203.asciidoc (100%) rename python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC34.html => ecocode-rules-specifications/src/main/rules/EC34/python/EC34.asciidoc (100%) rename python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC4.html => ecocode-rules-specifications/src/main/rules/EC4/python/EC4.asciidoc (100%) create mode 100644 ecocode-rules-specifications/src/main/rules/EC404/EC404.json rename python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC404.html => ecocode-rules-specifications/src/main/rules/EC404/python/EC404.asciidoc (100%) rename python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC66.html => ecocode-rules-specifications/src/main/rules/EC66/python/EC66.asciidoc (100%) rename python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC69.html => ecocode-rules-specifications/src/main/rules/EC69/python/EC69.asciidoc (100%) create mode 100644 ecocode-rules-specifications/src/main/rules/EC7/EC7.json rename python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC7.html => ecocode-rules-specifications/src/main/rules/EC7/python/EC7.asciidoc (100%) rename python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC72.html => ecocode-rules-specifications/src/main/rules/EC72/python/EC72.asciidoc (100%) rename python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC74.html => ecocode-rules-specifications/src/main/rules/EC74/python/EC74.asciidoc (100%) diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index 8c53609a4..30e3f07ce 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -126,6 +126,13 @@ + + + + + + + @@ -171,6 +178,18 @@ + + assembly-python + prepare-package + + single + + + + ${project.basedir}/src/main/assembly/python.xml + + + true diff --git a/ecocode-rules-specifications/src/main/assembly/python.xml b/ecocode-rules-specifications/src/main/assembly/python.xml new file mode 100644 index 000000000..7c294fd24 --- /dev/null +++ b/ecocode-rules-specifications/src/main/assembly/python.xml @@ -0,0 +1,18 @@ + + python + + jar + + false + + + ${project.build.outputDirectory} + + io/ecocode/rules/python/*.* + + + + + diff --git a/ecocode-rules-specifications/src/main/rules/EC10/EC10.json b/ecocode-rules-specifications/src/main/rules/EC10/EC10.json new file mode 100644 index 000000000..ec7fbffd9 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC10/EC10.json @@ -0,0 +1,14 @@ +{ + "title": "Avoid using unoptimized vector images", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "60min" + }, + "tags": [ + "eco-design", + "ecocode" + ], + "defaultSeverity": "Minor" +} diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC10.html b/ecocode-rules-specifications/src/main/rules/EC10/python/EC10.asciidoc similarity index 100% rename from python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC10.html rename to ecocode-rules-specifications/src/main/rules/EC10/python/EC10.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules/EC203/EC203.json b/ecocode-rules-specifications/src/main/rules/EC203/EC203.json new file mode 100644 index 000000000..3f46aefd8 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC203/EC203.json @@ -0,0 +1,16 @@ +{ + "title": "Detect unoptimized image format", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "60min" + }, + "tags": [ + "performance", + "user-experience", + "eco-design", + "ecocode" + ], + "defaultSeverity": "Minor" +} diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC203.html b/ecocode-rules-specifications/src/main/rules/EC203/python/EC203.asciidoc similarity index 100% rename from python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC203.html rename to ecocode-rules-specifications/src/main/rules/EC203/python/EC203.asciidoc diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC34.html b/ecocode-rules-specifications/src/main/rules/EC34/python/EC34.asciidoc similarity index 100% rename from python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC34.html rename to ecocode-rules-specifications/src/main/rules/EC34/python/EC34.asciidoc diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC4.html b/ecocode-rules-specifications/src/main/rules/EC4/python/EC4.asciidoc similarity index 100% rename from python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC4.html rename to ecocode-rules-specifications/src/main/rules/EC4/python/EC4.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules/EC404/EC404.json b/ecocode-rules-specifications/src/main/rules/EC404/EC404.json new file mode 100644 index 000000000..c04d919d3 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC404/EC404.json @@ -0,0 +1,15 @@ +{ + "title": "Use generator comprehension instead of list comprehension in for loop declaration", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "15min" + }, + "tags": [ + "performance", + "eco-design", + "ecocode" + ], + "defaultSeverity": "Minor" +} diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC404.html b/ecocode-rules-specifications/src/main/rules/EC404/python/EC404.asciidoc similarity index 100% rename from python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC404.html rename to ecocode-rules-specifications/src/main/rules/EC404/python/EC404.asciidoc diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC66.html b/ecocode-rules-specifications/src/main/rules/EC66/python/EC66.asciidoc similarity index 100% rename from python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC66.html rename to ecocode-rules-specifications/src/main/rules/EC66/python/EC66.asciidoc diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC69.html b/ecocode-rules-specifications/src/main/rules/EC69/python/EC69.asciidoc similarity index 100% rename from python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC69.html rename to ecocode-rules-specifications/src/main/rules/EC69/python/EC69.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules/EC7/EC7.json b/ecocode-rules-specifications/src/main/rules/EC7/EC7.json new file mode 100644 index 000000000..46c194d56 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC7/EC7.json @@ -0,0 +1,15 @@ +{ + "title": "Avoid creating getter and setter methods in classes", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "eco-design", + "performance", + "ecocode" + ], + "defaultSeverity": "Minor" +} diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC7.html b/ecocode-rules-specifications/src/main/rules/EC7/python/EC7.asciidoc similarity index 100% rename from python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC7.html rename to ecocode-rules-specifications/src/main/rules/EC7/python/EC7.asciidoc diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC72.html b/ecocode-rules-specifications/src/main/rules/EC72/python/EC72.asciidoc similarity index 100% rename from python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC72.html rename to ecocode-rules-specifications/src/main/rules/EC72/python/EC72.asciidoc diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC74.html b/ecocode-rules-specifications/src/main/rules/EC74/python/EC74.asciidoc similarity index 100% rename from python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC74.html rename to ecocode-rules-specifications/src/main/rules/EC74/python/EC74.asciidoc diff --git a/python-plugin/pom.xml b/python-plugin/pom.xml index 3447d148a..b2a7d6e60 100644 --- a/python-plugin/pom.xml +++ b/python-plugin/pom.xml @@ -16,16 +16,24 @@ https://github.com/green-code-initiative/ecoCode/tree/main/python-plugin + + ${project.groupId} + ecocode-rules-specifications + ${project.version} + python + org.sonarsource.python sonar-python-plugin sonar-plugin + provided org.sonarsource.sonarqube sonar-plugin-api + provided @@ -63,6 +71,11 @@ ${java.version} + + + org.apache.maven.plugins + maven-shade-plugin + org.apache.maven.plugins maven-dependency-plugin 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 2718b28f0..23ff4f51e 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java @@ -17,58 +17,32 @@ package fr.greencodeinitiative.python; import fr.greencodeinitiative.python.checks.*; -import org.sonar.api.rules.RuleType; +import org.sonar.api.SonarRuntime; import org.sonar.api.server.rule.RulesDefinition; -import org.sonar.api.server.rule.RulesDefinitionAnnotationLoader; import org.sonar.plugins.python.api.PythonCustomRuleRepository; +import org.sonarsource.analyzer.commons.RuleMetadataLoader; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.nio.charset.StandardCharsets; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; public class PythonRuleRepository implements RulesDefinition, PythonCustomRuleRepository { public static final String LANGUAGE = "py"; public static final String NAME = "ecoCode"; - public static final String RESOURCE_BASE_PATH = "/fr/greencodeinitiative/l10n/python/rules/python/"; + public static final String RESOURCE_BASE_PATH = "io/ecocode/rules/python"; public static final String REPOSITORY_KEY = "ecocode-python"; - @Override - public void define(Context context) { - NewRepository repository = context.createRepository(repositoryKey(), LANGUAGE).setName(NAME); - - new RulesDefinitionAnnotationLoader().load(repository, checkClasses().toArray(new Class[] {})); - - // technical debt - Map remediationCosts = new HashMap<>(); - remediationCosts.put(AvoidSQLRequestInLoop.RULE_KEY, "10min"); - remediationCosts.put(AvoidFullSQLRequest.RULE_KEY, "20min"); - repository.rules().forEach(rule -> { - rule.setType(RuleType.CODE_SMELL); - String debt = remediationCosts.get(rule.key()); - - // TODO DDC : create support to use org.apache.commons.lang.StringUtils -// if (StringUtils.isBlank(debt)) { - if (debt == null || debt.trim().equals("")) { - // default debt to 5min for issue correction - rule.setDebtRemediationFunction( - rule.debtRemediationFunctions().constantPerIssue("5min")); - } else { - rule.setDebtRemediationFunction( - rule.debtRemediationFunctions().constantPerIssue(debt)); - } - }); + private final SonarRuntime sonarRuntime; - // HTML description - repository.rules().forEach(rule -> - rule.setHtmlDescription(loadResource(RESOURCE_BASE_PATH + rule.key() + ".html"))); + public PythonRuleRepository(SonarRuntime sonarRuntime) { + this.sonarRuntime = sonarRuntime; + } + @Override + public void define(Context context) { + NewRepository repository = context.createRepository(REPOSITORY_KEY, LANGUAGE).setName(NAME); + RuleMetadataLoader ruleMetadataLoader = new RuleMetadataLoader(RESOURCE_BASE_PATH, sonarRuntime); + ruleMetadataLoader.addRulesByAnnotatedClass(repository, (List) checkClasses()); repository.done(); } @@ -92,21 +66,4 @@ public List checkClasses() { DetectUnoptimizedImageFormat.class ); } - - private String loadResource(String path) { - URL resource = getClass().getResource(path); - if (resource == null) { - throw new IllegalStateException("Resource not found: " + path); - } - ByteArrayOutputStream result = new ByteArrayOutputStream(); - try (InputStream in = resource.openStream()) { - byte[] buffer = new byte[1024]; - for (int len = in.read(buffer); len != -1; len = in.read(buffer)) { - result.write(buffer, 0, len); - } - return new String(result.toByteArray(), StandardCharsets.UTF_8); - } catch (IOException e) { - throw new IllegalStateException("Failed to read resource: " + path, e); - } - } } diff --git a/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonPluginTest.java b/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonPluginTest.java index 477f5d4bb..e270ad449 100644 --- a/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonPluginTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonPluginTest.java @@ -16,20 +16,26 @@ */ package fr.greencodeinitiative.python; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.sonar.api.Plugin; import org.sonar.api.SonarRuntime; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; -public class PythonPluginTest { +class PythonPluginTest { + private Plugin.Context context; - @Test - public void test() { + @BeforeEach + void init() { SonarRuntime sonarRuntime = mock(SonarRuntime.class); - Plugin.Context context = new Plugin.Context(sonarRuntime); + context = new Plugin.Context(sonarRuntime); new PythonPlugin().define(context); + } + + @Test + void test() { assertThat(context.getExtensions()).hasSize(1); } 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 828447a4d..714ef278e 100644 --- a/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonRuleRepositoryTest.java +++ b/python-plugin/src/test/java/fr/greencodeinitiative/python/PythonRuleRepositoryTest.java @@ -16,45 +16,88 @@ */ package fr.greencodeinitiative.python; -import static org.assertj.core.api.Assertions.assertThat; - import org.assertj.core.api.SoftAssertions; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.sonar.api.SonarRuntime; import org.sonar.api.server.rule.RulesDefinition; +import org.sonar.api.utils.Version; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; -public class PythonRuleRepositoryTest { +class PythonRuleRepositoryTest { - private PythonRuleRepository pythonRuleRepository; - private RulesDefinition.Context context; private RulesDefinition.Repository repository; - @Before - public void init() { - pythonRuleRepository = new PythonRuleRepository(); - context = new RulesDefinition.Context(); - pythonRuleRepository.define(context); - repository = context.repository(PythonRuleRepository.REPOSITORY_KEY); + @BeforeEach + void init() { + // TODO: Remove this check after Git repo split + /* + On an IDE (like IntelliJ), if the developer runs the unit tests without building/generating the Maven goals on the + "ecocode-rules-specifications" module before, the unit tests will not see the generated HTML descriptions (from ASCIIDOC files). + The developer must therefore configure his IDE to build the `ecocode-rules-specifications` module before launching the Tests. + + When the `python-plugin` submodule is in a specific Git repository, `ecocode-rules-specifications` will be fetched from a classic + external Maven dependency. There will therefore no longer be any need to perform this specific configuration. + */ + if (PythonRuleRepository.class.getResource("/io/ecocode/rules/python/EC4.json") == null) { + String message = "'ecocode-rules-specification' resources corrupted. Please check build of 'ecocode-rules-specification' module"; + if (System.getProperties().keySet().stream().anyMatch(k -> k.toString().startsWith("idea."))) { + message += "\n\nOn 'IntelliJ IDEA':" + + "\n1. go to settings :" + + "\n > Build, Execution, Deployment > Build Tools > Maven > Runner" + + "\n2. check option:" + + "\n > Delegate IDE build/run actions to Maven" + + "\n3. Click on menu: " + + "\n > Build > Build Project" + ; + } + fail(message); + } + + final SonarRuntime sonarRuntime = mock(SonarRuntime.class); + doReturn(Version.create(0, 0)).when(sonarRuntime).getApiVersion(); + PythonRuleRepository rulesDefinition = new PythonRuleRepository(sonarRuntime); + RulesDefinition.Context context = new RulesDefinition.Context(); + rulesDefinition.define(context); + repository = context.repository(rulesDefinition.repositoryKey()); } @Test - 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(10); - assertThat(pythonRuleRepository.checkClasses()).hasSize(10); + @DisplayName("Test repository metadata") + void testMetadata() { + assertThat(repository.name()).isEqualTo("ecoCode"); + assertThat(repository.language()).isEqualTo("py"); + assertThat(repository.key()).isEqualTo("ecocode-python"); } + @Test + void testRegistredRules() { + assertThat(repository.rules()).hasSize(10); + } - /** - * Check all rule keys must be prefixed by 'EC' - */ @Test - public void testRuleKeyPrefix() { + @DisplayName("All rule keys must be prefixed by 'EC'") + void testRuleKeyPrefix() { SoftAssertions assertions = new SoftAssertions(); repository.rules().forEach( rule -> assertions.assertThat(rule.key()).startsWith("EC") ); assertions.assertAll(); } + + @Test + void testAllRuleParametersHaveDescription() { + SoftAssertions assertions = new SoftAssertions(); + repository.rules().stream() + .flatMap(rule -> rule.params().stream()) + .forEach(param -> assertions.assertThat(param.description()) + .as("description for " + param.key()) + .isNotEmpty()); + assertions.assertAll(); + } } From 837c2bf4a16aa497ac5dc4e79be37ee274d381bb Mon Sep 17 00:00:00 2001 From: jycr Date: Tue, 27 Jun 2023 09:21:54 +0200 Subject: [PATCH 5/7] green-code-initiative/ecoCode#92 improves code quality (reduce code duplication) --- .../src/main/rules/EC3/EC3.json | 1 + .../src/main/rules/EC34/EC34.json | 2 +- .../src/main/rules/EC66/EC66.json | 1 + .../java/checks/ArrayCopyCheck.java | 7 +------ .../java/checks/AvoidConcatenateStringsInLoop.java | 9 +-------- .../java/checks/AvoidFullSQLRequest.java | 9 ++------- .../java/checks/AvoidGettingSizeCollectionInLoop.java | 7 +------ .../java/checks/AvoidMultipleIfElseStatement.java | 7 +------ .../java/checks/AvoidRegexPatternNotStatic.java | 8 +------- .../java/checks/AvoidSQLRequestInLoop.java | 4 +--- .../java/checks/AvoidSetConstantInBatchUpdate.java | 7 ++----- .../checks/AvoidSpringRepositoryCallInLoopCheck.java | 7 +------ .../java/checks/AvoidStatementForDMLQueries.java | 2 +- .../java/checks/AvoidUsageOfStaticCollections.java | 8 +------- .../java/checks/AvoidUsingGlobalVariablesCheck.java | 8 +------- .../checks/FreeResourcesOfAutoCloseableInterface.java | 8 +------- .../java/checks/IncrementCheck.java | 8 +------- .../checks/InitializeBufferWithAppropriateSize.java | 8 +------- .../checks/NoFunctionCallWhenDeclaringForLoop.java | 4 +--- .../java/checks/OptimizeReadFileExceptions.java | 8 +------- .../checks/UnnecessarilyAssignValuesToVariables.java | 4 +--- .../java/checks/UseCorrectForLoop.java | 8 +------- .../php/checks/AvoidDoubleQuoteCheck.java | 9 +-------- .../php/checks/AvoidFullSQLRequestCheck.java | 10 +--------- .../checks/AvoidGettingSizeCollectionInLoopCheck.java | 10 +--------- .../php/checks/AvoidSQLRequestInLoopCheck.java | 9 +-------- ...TryCatchFinallyCheck_NOK_failsAllTryStatements.java | 9 +-------- .../php/checks/AvoidUsingGlobalVariablesCheck.java | 9 +-------- .../greencodeinitiative/php/checks/IncrementCheck.java | 9 +-------- .../php/checks/NoFunctionCallWhenDeclaringForLoop.java | 9 +-------- .../php/checks/UseOfMethodsForBasicOperations.java | 9 +-------- .../python/checks/AvoidDoubleQuoteCheck.java | 9 +-------- .../python/checks/AvoidFullSQLRequest.java | 10 +--------- .../python/checks/AvoidGettersAndSetters.java | 9 +-------- .../checks/AvoidGlobalVariableInFunctionCheck.java | 9 +-------- .../checks/AvoidListComprehensionInIterations.java | 9 +-------- .../python/checks/AvoidSQLRequestInLoop.java | 10 +--------- .../python/checks/AvoidTryCatchFinallyCheck.java | 9 +-------- .../checks/AvoidUnoptimizedVectorImagesCheck.java | 9 +-------- .../python/checks/DetectUnoptimizedImageFormat.java | 8 +------- .../checks/NoFunctionCallWhenDeclaringForLoop.java | 9 +-------- 41 files changed, 43 insertions(+), 266 deletions(-) diff --git a/ecocode-rules-specifications/src/main/rules/EC3/EC3.json b/ecocode-rules-specifications/src/main/rules/EC3/EC3.json index f511c1c2a..33916b15e 100644 --- a/ecocode-rules-specifications/src/main/rules/EC3/EC3.json +++ b/ecocode-rules-specifications/src/main/rules/EC3/EC3.json @@ -7,6 +7,7 @@ "constantCost": "5min" }, "tags": [ + "bad-practice", "eco-design", "performance", "ecocode" diff --git a/ecocode-rules-specifications/src/main/rules/EC34/EC34.json b/ecocode-rules-specifications/src/main/rules/EC34/EC34.json index 4191cbb71..00bc65320 100644 --- a/ecocode-rules-specifications/src/main/rules/EC34/EC34.json +++ b/ecocode-rules-specifications/src/main/rules/EC34/EC34.json @@ -1,5 +1,5 @@ { - "title": "Avoid using try-catch-finally statement", + "title": "Avoid using try-catch statement", "type": "CODE_SMELL", "status": "ready", "remediation": { diff --git a/ecocode-rules-specifications/src/main/rules/EC66/EC66.json b/ecocode-rules-specifications/src/main/rules/EC66/EC66.json index 6ed6137dc..034168254 100644 --- a/ecocode-rules-specifications/src/main/rules/EC66/EC66.json +++ b/ecocode-rules-specifications/src/main/rules/EC66/EC66.json @@ -7,6 +7,7 @@ "constantCost": "5min" }, "tags": [ + "bad-practice", "eco-design", "performance", "ecocode" diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ArrayCopyCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ArrayCopyCheck.java index 5abd5201e..080fc83d1 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ArrayCopyCheck.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/ArrayCopyCheck.java @@ -6,7 +6,6 @@ import java.util.function.Predicate; import java.util.stream.Collectors; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree; @@ -37,11 +36,7 @@ * @author Aubay * @formatter:off */ -@Rule(key = "EC27", - name = "Developpement", - description = ArrayCopyCheck.MESSAGERULE, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC27") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "GRPS0027") public class ArrayCopyCheck extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoop.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoop.java index 996043b5a..90d68b852 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoop.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidConcatenateStringsInLoop.java @@ -2,10 +2,8 @@ import java.util.Arrays; import java.util.List; - import javax.annotation.Nonnull; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.tree.AssignmentExpressionTree; @@ -15,12 +13,7 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = "EC75", - name = "Developpement", - description = AvoidConcatenateStringsInLoop.MESSAGE_RULE, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode", "memory"}) +@Rule(key = "EC75") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S75") public class AvoidConcatenateStringsInLoop extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidFullSQLRequest.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidFullSQLRequest.java index 3d9bcc22f..e8dd5d28b 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidFullSQLRequest.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidFullSQLRequest.java @@ -6,7 +6,7 @@ import static java.util.Collections.singletonList; import static java.util.regex.Pattern.CASE_INSENSITIVE; import static java.util.regex.Pattern.compile; -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; @@ -14,12 +14,7 @@ import org.sonar.plugins.java.api.tree.Tree.Kind; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = "EC74", - name = "Developpement", - description = AvoidFullSQLRequest.MESSAGERULE, - priority = Priority.MINOR, - tags = {"performance", "sql", "eco-design", "ecocode", "network"}) +@Rule(key = "EC74") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S74") public class AvoidFullSQLRequest extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidGettingSizeCollectionInLoop.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidGettingSizeCollectionInLoop.java index 9f70e0ede..7112bb685 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidGettingSizeCollectionInLoop.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidGettingSizeCollectionInLoop.java @@ -5,7 +5,6 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.semantic.MethodMatchers; @@ -18,11 +17,7 @@ import org.sonar.plugins.java.api.tree.WhileStatementTree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule(key = "EC3", - name = "Developpement", - description = AvoidGettingSizeCollectionInLoop.MESSAGERULE, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC3") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "GSCIL") public class AvoidGettingSizeCollectionInLoop extends IssuableSubscriptionVisitor { protected static final String MESSAGERULE = "Avoid getting the size of the collection in the loop"; diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatement.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatement.java index 64a8fa037..9460642ab 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatement.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidMultipleIfElseStatement.java @@ -3,7 +3,6 @@ import java.util.Arrays; import java.util.List; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.tree.BlockTree; @@ -12,11 +11,7 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule(key = "EC2", - name = "Developpement", - description = AvoidMultipleIfElseStatement.RULE_MESSAGE, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC2") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "AMIES") public class AvoidMultipleIfElseStatement extends IssuableSubscriptionVisitor { protected static final String RULE_MESSAGE = "Using a switch statement instead of multiple if-else if possible"; diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStatic.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStatic.java index 4b6949cce..a4e8e2f7e 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStatic.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidRegexPatternNotStatic.java @@ -6,7 +6,6 @@ import javax.annotation.Nonnull; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.semantic.MethodMatchers; @@ -16,12 +15,7 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = "EC77", - name = "Developpement", - description = AvoidRegexPatternNotStatic.MESSAGE_RULE, - priority = Priority.MINOR, - tags = {"performance", "regex", "eco-design", "ecocode", "memory"}) +@Rule(key = "EC77") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S77") public class AvoidRegexPatternNotStatic extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoop.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoop.java index 532b89c12..297895e08 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoop.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSQLRequestInLoop.java @@ -3,7 +3,6 @@ import java.util.Arrays; import java.util.List; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.semantic.MethodMatchers; @@ -14,8 +13,7 @@ import org.sonar.plugins.java.api.tree.Tree.Kind; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule(key = "EC72", name = "Developpement", description = AvoidSQLRequestInLoop.MESSAGERULE, priority = Priority.MINOR, - tags = {"performance", "sql", "spring", "eco-design", "ecocode", "memory", "network"}) +@Rule(key = "EC72") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S72") public class AvoidSQLRequestInLoop extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchUpdate.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchUpdate.java index 9a692f82d..4a014c2a3 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchUpdate.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSetConstantInBatchUpdate.java @@ -6,7 +6,7 @@ import static fr.greencodeinitiative.java.checks.ConstOrLiteralDeclare.isLiteral; import static java.util.Arrays.asList; -import org.sonar.check.Priority; + import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.semantic.MethodMatchers; @@ -21,10 +21,7 @@ import static org.sonar.plugins.java.api.tree.Tree.Kind.MEMBER_SELECT; import static org.sonar.plugins.java.api.tree.Tree.Kind.METHOD_INVOCATION; -@Rule(key = "EC78", name = "Developpement", - description = AvoidSetConstantInBatchUpdate.MESSAGERULE, - priority = Priority.MINOR, - tags = {"eco-design", "ecocode", "sql", "performance", "memory"}) +@Rule(key = "EC78") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S78") public class AvoidSetConstantInBatchUpdate extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopCheck.java index 5730b69d9..6cf843392 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopCheck.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidSpringRepositoryCallInLoopCheck.java @@ -3,7 +3,6 @@ import java.util.Arrays; import java.util.List; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.semantic.MethodMatchers; @@ -12,11 +11,7 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule(key = "EC1", - name = "Developpement", - description = AvoidSpringRepositoryCallInLoopCheck.RULE_MESSAGE, - priority = Priority.MINOR, - tags = {"performance", "spring", "eco-design", "ecocode"}) +@Rule(key = "EC1") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "GRC1") public class AvoidSpringRepositoryCallInLoopCheck extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueries.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueries.java index efd76ca31..f273ae707 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueries.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidStatementForDMLQueries.java @@ -14,7 +14,7 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule(key = "EC5", tags={"performance", "sql", "eco-design", "ecocode"}) +@Rule(key = "EC5") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "SDMLQ1") public class AvoidStatementForDMLQueries extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollections.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollections.java index d4de0d49e..c6eba94b7 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollections.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsageOfStaticCollections.java @@ -6,7 +6,6 @@ import javax.annotation.Nonnull; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.tree.BaseTreeVisitor; @@ -14,12 +13,7 @@ import org.sonar.plugins.java.api.tree.VariableTree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = "EC76", - name = "Developpement", - description = AvoidUsageOfStaticCollections.MESSAGE_RULE, - priority = Priority.MINOR, - tags = {"cwe", "leak", "eco-design", "ecocode", "memory"}) +@Rule(key = "EC76") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S76") public class AvoidUsageOfStaticCollections extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheck.java index e2f08c522..bdc4dee1b 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheck.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheck.java @@ -4,7 +4,6 @@ import java.util.List; import com.google.re2j.Pattern; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.tree.Tree; @@ -12,12 +11,7 @@ import org.sonar.plugins.java.api.tree.VariableTree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = "EC4", - name = "Developpement", - description = "

Prefer local variables to globals

", - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC4") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "D4") public class AvoidUsingGlobalVariablesCheck extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterface.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterface.java index 0cacf32d0..fb41ba0f7 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterface.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/FreeResourcesOfAutoCloseableInterface.java @@ -8,7 +8,6 @@ import javax.annotation.ParametersAreNonnullByDefault; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.JavaFileScannerContext; @@ -19,12 +18,7 @@ import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = "EC79", - name = "Developpement", - description = FreeResourcesOfAutoCloseableInterface.MESSAGE_RULE, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC79") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S79") public class FreeResourcesOfAutoCloseableInterface extends IssuableSubscriptionVisitor { private final Deque withinTry = new LinkedList<>(); diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java index 1ac657aa3..7b480b1c7 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/IncrementCheck.java @@ -3,19 +3,13 @@ import java.util.Collections; import java.util.List; -import org.sonar.check.Priority; 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.Tree.Kind; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = "EC67", - name = "Developpement", - description = IncrementCheck.MESSAGERULE, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC67") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S67") public class IncrementCheck extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSize.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSize.java index 0090a7ac5..bb3cc4dd6 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSize.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/InitializeBufferWithAppropriateSize.java @@ -3,7 +3,6 @@ import java.util.Collections; import java.util.List; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.tree.NewClassTree; @@ -11,12 +10,7 @@ import org.sonar.plugins.java.api.tree.Tree.Kind; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = "EC32", - name = "Developpement", - description = InitializeBufferWithAppropriateSize.RULE_MESSAGE, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC32") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "GRSP0032") public class InitializeBufferWithAppropriateSize extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoop.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoop.java index 6c8758aa0..1c7c47347 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoop.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/NoFunctionCallWhenDeclaringForLoop.java @@ -7,7 +7,6 @@ import java.util.List; import java.util.Map; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.tree.BaseTreeVisitor; @@ -21,8 +20,7 @@ import org.sonar.plugins.java.api.tree.Tree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule(key = "EC69", name = "Developpement", description = NoFunctionCallWhenDeclaringForLoop.MESSAGERULE, priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC69") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S69") public class NoFunctionCallWhenDeclaringForLoop extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptions.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptions.java index 0ee8a79c7..9766d2009 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptions.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/OptimizeReadFileExceptions.java @@ -6,7 +6,6 @@ import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.tree.CatchTree; @@ -16,12 +15,7 @@ import org.sonar.plugins.java.api.tree.TryStatementTree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = "EC28", - name = "Developpement", - description = OptimizeReadFileExceptions.MESSAGERULE, - priority = Priority.MINOR, - tags = {"performance", "error-handling", "eco-design", "ecocode"}) +@Rule(key = "EC28") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "GRSP0028") public class OptimizeReadFileExceptions extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariables.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariables.java index d8a7e69da..8c6dde8f0 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariables.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UnnecessarilyAssignValuesToVariables.java @@ -1,6 +1,5 @@ package fr.greencodeinitiative.java.checks; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.JavaFileScanner; import org.sonar.plugins.java.api.JavaFileScannerContext; @@ -11,8 +10,7 @@ import javax.annotation.CheckForNull; import java.util.*; -@Rule(key = "EC63", name = "Developpement", description = "Do not unnecessarily assign values to variables", priority = Priority.MINOR, - tags = {"eco-design", "ecocode", "memory"}) +@Rule(key = "EC63") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S63") public class UnnecessarilyAssignValuesToVariables extends BaseTreeVisitor implements JavaFileScanner { diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UseCorrectForLoop.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UseCorrectForLoop.java index 5cbc68e2f..6da7c13c1 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UseCorrectForLoop.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UseCorrectForLoop.java @@ -3,7 +3,6 @@ import java.util.Arrays; import java.util.List; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; import org.sonar.plugins.java.api.tree.ForEachStatement; @@ -11,12 +10,7 @@ import org.sonar.plugins.java.api.tree.Tree.Kind; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = "EC53", - name = "Developpement", - description = UseCorrectForLoop.MESSAGERULE, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC53") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S53") public class UseCorrectForLoop extends IssuableSubscriptionVisitor { diff --git a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidDoubleQuoteCheck.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidDoubleQuoteCheck.java index cc89b1600..67e6a7c47 100644 --- a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidDoubleQuoteCheck.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidDoubleQuoteCheck.java @@ -7,23 +7,16 @@ import java.util.List; import java.util.Map; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.php.api.tree.Tree; import org.sonar.plugins.php.api.tree.expression.LiteralTree; import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = AvoidDoubleQuoteCheck.RULE_KEY, - name = AvoidDoubleQuoteCheck.ERROR_MESSAGE, - description = AvoidDoubleQuoteCheck.ERROR_MESSAGE, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC66") @DeprecatedRuleKey(repositoryKey = "gci-php", ruleKey = "S66") public class AvoidDoubleQuoteCheck extends PHPSubscriptionCheck { - public static final String RULE_KEY = "EC66"; public static final String ERROR_MESSAGE = "Avoid using double quote (\"), prefer using simple quote (')"; private static final Map> linesWithIssuesByFile = new HashMap<>(); diff --git a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidFullSQLRequestCheck.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidFullSQLRequestCheck.java index c6fe2b28a..61c175d36 100644 --- a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidFullSQLRequestCheck.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidFullSQLRequestCheck.java @@ -4,7 +4,6 @@ import java.util.List; import java.util.regex.Pattern; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.php.api.tree.Tree; import org.sonar.plugins.php.api.tree.Tree.Kind; @@ -12,17 +11,10 @@ import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = AvoidFullSQLRequestCheck.RULE_KEY, - name = AvoidFullSQLRequestCheck.ERROR_MESSAGE, - description = AvoidFullSQLRequestCheck.ERROR_MESSAGE, - priority = Priority.MINOR, - tags = {"sql", "performance", "eco-design", "ecocode"}) +@Rule(key = "EC74") @DeprecatedRuleKey(repositoryKey = "gci-php", ruleKey = "S74") public class AvoidFullSQLRequestCheck extends PHPSubscriptionCheck { - public static final String RULE_KEY = "EC74"; - public static final String ERROR_MESSAGE = "Don't use the query SELECT * FROM"; private static final Pattern PATTERN = Pattern.compile("(?i).*select.*\\*.*from.*"); diff --git a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidGettingSizeCollectionInLoopCheck.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidGettingSizeCollectionInLoopCheck.java index 7ef27ade9..de7fd1729 100644 --- a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidGettingSizeCollectionInLoopCheck.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidGettingSizeCollectionInLoopCheck.java @@ -1,6 +1,5 @@ package fr.greencodeinitiative.php.checks; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.php.api.tree.SeparatedList; import org.sonar.plugins.php.api.tree.Tree; @@ -16,16 +15,9 @@ import java.util.List; import java.util.regex.Pattern; -@Rule( - key = AvoidGettingSizeCollectionInLoopCheck.RULE_KEY, - name = AvoidGettingSizeCollectionInLoopCheck.ERROR_MESSAGE, - description = AvoidGettingSizeCollectionInLoopCheck.ERROR_MESSAGE, - priority = Priority.MINOR, - tags = {"eco-design", "ecocode", "bad-practice", "performance"} -) +@Rule(key = "EC3") public class AvoidGettingSizeCollectionInLoopCheck extends PHPSubscriptionCheck { - public static final String RULE_KEY = "EC3"; public static final String ERROR_MESSAGE = "Avoid getting the size of the collection in the loop"; private static final Pattern PATTERN = Pattern.compile("\\b(?:count|sizeof|iterator_count)\\b"); diff --git a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidSQLRequestInLoopCheck.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidSQLRequestInLoopCheck.java index 8a392fcb3..0badb8c2b 100644 --- a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidSQLRequestInLoopCheck.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidSQLRequestInLoopCheck.java @@ -4,7 +4,6 @@ import java.util.List; import java.util.regex.Pattern; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.php.api.tree.Tree; import org.sonar.plugins.php.api.tree.Tree.Kind; @@ -18,16 +17,10 @@ import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = AvoidSQLRequestInLoopCheck.RULE_KEY, - name = AvoidSQLRequestInLoopCheck.ERROR_MESSAGE, - description = AvoidSQLRequestInLoopCheck.ERROR_MESSAGE, - priority = Priority.MINOR, - tags = {"sql", "performance", "eco-design", "ecocode"}) +@Rule(key = "EC72") @DeprecatedRuleKey(repositoryKey = "gci-php", ruleKey = "S72") public class AvoidSQLRequestInLoopCheck extends PHPSubscriptionCheck { - public static final String RULE_KEY = "EC72"; public static final String ERROR_MESSAGE = "Avoid SQL request in loop"; private static final Pattern PATTERN = Pattern.compile("(mysql(i::|_)query\\s*\\(.*)|(oci_execute\\(.*)"); diff --git a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.java index fa818f8c5..6aa9943a7 100644 --- a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.java @@ -3,22 +3,15 @@ import java.util.Collections; import java.util.List; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.php.api.tree.Tree; import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.RULE_KEY, - name = AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.ERROR_MESSAGE, - description = AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements.ERROR_MESSAGE, - priority = Priority.MINOR, - tags = {"error-handling", "performance", "eco-design", "ecocode"}) +@Rule(key = "EC34") @DeprecatedRuleKey(repositoryKey = "gci-php", ruleKey = "S34") public class AvoidTryCatchFinallyCheck_NOK_failsAllTryStatements extends PHPSubscriptionCheck { - public static final String RULE_KEY = "EC34"; public static final String ERROR_MESSAGE = "Avoid using try-catch"; @Override diff --git a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidUsingGlobalVariablesCheck.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidUsingGlobalVariablesCheck.java index 7eecfdb2a..1b37a96e5 100644 --- a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidUsingGlobalVariablesCheck.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/AvoidUsingGlobalVariablesCheck.java @@ -2,22 +2,15 @@ import java.util.regex.Pattern; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.php.api.tree.declaration.FunctionDeclarationTree; import org.sonar.plugins.php.api.visitors.PHPVisitorCheck; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = AvoidUsingGlobalVariablesCheck.RULE_KEY, - name = AvoidUsingGlobalVariablesCheck.ERROR_MESSAGE, - description = AvoidUsingGlobalVariablesCheck.ERROR_MESSAGE, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC4") @DeprecatedRuleKey(repositoryKey = "gci-php", ruleKey = "D4") public class AvoidUsingGlobalVariablesCheck extends PHPVisitorCheck { - public static final String RULE_KEY = "EC4"; public static final String ERROR_MESSAGE = "Prefer local variables to globals"; private static final Pattern PATTERN = Pattern.compile("^.*(global \\$|\\$GLOBALS).*$", Pattern.CASE_INSENSITIVE); diff --git a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/IncrementCheck.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/IncrementCheck.java index 79bd2cb4a..904cd404b 100644 --- a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/IncrementCheck.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/IncrementCheck.java @@ -3,23 +3,16 @@ import java.util.Collections; import java.util.List; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.php.api.tree.Tree; import org.sonar.plugins.php.api.tree.Tree.Kind; import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = IncrementCheck.RULE_KEY, - name = IncrementCheck.ERROR_MESSAGE, - description = IncrementCheck.ERROR_MESSAGE, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC67") @DeprecatedRuleKey(repositoryKey = "gci-php", ruleKey = "S67") public class IncrementCheck extends PHPSubscriptionCheck { - public static final String RULE_KEY = "EC67"; public static final String ERROR_MESSAGE = "Remove the usage of $i++. prefer ++$i"; @Override diff --git a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/NoFunctionCallWhenDeclaringForLoop.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/NoFunctionCallWhenDeclaringForLoop.java index 39ddca9a4..999d0cfe5 100644 --- a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/NoFunctionCallWhenDeclaringForLoop.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/NoFunctionCallWhenDeclaringForLoop.java @@ -3,7 +3,6 @@ import java.util.Collections; import java.util.List; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.php.api.tree.SeparatedList; import org.sonar.plugins.php.api.tree.Tree; @@ -14,16 +13,10 @@ import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = NoFunctionCallWhenDeclaringForLoop.RULE_KEY, - name = NoFunctionCallWhenDeclaringForLoop.ERROR_MESSAGE, - description = NoFunctionCallWhenDeclaringForLoop.ERROR_MESSAGE, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC69") @DeprecatedRuleKey(repositoryKey = "gci-php", ruleKey = "S69") public class NoFunctionCallWhenDeclaringForLoop extends PHPSubscriptionCheck { - public static final String RULE_KEY = "EC69"; public static final String ERROR_MESSAGE = "Do not call a function in for-type loop declaration"; @Override diff --git a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/UseOfMethodsForBasicOperations.java b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/UseOfMethodsForBasicOperations.java index a1d03d769..1285fb4d7 100644 --- a/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/UseOfMethodsForBasicOperations.java +++ b/php-plugin/src/main/java/fr/greencodeinitiative/php/checks/UseOfMethodsForBasicOperations.java @@ -6,7 +6,6 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.php.api.tree.ScriptTree; import org.sonar.plugins.php.api.tree.Tree; @@ -18,16 +17,10 @@ import org.sonar.plugins.php.api.visitors.PHPSubscriptionCheck; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = UseOfMethodsForBasicOperations.RULE_KEY, - name = UseOfMethodsForBasicOperations.ERROR_MESSAGE, - description = UseOfMethodsForBasicOperations.ERROR_MESSAGE, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC22") @DeprecatedRuleKey(repositoryKey = "gci-php", ruleKey = "D2") public class UseOfMethodsForBasicOperations extends PHPSubscriptionCheck { - public static final String RULE_KEY = "EC22"; protected static final String ERROR_MESSAGE = "Use of methods for basic operations"; @Override diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidDoubleQuoteCheck.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidDoubleQuoteCheck.java index 033c468d1..e6b191289 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidDoubleQuoteCheck.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidDoubleQuoteCheck.java @@ -1,20 +1,13 @@ package fr.greencodeinitiative.python.checks; -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; import org.sonar.plugins.python.api.tree.Tree; -@Rule( - key = AvoidDoubleQuoteCheck.RULE_KEY, - name = AvoidDoubleQuoteCheck.MESSAGE_RULE, - description = AvoidDoubleQuoteCheck.MESSAGE_RULE, - priority = Priority.MINOR, - tags = {"eco-design", "ecocode", "bad-practice"}) +@Rule(key = "EC66") public class AvoidDoubleQuoteCheck extends PythonSubscriptionCheck { - public static final String RULE_KEY = "EC66"; public static final String MESSAGE_RULE = "Avoid using quotation mark (\"), prefer using simple quote (')"; @Override public void initialize(Context context) { diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidFullSQLRequest.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidFullSQLRequest.java index 77c5f43be..8c6d4eedf 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidFullSQLRequest.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidFullSQLRequest.java @@ -7,7 +7,6 @@ import java.util.Map; import java.util.regex.Pattern; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.python.api.PythonSubscriptionCheck; import org.sonar.plugins.python.api.SubscriptionContext; @@ -16,17 +15,10 @@ import org.sonar.plugins.python.api.tree.Tree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = AvoidFullSQLRequest.RULE_KEY, - name = AvoidFullSQLRequest.MESSAGERULE, - description = AvoidFullSQLRequest.MESSAGERULE, - priority = Priority.MINOR, - tags = {"sql", "performance", "eco-design", "ecocode"}) +@Rule(key = "EC74") @DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "S74") public class AvoidFullSQLRequest extends PythonSubscriptionCheck { - public static final String RULE_KEY = "EC74"; - protected static final String MESSAGERULE = "Don't use the query SELECT * FROM"; // TODO DDC : create support to add in deployment th dependency com.google.re2j:re2j diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSetters.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSetters.java index fc1dddca4..beb063de7 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSetters.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidGettersAndSetters.java @@ -3,7 +3,6 @@ import java.util.List; import java.util.stream.Collectors; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.python.api.PythonSubscriptionCheck; import org.sonar.plugins.python.api.SubscriptionContext; @@ -18,16 +17,10 @@ import org.sonar.plugins.python.api.tree.Tree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = AvoidGettersAndSetters.RULE_KEY, - name = AvoidGettersAndSetters.DESCRIPTION, - description = AvoidGettersAndSetters.DESCRIPTION, - priority = Priority.MINOR, - tags = {"convention", "eco-design", "ecocode"}) +@Rule(key = "EC7") @DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "D7") public class AvoidGettersAndSetters extends PythonSubscriptionCheck { - public static final String RULE_KEY = "EC7"; public static final String DESCRIPTION = "Avoid creating getter and setter methods in classes"; @Override diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidGlobalVariableInFunctionCheck.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidGlobalVariableInFunctionCheck.java index ab4302bbd..82bc46b74 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidGlobalVariableInFunctionCheck.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidGlobalVariableInFunctionCheck.java @@ -5,7 +5,6 @@ import java.util.List; import java.util.Map; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.python.api.PythonSubscriptionCheck; import org.sonar.plugins.python.api.SubscriptionCheck; @@ -62,16 +61,10 @@ import org.sonar.plugins.python.api.tree.YieldStatement; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = AvoidGlobalVariableInFunctionCheck.RULE_KEY, - name = "Do not call global variables directly inside functions", - description = AvoidGlobalVariableInFunctionCheck.DESCRIPTION, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC4") @DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "D4") public class AvoidGlobalVariableInFunctionCheck extends PythonSubscriptionCheck { - public static final String RULE_KEY = "EC4"; public static final String DESCRIPTION = "Use local variable (function/class scope) instead of global variable (application scope)"; private List globalVariables; diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidListComprehensionInIterations.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidListComprehensionInIterations.java index 5cb64346d..917d053c9 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidListComprehensionInIterations.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidListComprehensionInIterations.java @@ -1,6 +1,5 @@ package fr.greencodeinitiative.python.checks; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.python.api.PythonSubscriptionCheck; import org.sonar.plugins.python.api.SubscriptionContext; @@ -18,15 +17,9 @@ import static org.sonar.plugins.python.api.tree.Tree.Kind.LIST_COMPREHENSION; import static org.sonar.plugins.python.api.tree.Tree.Kind.REGULAR_ARGUMENT; -@Rule( - key = AvoidListComprehensionInIterations.RULE_KEY, - name = AvoidListComprehensionInIterations.DESCRIPTION, - description = AvoidListComprehensionInIterations.DESCRIPTION, - priority = Priority.MINOR, - tags = {"eco-design", "ecocode", "performance"}) +@Rule(key = "EC404") public class AvoidListComprehensionInIterations extends PythonSubscriptionCheck { - public static final String RULE_KEY = "EC404"; public static final String DESCRIPTION = "Use generator comprehension instead of list comprehension in for loop declaration"; @Override diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidSQLRequestInLoop.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidSQLRequestInLoop.java index 7a7c1187e..a2bb20786 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidSQLRequestInLoop.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidSQLRequestInLoop.java @@ -6,7 +6,6 @@ import java.util.Objects; import java.util.Set; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.python.api.PythonSubscriptionCheck; import org.sonar.plugins.python.api.SubscriptionContext; @@ -20,17 +19,10 @@ import org.sonar.plugins.python.api.tree.Tree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = AvoidSQLRequestInLoop.RULE_KEY, - name = "Avoid SQL request in loop", - description = AvoidSQLRequestInLoop.MESSAGE_RULE, - priority = Priority.MINOR, - tags = {"sql", "performance", "eco-design", "ecocode"}) +@Rule(key = "EC72") @DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "S72") public class AvoidSQLRequestInLoop extends PythonSubscriptionCheck { - public static final String RULE_KEY = "EC72"; - // TODO: Handle ORM lib private static final List SQL_LIBS = Arrays.asList("cx_Oracle", "mysql.connector", "psycopg2", "pymssql", "pyodbc", "sqlite3"); diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheck.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheck.java index 43ae16492..be801c44d 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheck.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheck.java @@ -1,6 +1,5 @@ package fr.greencodeinitiative.python.checks; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.python.api.PythonSubscriptionCheck; import org.sonar.plugins.python.api.SubscriptionContext; @@ -8,16 +7,10 @@ import org.sonar.plugins.python.api.tree.TryStatement; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = AvoidTryCatchFinallyCheck.RULE_KEY, - name = "Avoid using try-catch statement", - description = AvoidTryCatchFinallyCheck.DESCRIPTION, - priority = Priority.MINOR, - tags = {"error-handling", "performance", "eco-design", "ecocode"}) +@Rule(key = "EC34") @DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "S34") public class AvoidTryCatchFinallyCheck extends PythonSubscriptionCheck { - public static final String RULE_KEY = "EC34"; public static final String DESCRIPTION = "Avoid the use of try-catch"; @Override diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidUnoptimizedVectorImagesCheck.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidUnoptimizedVectorImagesCheck.java index 5819eeec0..f3a9c3686 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidUnoptimizedVectorImagesCheck.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/AvoidUnoptimizedVectorImagesCheck.java @@ -3,21 +3,14 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -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.*; -@Rule( - key = AvoidUnoptimizedVectorImagesCheck.RULE_KEY, - name = AvoidUnoptimizedVectorImagesCheck.DESCRIPTION, - description = AvoidUnoptimizedVectorImagesCheck.DESCRIPTION, - priority = Priority.MINOR, - tags = {"eco-design", "ecocode"}) +@Rule(key = "EC10") public class AvoidUnoptimizedVectorImagesCheck extends PythonSubscriptionCheck { - public static final String RULE_KEY = "EC10"; public static final String DESCRIPTION = "Avoid using unoptimized vector images"; private static final Pattern LAYERS_PATTERN = Pattern.compile(""); 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 5cd3c48c7..a69599aed 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 org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.python.api.PythonSubscriptionCheck; import org.sonar.plugins.python.api.SubscriptionContext; @@ -10,12 +9,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -@Rule( - key = DetectUnoptimizedImageFormat.RULE_KEY, - name = DetectUnoptimizedImageFormat.MESSAGERULE, - description = DetectUnoptimizedImageFormat.MESSAGEERROR, - priority = Priority.MINOR, - tags = {"eco-design", "ecocode", "performance", "user-experience"}) +@Rule(key = "EC203") public class DetectUnoptimizedImageFormat extends PythonSubscriptionCheck { protected static final String RULE_KEY = "EC203"; diff --git a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/NoFunctionCallWhenDeclaringForLoop.java b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/NoFunctionCallWhenDeclaringForLoop.java index 5c7e09f05..b0bf30286 100644 --- a/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/NoFunctionCallWhenDeclaringForLoop.java +++ b/python-plugin/src/main/java/fr/greencodeinitiative/python/checks/NoFunctionCallWhenDeclaringForLoop.java @@ -1,22 +1,15 @@ package fr.greencodeinitiative.python.checks; -import org.sonar.check.Priority; import org.sonar.check.Rule; import org.sonar.plugins.python.api.PythonSubscriptionCheck; import org.sonar.plugins.python.api.tree.CallExpression; import org.sonar.plugins.python.api.tree.Tree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -@Rule( - key = NoFunctionCallWhenDeclaringForLoop.RULE_KEY, - name = NoFunctionCallWhenDeclaringForLoop.DESCRIPTION, - description = NoFunctionCallWhenDeclaringForLoop.DESCRIPTION, - priority = Priority.MINOR, - tags = {"performance", "eco-design", "ecocode"}) +@Rule(key = "EC69") @DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "S69") public class NoFunctionCallWhenDeclaringForLoop extends PythonSubscriptionCheck { - public static final String RULE_KEY = "EC69"; public static final String DESCRIPTION = "Do not call a function when declaring a for-type loop"; @Override From 9ab4abbcffc19701ef183e7632b3172e930e248e Mon Sep 17 00:00:00 2001 From: jycr Date: Fri, 14 Apr 2023 18:26:42 +0200 Subject: [PATCH 6/7] refactor: convert rule description from HTML to ASCIIDOC --- .../src/main/rules/EC1/java/EC1.asciidoc | 39 +- .../src/main/rules/EC10/python/EC10.asciidoc | 54 +-- .../src/main/rules/EC2/java/EC2.asciidoc | 53 +-- .../main/rules/EC203/python/EC203.asciidoc | 151 +++---- .../src/main/rules/EC22/php/EC22.asciidoc | 22 +- .../src/main/rules/EC27/java/EC27.asciidoc | 52 +-- .../src/main/rules/EC28/java/EC28.asciidoc | 57 +-- .../src/main/rules/EC3/java/EC3.asciidoc | 35 +- .../src/main/rules/EC3/php/EC3.asciidoc | 189 ++++---- .../src/main/rules/EC32/java/EC32.asciidoc | 40 +- .../src/main/rules/EC34/php/1GB.etsdiff.csv | 3 + .../src/main/rules/EC34/php/EC34.asciidoc | 114 ++--- .../src/main/rules/EC34/python/EC34.asciidoc | 22 +- .../src/main/rules/EC4/java/EC4.asciidoc | 71 +-- .../src/main/rules/EC4/php/EC4.asciidoc | 42 +- .../src/main/rules/EC4/python/EC4.asciidoc | 24 +- .../main/rules/EC404/python/EC404.asciidoc | 25 +- .../src/main/rules/EC5/java/EC5.asciidoc | 38 +- .../src/main/rules/EC53/java/EC53.asciidoc | 33 +- .../src/main/rules/EC63/java/EC63.asciidoc | 19 +- .../src/main/rules/EC66/php/1GB.etsdiff.csv | 3 + .../src/main/rules/EC66/php/EC66.asciidoc | 116 ++--- .../src/main/rules/EC66/python/EC66.asciidoc | 58 +-- .../src/main/rules/EC67/java/EC67.asciidoc | 18 +- .../src/main/rules/EC67/php/1GB.etsdiff.csv | 3 + .../src/main/rules/EC67/php/EC67.asciidoc | 112 ++--- .../src/main/rules/EC69/java/EC69.asciidoc | 39 +- .../src/main/rules/EC69/php/1GB.etsdiff.csv | 3 + .../src/main/rules/EC69/php/EC69.asciidoc | 110 +---- .../src/main/rules/EC69/python/EC69.asciidoc | 18 +- .../src/main/rules/EC7/python/EC7.asciidoc | 18 +- .../src/main/rules/EC72/java/EC72.asciidoc | 73 ++-- .../src/main/rules/EC72/php/1GB.etsdiff.csv | 3 + .../src/main/rules/EC72/php/2GB.etsdiff.csv | 3 + .../src/main/rules/EC72/php/4GB.etsdiff.csv | 3 + .../src/main/rules/EC72/php/8GB.etsdiff.csv | 3 + .../src/main/rules/EC72/php/EC72.asciidoc | 408 ++++-------------- .../src/main/rules/EC72/python/EC72.asciidoc | 40 +- .../src/main/rules/EC74/java/EC74.asciidoc | 34 +- .../src/main/rules/EC74/php/1GB.etsdiff.csv | 3 + .../src/main/rules/EC74/php/EC74.asciidoc | 129 ++---- .../src/main/rules/EC74/python/EC74.asciidoc | 34 +- .../src/main/rules/EC75/java/EC75.asciidoc | 57 ++- .../src/main/rules/EC76/java/EC76.asciidoc | 62 ++- .../src/main/rules/EC77/java/EC77.asciidoc | 88 ++-- .../src/main/rules/EC78/java/EC78.asciidoc | 60 +-- .../src/main/rules/EC79/java/EC79.asciidoc | 40 +- .../main/rules/etsdiff-methodology.asciidoc | 11 + 48 files changed, 1075 insertions(+), 1557 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/rules/EC34/php/1GB.etsdiff.csv create mode 100644 ecocode-rules-specifications/src/main/rules/EC66/php/1GB.etsdiff.csv create mode 100644 ecocode-rules-specifications/src/main/rules/EC67/php/1GB.etsdiff.csv create mode 100644 ecocode-rules-specifications/src/main/rules/EC69/php/1GB.etsdiff.csv create mode 100644 ecocode-rules-specifications/src/main/rules/EC72/php/1GB.etsdiff.csv create mode 100644 ecocode-rules-specifications/src/main/rules/EC72/php/2GB.etsdiff.csv create mode 100644 ecocode-rules-specifications/src/main/rules/EC72/php/4GB.etsdiff.csv create mode 100644 ecocode-rules-specifications/src/main/rules/EC72/php/8GB.etsdiff.csv create mode 100644 ecocode-rules-specifications/src/main/rules/EC74/php/1GB.etsdiff.csv create mode 100644 ecocode-rules-specifications/src/main/rules/etsdiff-methodology.asciidoc diff --git a/ecocode-rules-specifications/src/main/rules/EC1/java/EC1.asciidoc b/ecocode-rules-specifications/src/main/rules/EC1/java/EC1.asciidoc index c7bc959b3..91ae3f7f0 100644 --- a/ecocode-rules-specifications/src/main/rules/EC1/java/EC1.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC1/java/EC1.asciidoc @@ -1,20 +1,23 @@ -

The use of Spring repository in a loop induces unnecessary calculations by the CPU and therefore superfluous energy consumption.

-

Noncompliant Code Example

-
-		private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
+The use of Spring repository in a loop induces unnecessary calculations by the CPU and therefore superfluous energy consumption.
 
-		List<Employee> employees = new ArrayList<>();
-		
-		for (Integer id: ids) {
-            Optional<Employee> employee = employeeRepository.findById(id); // Noncompliant
-            if (employee.isPresent()) {
-                employees.add(employee.get());
-            }
-        }
+## Noncompliant Code Example
 
-
-

Compliant Solution

-
-		private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
-		List<Employee> employees = employeeRepository.findAllById(ids);
-
+```java +private final List ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + +List employees = new ArrayList<>(); + +for (Integer id: ids) { + Optional employee = employeeRepository.findById(id); // Noncompliant + if (employee.isPresent()) { + employees.add(employee.get()); + } +} +``` + +## Compliant Solution + +```java +private final List ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); +List employees = employeeRepository.findAllById(ids); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC10/python/EC10.asciidoc b/ecocode-rules-specifications/src/main/rules/EC10/python/EC10.asciidoc index 131ab21f8..cf2483171 100644 --- a/ecocode-rules-specifications/src/main/rules/EC10/python/EC10.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC10/python/EC10.asciidoc @@ -1,8 +1,10 @@ -

SVG images generated by common drawing softwares contains unnecessary data: calc layer, metadata, namespaces and comments.

-

Noncompliant Code Example

-
-<!-- Created with Inkscape (http://www.inkscape.org/) -->
-<svg
+SVG images generated by common drawing softwares contains unnecessary data: calc layer, metadata, namespaces and comments.
+
+## Noncompliant Code Example
+
+```xml
+
+
+    
+        
-

Compliant Solution

-
-<svg
-    width="210mm"
-    height="297mm"
-    viewBox="0 0 210 297"
-    xmlns="http://www.w3.org/2000/svg"
-    xmlns:svg="http://www.w3.org/2000/svg">
-    <g>
-        <circle
-            style="fill:#ff00ff;stroke-width:0.264583"
-            id="path111"
-            cx="104.02724"
-            cy="152.19028"
-            r="73.177132" />
-    </g>
-</svg>
-
+ r="73.177132" /> + + +``` + +## Compliant Solution + +```xml + + + +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC2/java/EC2.asciidoc b/ecocode-rules-specifications/src/main/rules/EC2/java/EC2.asciidoc index 7e5fcc2a0..0e56f2845 100644 --- a/ecocode-rules-specifications/src/main/rules/EC2/java/EC2.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC2/java/EC2.asciidoc @@ -1,31 +1,32 @@ -

If we are using too many conditional if-else statements it will impact performance since JVM will have to compare the conditions. We can think of using a switch statement instead of multiple if-else if possible. Switch statement has a performance advantage over if – else.

+If we are using too many conditional `if` – `else` statements it will impact performance since JVM will have to compare the conditions. We can think of using a switch statement instead of multiple `if` – `else` if possible. `switch` statement has a performance advantage over `if` – `else`. -

Non-compliant Code Example

-
-		int index = 1;
-        int nb = 2;
+## Non-compliant Code Example
 
-        if (nb > index) {
-            nb = nb + index;
-        } else {
-            nb = nb - 1;
-        }
-        if (nb != index + 1) {
-            nb = nb + index;
-        } else {
-            nb = nb - 1;
-        }
+```java
+int index = 1;
+int nb = 2;
 
+if (nb > index) {
+    nb = nb + index;
+} else {
+    nb = nb - 1;
+}
+if (nb != index + 1) {
+    nb = nb + index;
+} else {
+    nb = nb - 1;
+}
+```
 
-
-

Compliant Code Example

-
-        int index = 1;
-        int nb = 2;
+## Compliant Code Example
 
-        if (nb > index) {
-            nb = nb + index;
-        } else {
-            nb = nb - 1;
-        }
-
+```java +int index = 1; +int nb = 2; + +if (nb > index) { + nb = nb + index; +} else { + nb = nb - 1; +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC203/python/EC203.asciidoc b/ecocode-rules-specifications/src/main/rules/EC203/python/EC203.asciidoc index 7f9e458fd..85d38acb6 100644 --- a/ecocode-rules-specifications/src/main/rules/EC203/python/EC203.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC203/python/EC203.asciidoc @@ -1,73 +1,78 @@ -

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() {
-        ...
-        image_format = testImage("image.jpg")
-        ...
-    }
-
-

Compliant Solution

-
-    public void foo() {
-        ...
-        image_format = testImage("image.svg")
-        ...
-   }
-
- -

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>')
-        ...
-   }
-
+If possible, the utilisation of svg image format (or `` 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() { + // ... + image_format = testImage("image.jpg") + // ... +} +``` + +## Compliant Solution + +``` +public void foo() { + // ... + image_format = testImage("image.svg") + // ... +} +``` + +## Noncompliant Code Example + +``` +public void foo() { + // ... + return '' + // ... +} +``` + +## Compliant Solution + +``` +public void foo() { + // ... + return '' + // ... +} +``` + +Or + +``` + public void foo() { + // ... + return ('') + // ... +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC22/php/EC22.asciidoc b/ecocode-rules-specifications/src/main/rules/EC22/php/EC22.asciidoc index 0294f0a03..c42338993 100644 --- a/ecocode-rules-specifications/src/main/rules/EC22/php/EC22.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC22/php/EC22.asciidoc @@ -1,9 +1,13 @@ -

Use of methods for basic operations

-

Noncompliant Code Example

-
-	$min = min($a, $b);  // Noncompliant
-
-

Compliant Solution

-
-	$min = $a < $b ? $a : $b;
-
+Use of methods for basic operations + +## Noncompliant Code Example + +```php +$min = min($a, $b); // Noncompliant +``` + +## Compliant Solution + +```php +$min = $a < $b ? $a : $b; +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC27/java/EC27.asciidoc b/ecocode-rules-specifications/src/main/rules/EC27/java/EC27.asciidoc index 8bfb72e6b..455e0a137 100644 --- a/ecocode-rules-specifications/src/main/rules/EC27/java/EC27.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC27/java/EC27.asciidoc @@ -1,24 +1,28 @@ -

Using 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.
- For example, an array copy is potentially a non-performance source if it is poorly designed. Indeed, the use of a single copy loop can be twice as consuming as dedicated methods.
- Loops must be optimized to reduce processing time and make full use of hardware and processor mechanisms and optimizations.
- In the case of table copying (table), the native System.arraycopy.
- We can also use copyOf or clone that are slightly less efficient.
- The looping method will be outlawed. -

-

Noncompliant Code Example

-
-	int len = array.length;
-	boolean[] copy = new boolean[array.length];
-	for (int i = 0; i < len; i++) {
-  		copy[i] = array[i];  // Noncompliant
-	}
-	return copy;
-
-

Compliant Solution

-
-	int[] copy = new int[array.length];
-	System.arraycopy(array, 0, copy, 0, array.length);
-	return copy;
-
+Using `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. + +For example, an array copy is potentially a non-performance source if it is poorly designed. Indeed, the use of a single copy loop can be twice as consuming as dedicated methods. +Loops must be optimized to reduce processing time and make full use of hardware and processor mechanisms and optimizations. +In the case of table copying (table), use the native `System.arraycopy`. +We can also use `copyOf` or `clone` that are slightly less efficient. +The looping method will be outlawed. + +## Noncompliant Code Example + +```java +int len = array.length; +boolean[] copy = new boolean[array.length]; +for (int i = 0; i < len; i++) { + copy[i] = array[i]; // Noncompliant +} +return copy; +``` + +## Compliant Solution + +```java +int[] copy = new int[array.length]; +System.arraycopy(array, 0, copy, 0, array.length); +return copy; +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC28/java/EC28.asciidoc b/ecocode-rules-specifications/src/main/rules/EC28/java/EC28.asciidoc index d37bcef22..841ee9f8c 100644 --- a/ecocode-rules-specifications/src/main/rules/EC28/java/EC28.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC28/java/EC28.asciidoc @@ -1,29 +1,30 @@ -

Optimize read file exception

-

Noncompliant Code Example

-
-		public void readPreferences(String filename) {
-		  //...
-		  InputStream in = null;
-		  try {
-			in = new FileInputStream(filename);
-		  } catch (FileNotFoundException e) {
-			logger.log(e);
-		  }
-		  in.read(...);
-		  //...
-		}
+Optimize read file exception
 
-
-

Compliant Solution

-
-		public void readPreferences(String filename)
-			throws IllegalArgumentException,
-				   FileNotFoundException, IOException {
-		  if (filename == null) {
-			throw new IllegalArgumentException ("filename is null");
-		  }  //if
-		  //...
-		  InputStream in = new FileInputStream(filename);
-		  //...
-		}
-
+## Noncompliant Code Example + +```java +public void readPreferences(String filename) { + //... + InputStream in = null; + try { + in = new FileInputStream(filename); + } catch (FileNotFoundException e) { + logger.log(e); + } + in.read(...); + //... +} +``` + +## Compliant Solution + +```java +public void readPreferences(String filename) throws IllegalArgumentException, FileNotFoundException, IOException { + if (filename == null) { + throw new IllegalArgumentException ("filename is null"); + } + //... + InputStream in = new FileInputStream(filename); + //... +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC3/java/EC3.asciidoc b/ecocode-rules-specifications/src/main/rules/EC3/java/EC3.asciidoc index b347d7141..49fcb223e 100644 --- a/ecocode-rules-specifications/src/main/rules/EC3/java/EC3.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC3/java/EC3.asciidoc @@ -1,19 +1,22 @@ -

When iterating over any collection, fetch the size of the collection in advance to avoid fetching it on each iteration, this saves CPU cycles, and therefore consumes less power. The example provided below illustrates what should be avoided.

-

Noncompliant Code Example

-
-		List<String> objList = getData();
+When iterating over any collection, fetch the size of the collection in advance to avoid fetching it on each iteration, this saves CPU cycles, and therefore consumes less power. The example provided below illustrates what should be avoided.
 
-        for (int i = 0; i < objList.size(); i++) {  // Noncompliant
-            // execute code
-        }
+## Noncompliant Code Example
 
-
-

Compliant Solution

-
-        List<String> objList = getData();
+```java
+List objList = getData();
 
-        int size = objList.size();
-        for (int i = 0; i < size; i++) {
-            // execute code
-        }
-
+for (int i = 0; i < objList.size(); i++) { // Noncompliant + // execute code +} +``` + +## Compliant Solution + +```java +List objList = getData(); + +int size = objList.size(); +for (int i = 0; i < size; i++) { + // execute code +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC3/php/EC3.asciidoc b/ecocode-rules-specifications/src/main/rules/EC3/php/EC3.asciidoc index 3eace53ff..9c4d5bb81 100644 --- a/ecocode-rules-specifications/src/main/rules/EC3/php/EC3.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC3/php/EC3.asciidoc @@ -1,93 +1,96 @@ -

When iterating over any collection, fetch the size of the collection in advance to avoid fetching it on each iteration, this saves CPU cycles, and therefore consumes less power.

-

NB : note that we are using the count() method to get the size of an array but it would work the same with the sizeof() and iterator_count() methods

- -

Noncompliant Code Examples

-
-	$array = array('orange', 'banana', 'apple', 'carrot', 'collard', 'pea');
-
-	// FOR STATEMENTS / Right operand
-	for ($i = 0; $i < count($array); ++$i) {
-		var_dump($array[$i]);
-	}
-
-	// FOR STATEMENTS / Left operand
-	for ($i = 0; count($array) > $i; ++$i) {
-		var_dump($array[$i]);
-	}
-
-	// WHILE STATEMENTS / Right operand
-	$i = 0;
-	while($i < count($array)) {
-		var_dump($array[$i]);
-		++$i;
-	}
-
-	// WHILE STATEMENTS / Left operand
-	$i = 0;
-	while(count($array) > $i) {
-		var_dump($array[$i]);
-		++$i;
-	}
-
-	// DO WHILE STATEMENTS / Right operand
-	$i = 0;
-	do {
-		var_dump($array[$i]);
-		++$i;
-	} while ($i < count($array));
-
-	// DO WHILE STATEMENTS / Left operand
-	$i = 0;
-	do {
-		var_dump($array[$i]);
-		++$i;
-	} while (count($array) > $i);
-
- -

Compliant Solution

-
-	$array = array('orange', 'banana', 'apple', 'carrot', 'collard', 'pea');
-	// FOR STATEMENTS / Right operand
-	$size = sizeof($array);
-	for ($i = 0; $i < $size; ++$i) {
-		var_dump($array[$i]);
-	}
-
-	// FOR STATEMENTS / Left operand
-	$size = sizeof($array);
-	for ($i = 0; $size > $i; ++$i) {
-		var_dump($array[$i]);
-	}
-
-	// WHILE STATEMENTS / Right operand
-	$i = 0;
-	$size = count($array);
-	while($i < $size) {
-		var_dump($array[$i]);
-		++$i;
-	}
-
-	// WHILE STATEMENTS / Left operand
-	$i = 0;
-	$size = count($array);
-	while($size > $i) {
-		var_dump($array[$i]);
-		++$i;
-	}
-
-	// DO WHILE STATEMENTS / Right operand
-	$i = 0;
-	$size = count($array);
-	do {
-		var_dump($array[$i]);
-		++$i;
-	} while ($i < $size);
-
-	// DO WHILE STATEMENTS / Left operand
-	$i = 0;
-	$size = count($array);
-	do {
-		var_dump($array[$i]);
-		++$i;
-	} while ($size > $i);
-
+When iterating over any collection, fetch the size of the collection in advance to avoid fetching it on each iteration, this saves CPU cycles, and therefore consumes less power. + +NB : note that we are using the `count()` method to get the size of an array but it would work the same with the `sizeof()` and `iterator_count()` methods. + +## Noncompliant Code Example + +```php +$array = array('orange', 'banana', 'apple', 'carrot', 'collard', 'pea'); + +// FOR STATEMENTS / Right operand +for ($i = 0; $i < count($array); ++$i) { + var_dump($array[$i]); +} + +// FOR STATEMENTS / Left operand +for ($i = 0; count($array) > $i; ++$i) { + var_dump($array[$i]); +} + +// WHILE STATEMENTS / Right operand +$i = 0; +while($i < count($array)) { + var_dump($array[$i]); + ++$i; +} + +// WHILE STATEMENTS / Left operand +$i = 0; +while(count($array) > $i) { + var_dump($array[$i]); + ++$i; +} + +// DO WHILE STATEMENTS / Right operand +$i = 0; +do { + var_dump($array[$i]); + ++$i; +} while ($i < count($array)); + +// DO WHILE STATEMENTS / Left operand +$i = 0; +do { + var_dump($array[$i]); + ++$i; +} while (count($array) > $i); +``` + +## Compliant Solution + +```php +$array = array('orange', 'banana', 'apple', 'carrot', 'collard', 'pea'); +// FOR STATEMENTS / Right operand +$size = sizeof($array); +for ($i = 0; $i < $size; ++$i) { + var_dump($array[$i]); +} + +// FOR STATEMENTS / Left operand +$size = sizeof($array); +for ($i = 0; $size > $i; ++$i) { + var_dump($array[$i]); +} + +// WHILE STATEMENTS / Right operand +$i = 0; +$size = count($array); +while($i < $size) { + var_dump($array[$i]); + ++$i; +} + +// WHILE STATEMENTS / Left operand +$i = 0; +$size = count($array); +while($size > $i) { + var_dump($array[$i]); + ++$i; +} + +// DO WHILE STATEMENTS / Right operand +$i = 0; +$size = count($array); +do { + var_dump($array[$i]); + ++$i; +} while ($i < $size); + +// DO WHILE STATEMENTS / Left operand +$i = 0; +$size = count($array); +do { + var_dump($array[$i]); + ++$i; +} while ($size > $i); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC32/java/EC32.asciidoc b/ecocode-rules-specifications/src/main/rules/EC32/java/EC32.asciidoc index 253090269..00c2ac907 100644 --- a/ecocode-rules-specifications/src/main/rules/EC32/java/EC32.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC32/java/EC32.asciidoc @@ -1,19 +1,21 @@ -

- If you know in advance how many characters would be appended, initialize builder/buffer with the appropriate size. - They will thus never have to be resized. - This saves CPU cycles and therefore consumes less energy. -

-

Noncompliant Code Example

-
-    StringBuilder sb = new StringBuilder(); // Noncompliant
-    for (int i = 0; i < 100; i++) {
-       sb.append(...);
-    }
-
-

Compliant Solution

-
-    StringBuilder sb = new StringBuilder(100);
-    for (int i = 0; i < 100; i++) {
-       sb.append(...);
-    }
-
+If you know in advance how many characters would be appended, initialize builder/buffer with the appropriate size. +They will thus never have to be resized. +This saves CPU cycles and therefore consumes less energy. + +## Noncompliant Code Example + +```java +StringBuilder sb = new StringBuilder(); // Noncompliant +for (int i = 0; i < 100; i++) { + sb.append(...); +} +``` + +## Compliant Solution + +```java +StringBuilder sb = new StringBuilder(100); +for (int i = 0; i < 100; i++) { + sb.append(...); +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC34/php/1GB.etsdiff.csv b/ecocode-rules-specifications/src/main/rules/EC34/php/1GB.etsdiff.csv new file mode 100644 index 000000000..5b56c3789 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC34/php/1GB.etsdiff.csv @@ -0,0 +1,3 @@ +Energy (J),515.855638,516.9188409999999 +Transfer (B),1579453,1579457 +Storage (B),637549804,637549804 diff --git a/ecocode-rules-specifications/src/main/rules/EC34/php/EC34.asciidoc b/ecocode-rules-specifications/src/main/rules/EC34/php/EC34.asciidoc index 5b8bd83c2..f7b2ab71d 100644 --- a/ecocode-rules-specifications/src/main/rules/EC34/php/EC34.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC34/php/EC34.asciidoc @@ -1,7 +1,10 @@ -

Inside complex code parts (for exemple multiple loops, complex data constructions...), avoid using try...catch...finally.

-

When an exception is thrown, a variable (the exception itself) is created in a catch block and it's destruction consumes unnecessary CPU cycles and RAM. Prefer using logical tests in this cases.

-

Noncompliant Code Example

-
+Inside complex code parts (for exemple multiple loops, complex data constructions...), avoid using try...catch...finally.
+
+When an exception is thrown, a variable (the exception itself) is created in a catch block and it's destruction consumes unnecessary CPU cycles and RAM. Prefer using logical tests in this cases.
+
+## Noncompliant Code Example
+
+```php
 try
 {
   $picture = PDF_open_image_file($PDF, "jpeg", $imgFile, "", 0); // This is the original statement, this works on PHP4
@@ -11,10 +14,11 @@ catch(Exception $ex)
   $msg = "Error opening $imgFile for Product $row['Identifier']";
   throw new Exception($msg);
 }
+```
 
-
-

Compliant Solution

-
+## Compliant Solution
+
+```php
 //try
 if (file_exists($imgFile)) {
     $picture = PDF_open_image_file($PDF, "jpeg", $imgFile, "", 0);
@@ -25,85 +29,17 @@ if (!$picture) {
    $msg = "Error opening $imgFile for Product $row['Identifier']";
    print $msg;
 }
-
-

The three sources of impacts of a code identified are:

-- Energy (measured in joules) -
- Transfer (measured in Bytes) -
- Storage (measured in Bytes)
-
The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption. -
The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment). -
The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed. -
-

Case for a 1GB database:

-
- ETSdiff percent comparison -
-

Total:

- - - - - - - - - - - - - - - - - - - - - - - -
-
-
Compliant
-
-
-
-
Non-compliant
-
-
-
-
Energy
-
-
-
-
515.855638
-
-
-
-
516.9188409999999
-
-
-
-
Transfer
-
-
-
-
1579453
-
-
-
-
1579457
-
-
-
-
Storage
-
-
-
-
637549804
-
-
-
-
637549804
-
-
+``` + +include::../../etsdiff-methodology.asciidoc[] + +## Case for a 1GB database: + +image::https://live.staticflickr.com/65535/52622382871_f19da08db4_o.png[ETSdiff percent comparison] + +[format=csv,cols="1h,1,1"] +|=== +Source of impacts,Compliant,Non-compliant + +include::1GB.etsdiff.csv[] +|=== diff --git a/ecocode-rules-specifications/src/main/rules/EC34/python/EC34.asciidoc b/ecocode-rules-specifications/src/main/rules/EC34/python/EC34.asciidoc index d90cacc9e..14b3c3c5b 100644 --- a/ecocode-rules-specifications/src/main/rules/EC34/python/EC34.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC34/python/EC34.asciidoc @@ -1,8 +1,10 @@ -

Inside complex code parts (for example multiple loops, complex data constructions...), avoid using try...catch...finally. -

When an exception is thrown, a variable (the exception itself) is created in a catch block, and it's destruction consumes unnecessary CPU cycles and RAM. Prefer using logical tests in this cases.

-

-

Noncompliant Code Example

-
+Inside complex code parts (for example multiple loops, complex data constructions...), avoid using try...catch...finally.
+
+When an exception is thrown, a variable (the exception itself) is created in a catch block, and it's destruction consumes unnecessary CPU cycles and RAM. Prefer using logical tests in this cases.
+
+## Noncompliant Code Example
+
+```python
 try:
     f = open(path)
     print(fh.read())
@@ -10,12 +12,14 @@ except:
     print('No such file '+path
 finally:
     f.close()
+```
 
-
-

Compliant Solution

-
+## Compliant Solution
+
+```python
 if os.path.isfile(path):
   fh = open(path, 'r')
   print(fh.read())
   fh.close
-
+``` + diff --git a/ecocode-rules-specifications/src/main/rules/EC4/java/EC4.asciidoc b/ecocode-rules-specifications/src/main/rules/EC4/java/EC4.asciidoc index e95fd679d..f8763b4ed 100644 --- a/ecocode-rules-specifications/src/main/rules/EC4/java/EC4.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC4/java/EC4.asciidoc @@ -1,34 +1,37 @@ -

- Prefer local variables as parameters -

-

When calling a global variable, the interpretation engine must check that it exists in all the scopes, that it has a value, etc. Passing global variables as arguments gives them the status of local variables inside the function, thus saving computing time (CPU cycles). -

-

-CASE 1 (Avoid as possible):
-You are back on the service code. You see that the func1() uses globalVariabl1. Okay, but whats its value by now ? How does it change ? Who mutates the globalVariabl1 before it comes to this function ? What have been the sequence of all these mutations ? You would have no idea. It will be quite difficult to figure all this out. -
-CASE 2 (Recommended):
-You are back to you code, and see that the func0() fetches something and then passes it to func1(param1) as a parameter. You clearly know what the data is, how does it gets here. -

-

Noncompliant Code Example

-
-    var aGlobal = new String('Hello');
-
-    function globalLength(){
-        length = aGlobal.length;
-        console.log(length);
-    }
-
-    globalLength();
-
-

Compliant Solution

-
-    var aGlobal = new String('Hello');
-
-    function someVarLength(str){
-        length = str.length;
-        console.log(length);
-    }
-
-    somVarLength(aGlobal);
-
\ No newline at end of file +Prefer local variables as parameters + +When calling a global variable, the interpretation engine must check that it exists in all the scopes, that it has a value, etc. Passing global variables as arguments gives them the status of local variables inside the function, thus saving computing time (CPU cycles). + +## CASE 1 (Avoid as possible) + +You are back on the service code. You see that the `func1()` uses `globalVariabl1`. Okay, but whats its value by now ? How does it change ? Who mutates the `globalVariabl1` before it comes to this function ? What have been the sequence of all these mutations ? You would have no idea. It will be quite difficult to figure all this out. + +## CASE 2 (Recommended) + +You are back to you code, and see that the `func0()` fetches something and then passes it to `func1(param1)` as a parameter. You clearly know what the data is, how does it gets here. + +## Noncompliant Code Example + +```java +var aGlobal = new String('Hello'); + +function globalLength(){ + length = aGlobal.length; + console.log(length); +} + +globalLength(); +``` + +## Compliant Solution + +```java +var aGlobal = new String('Hello'); + +function someVarLength(str){ + length = str.length; + console.log(length); +} + +somVarLength(aGlobal); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC4/php/EC4.asciidoc b/ecocode-rules-specifications/src/main/rules/EC4/php/EC4.asciidoc index 6e9687fa7..94021a9e6 100644 --- a/ecocode-rules-specifications/src/main/rules/EC4/php/EC4.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC4/php/EC4.asciidoc @@ -1,27 +1,29 @@ -

- Prefer local variables as parameters -

-

When calling a global variable, the interpretation engine must check that it exists in all the scopes, that it has a value, etc. Passing global variables as arguments gives them the status of local variables inside the function, thus saving computing time (CPU cycles). -

-

Noncompliant Code Example

-
-  var aGlobal = new String('Hello');
-
-  function globalLength(){
+Prefer local variables as parameters
+
+When calling a global variable, the interpretation engine must check that it exists in all the scopes, that it has a value, etc. Passing global variables as arguments gives them the status of local variables inside the function, thus saving computing time (CPU cycles).
+
+## Noncompliant Code Example
+
+```php
+var aGlobal = new String('Hello');
+
+function globalLength(){
     length = aGlobal.length;
     console.log(length);
-  }
+}
+
+globalLength();
+```
+
+## Compliant Solution
 
-  globalLength();
-
-

Compliant Solution

-
-  var aGlobal = new String('Hello');
+```php
+var aGlobal = new String('Hello');
 
-  function someVarLength(str){
+function someVarLength(str){
     length = str.length;
     console.log(length);
-  }
+}
 
-  somVarLength(aGlobal);
-
\ No newline at end of file +somVarLength(aGlobal); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC4/python/EC4.asciidoc b/ecocode-rules-specifications/src/main/rules/EC4/python/EC4.asciidoc index c92e7e5be..b777840a9 100644 --- a/ecocode-rules-specifications/src/main/rules/EC4/python/EC4.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC4/python/EC4.asciidoc @@ -1,20 +1,24 @@ -

When function calls global variables, a lot a CPU cycles is consumed.

-

Noncompliant Code Example

-
+When function calls global variables, a lot a CPU cycles is consumed.
+
+## Noncompliant Code Example
+
+```python
 global_var = 'foo'
 def print_global_var_details():
     print(len(global_var)) # Noncompliant
     print('Global var : ', global_var) # Noncompliant
     print('Global var : ' + global_var) # Noncompliant
 print_global_var_details()
-
-

Compliant Solution

-
+```
+
+## Compliant Solution
+
+```python
 global_var = 'foo';
 def print_var_details(local_var) {
-  print(len(local_var));
-  print('Var : ', local_var)
-  print('Var : ' + local_var)
+    print(len(local_var));
+    print('Var : ', local_var)
+    print('Var : ' + local_var)
 }
 print_length(global_var);
-
+``` diff --git a/ecocode-rules-specifications/src/main/rules/EC404/python/EC404.asciidoc b/ecocode-rules-specifications/src/main/rules/EC404/python/EC404.asciidoc index f7c47727f..4a0738f9f 100644 --- a/ecocode-rules-specifications/src/main/rules/EC404/python/EC404.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC404/python/EC404.asciidoc @@ -1,13 +1,20 @@ -

Use generator comprehension instead of list comprehension in for loop declaration.

-

Python generators resemble lazy lists from other programming languages: when iterated over, they compute their values on the fly. They lack some list behaviors (indexing, len method, ...) but are memory-efficient, as they do not store each of their values in memory, unlike lists. Thus, when declared in a for-loop declaration, list comprehensions can be safely replaced with generator comprehensions.

-

For more details on list comprehensions vs generator comprehensions, see Python documentation.

-

Noncompliant Code Example

-
+Use generator comprehension instead of list comprehension in for loop declaration.
+
+Python generators resemble lazy lists from other programming languages: when iterated over, they compute their values on the fly. They lack some list behaviors (indexing, len method, ...) but are memory-efficient, as they do not store each of their values in memory, unlike lists. Thus, when declared in a for-loop declaration, list comprehensions can be safely replaced with generator comprehensions.
+
+For more details on list comprehensions vs generator comprehensions, see https://docs.python.org/3/howto/functional.html#generator-expressions-and-list-comprehensions[Python documentation].
+
+## Noncompliant Code Example
+
+```python
 for var in [var2 for var2 in range(100)]:
     ...
-
-

Compliant Solution

-
+
+```
+
+## Compliant Solution
+
+```python
 for var in (var2 for var2 in range(100)):
     ...
-
+``` diff --git a/ecocode-rules-specifications/src/main/rules/EC5/java/EC5.asciidoc b/ecocode-rules-specifications/src/main/rules/EC5/java/EC5.asciidoc index 3fd5bb834..229a3503d 100644 --- a/ecocode-rules-specifications/src/main/rules/EC5/java/EC5.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC5/java/EC5.asciidoc @@ -1,18 +1,22 @@ -

Use PreparedStatement instead of Statement, because 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.

-

Noncompliant Code Example

-
-    public void select() {
-        Statement statement = connection.createStatement();
-        statement.executeUpdate("INSERT INTO persons(id, name) VALUES(2, 'Toto')");  // Noncompliant
-    }
-
-

Compliant Solution

-
-    public void select() {
-        PreparedStatement statement = connection.prepareStatement(INSERT INTO persons(id, name) VALUES(?, ?));
+Use `PreparedStatement` instead of `Statement`, because 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.
 
-        statement.setInt(1, 2);
-        statement.setString(2, "Toto");
-        statement.executeQuery();
-    }
-
+## Noncompliant Code Example + +```java +public void select() { + Statement statement = connection.createStatement(); + statement.executeUpdate("INSERT INTO persons(id, name) VALUES(2, 'John DOE')"); // Noncompliant +} +``` + +## Compliant Solution + +```java +public void select() { + PreparedStatement statement = connection.prepareStatement(INSERT INTO persons(id, name) VALUES(?, ?)); + + statement.setInt(1, 2); + statement.setString(2, "John DOE"); + statement.executeQuery(); +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC53/java/EC53.asciidoc b/ecocode-rules-specifications/src/main/rules/EC53/java/EC53.asciidoc index b1d548c92..924efda63 100644 --- a/ecocode-rules-specifications/src/main/rules/EC53/java/EC53.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC53/java/EC53.asciidoc @@ -1,18 +1,21 @@ -

Using List instead of Arrays with Foreach save CPU cycles calculations and RAM consumption.

-

Noncompliant Code Example

-
-		private final Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+Using List instead of Arrays with Foreach save CPU cycles calculations and RAM consumption.
 
-		for (Integer i : intArray) {
-			...
-		}
+## Noncompliant Code Example
 
-
-

Compliant Solution

-
-		private final List<Integer> intList = Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
+```java
+private final Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
-		for (Integer i : intList) {
-			...
-		}
-
+for (Integer i : intArray) { + // ... +} +``` + +## Compliant Solution + +```java +private final List intList = Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); + +for (Integer i : intList) { + // ... +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC63/java/EC63.asciidoc b/ecocode-rules-specifications/src/main/rules/EC63/java/EC63.asciidoc index be6d491d4..9d3ca796d 100644 --- a/ecocode-rules-specifications/src/main/rules/EC63/java/EC63.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC63/java/EC63.asciidoc @@ -1,12 +1,17 @@ -

Do not unnecessarily assign values to variables. It increases the use of RAM memory.

-

Noncompliant Code Example

-
+Do not unnecessarily assign values to variables. It increases the use of RAM memory.
+
+## Noncompliant Code Example
+
+```java
 String var1 = getValue();
 return var1;
 
 String var2 = "hello"
-var2 = "world"        //Non compliant cause never assigned 
+var2 = "world"        //Non compliant cause never assigned
+```
+
+## Compliant Solution
 
-
-

Compliant Solution

-
return getValue();
+```java +return getValue(); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC66/php/1GB.etsdiff.csv b/ecocode-rules-specifications/src/main/rules/EC66/php/1GB.etsdiff.csv new file mode 100644 index 000000000..4765a99fe --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC66/php/1GB.etsdiff.csv @@ -0,0 +1,3 @@ +Energy (J),3.041966,1.2651545000000002 +Transfer (B),68520884,68588123 +Storage (B),637548795,637548795 diff --git a/ecocode-rules-specifications/src/main/rules/EC66/php/EC66.asciidoc b/ecocode-rules-specifications/src/main/rules/EC66/php/EC66.asciidoc index 233604feb..8af551481 100644 --- a/ecocode-rules-specifications/src/main/rules/EC66/php/EC66.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC66/php/EC66.asciidoc @@ -1,94 +1,30 @@ -

PHP allows declaring a string with simple or double quotes. Using double quotes allows developers to insert variables which will be substituted during execution. When the string has no variables, using single quotes prevents PHP from searching for non-existent variables. It will save CPU cycles consumption and RAM usage.

-

Noncompliant Code Example

-
+PHP allows declaring a string with simple or double quotes. Using double quotes allows developers to insert variables which will be substituted during execution. When the string has no variables, using single quotes prevents PHP from searching for non-existent variables. It will save CPU cycles consumption and RAM usage.
+
+## Noncompliant Code Example
+
+```php
 myFunction("name", "age", "IsStudent");
-  $lastName = "Hugo";
-  $concatenatedString = "$lastName is a student";
-
-

Compliant Solution

-
+    $lastName = "Hugo";
+    $concatenatedString = "$lastName is a student";
+```
+
+## Compliant Solution
+
+```php
 myFunction('name', 'age', 'IsStudent');
     $lastName = 'Hugo';
     $concatenatedString = $lastName . 'is a student';
-
-

The three sources of impacts of a code identified are:

-- Energy (measured in joules) -
- Transfer (measured in Bytes) -
- Storage (measured in Bytes)
-
The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption. -
The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment). -
The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed. -
-

Case for a 1GB database:

-
- ETSdiff percent comparison -
-

Total:

- - - - - - - - - - - - - - - - - - - - - - - -
-
-
Compliant
-
-
-
-
Non-compliant
-
-
-
-
Energy
-
-
-
-
3.041966
-
-
-
-
1.2651545000000002
-
-
-
-
Transfer
-
-
-
-
68520884
-
-
-
-
68588123
-
-
-
-
Storage
-
-
-
-
637548795
-
-
-
-
637548795
-
-
+``` + +include::../../etsdiff-methodology.asciidoc[] + +## Case for a 1GB database: + +image::https://live.staticflickr.com/65535/52621866212_de15608a41_o.png[ETSdiff percent comparison] + +[format=csv,cols="1h,1,1"] +|=== +Source of impacts,Compliant,Non-compliant + +include::1GB.etsdiff.csv[] +|=== diff --git a/ecocode-rules-specifications/src/main/rules/EC66/python/EC66.asciidoc b/ecocode-rules-specifications/src/main/rules/EC66/python/EC66.asciidoc index 0a94f3529..09e0eda41 100644 --- a/ecocode-rules-specifications/src/main/rules/EC66/python/EC66.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC66/python/EC66.asciidoc @@ -1,27 +1,31 @@ -

- The shape using the quotation marks (") allows the developer to insert variables that will be substituted at run time.
- But if the string does not have a variable, use quotes (') instead.
- Thus, language will not look for variables to substitute, which will reduce the consumption of CPU cycles. -

-

Noncompliant Code Example

-
-    # in variables
-    firstname = "Andrea" # Noncompliant {{Avoid using quotation mark ("), prefer using simple quote (')}}
-
-    # in functions
-    def my_function(name, age):
-        print(name + 'is' + age + ' yo.')
-
-    my_function("Robert", 12) # Noncompliant {{Avoid using quotation mark ("), prefer using simple quote (')}}
-
-

Compliant Solution

-
-    # in variables
-    firstname = 'Andrea'
-
-    # in functions
-    def my_function(name, age):
-        print(name + 'is' + age + ' yo.')
-
-    my_function('Robert', 12)
-
+The shape using the quotation marks (") allows the developer to insert variables that will be substituted at run time. + +But if the string does not have a variable, use quotes (') instead. + +Thus, language will not look for variables to substitute, which will reduce the consumption of CPU cycles. + +## Noncompliant Code Example + +```python +# in variables +firstname = "Andrea" # Noncompliant {{Avoid using quotation mark ("), prefer using simple quote (')}} + +# in functions +def my_function(name, age): + print(name + 'is' + age + ' yo.') + +my_function("Robert", 12) # Noncompliant {{Avoid using quotation mark ("), prefer using simple quote (')}} +``` + +## Compliant Solution + +```python +# in variables +firstname = 'Andrea' + +# in functions +def my_function(name, age): + print(name + 'is' + age + ' yo.') + +my_function('Robert', 12) +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC67/java/EC67.asciidoc b/ecocode-rules-specifications/src/main/rules/EC67/java/EC67.asciidoc index 3c55323e5..a0c7b961a 100644 --- a/ecocode-rules-specifications/src/main/rules/EC67/java/EC67.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC67/java/EC67.asciidoc @@ -1,7 +1,13 @@ -

The form $i++ creates a temporary variable whereas ++$i does not. It save CPU cycles.

-

Noncompliant Code Example

-
+The form `$i++` creates a temporary variable whereas `++$i` does not. It save CPU cycles.
+
+## Noncompliant Code Example
+
+```java
 i++  // Noncompliant
-
-

Compliant Solution

-
++i
+``` + +## Compliant Solution + +```java +++i +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC67/php/1GB.etsdiff.csv b/ecocode-rules-specifications/src/main/rules/EC67/php/1GB.etsdiff.csv new file mode 100644 index 000000000..c79e54fc0 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC67/php/1GB.etsdiff.csv @@ -0,0 +1,3 @@ +Energy (J),1.8163645000000002,0.2613885000000001 +Transfer (B),11265758,11290494 +Storage (B),637548673,637548673 diff --git a/ecocode-rules-specifications/src/main/rules/EC67/php/EC67.asciidoc b/ecocode-rules-specifications/src/main/rules/EC67/php/EC67.asciidoc index 6e087ebf4..9a025ea99 100644 --- a/ecocode-rules-specifications/src/main/rules/EC67/php/EC67.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC67/php/EC67.asciidoc @@ -1,88 +1,26 @@ -

The form $i++ creates a temporary variable whereas ++$i does not. It save CPU cycles.

-

Noncompliant Code Example

-
+The form `$i++` creates a temporary variable whereas `++$i` does not. It save CPU cycles.
+
+## Noncompliant Code Example
+
+```php
 $i++
-
-

Compliant Solution

-
++$i
-

The three sources of impacts of a code identified are:

-- Energy (measured in joules) -
- Transfer (measured in Bytes) -
- Storage (measured in Bytes)
-
The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption. -
The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment). -
The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed. -
-

Case for a 1GB database:

-
- ETSdiff percent comparison -
-

Total:

- - - - - - - - - - - - - - - - - - - - - - - -
-
-
Compliant
-
-
-
-
Non-compliant
-
-
-
-
Energy
-
-
-
-
1.8163645000000002
-
-
-
-
0.2613885000000001
-
-
-
-
Transfer
-
-
-
-
11265758
-
-
-
-
11290494
-
-
-
-
Storage
-
-
-
-
637548673
-
-
-
-
637548673
-
-
\ No newline at end of file +``` + +## Compliant Solution + +```php +++$i +``` + +include::../../etsdiff-methodology.asciidoc[] + +## Case for a 1GB database: + +image::https://live.staticflickr.com/65535/52622379586_f84c767111_o.png[ETSdiff percent comparison] + +[format=csv,cols="1h,1,1"] +|=== +Source of impacts,Compliant,Non-compliant + +include::1GB.etsdiff.csv[] +|=== diff --git a/ecocode-rules-specifications/src/main/rules/EC69/java/EC69.asciidoc b/ecocode-rules-specifications/src/main/rules/EC69/java/EC69.asciidoc index 5d8c46aa1..e32cd94db 100644 --- a/ecocode-rules-specifications/src/main/rules/EC69/java/EC69.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC69/java/EC69.asciidoc @@ -1,23 +1,24 @@ -

Do not call a function when declaring a for-type loop in order to avoid function calls each iterations. It saves CPU cycles.

-

Noncompliant Code Example

-
-    public void foo() {
-        for (int i = 0; i < getMyValue(); i++) {  // Noncompliant
-            System.out.println(i);
-            boolean b = getMyValue() > 6;
-        }
-    }
+Do not call a function when declaring a for-type loop in order to avoid function calls each iterations. It saves CPU cycles.
 
-
-

Compliant Solution

-
+## Noncompliant Code Example
 
-    public void foo() {
-        int myValue =  getMyValue();
-        for (int i = 0; i < myValue; i++) {
-            System.out.println(i);
-            boolean b = getMyValue() > 6;
-        }
+```java
+public void foo() {
+    for (int i = 0; i < getMyValue(); i++) {  // Noncompliant
+        System.out.println(i);
+        boolean b = getMyValue() > 6;
     }
+}
+```
+
+## Compliant Solution
 
-
+```java +public void foo() { + int myValue = getMyValue(); + for (int i = 0; i < myValue; i++) { + System.out.println(i); + boolean b = getMyValue() > 6; + } +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC69/php/1GB.etsdiff.csv b/ecocode-rules-specifications/src/main/rules/EC69/php/1GB.etsdiff.csv new file mode 100644 index 000000000..227f4def5 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC69/php/1GB.etsdiff.csv @@ -0,0 +1,3 @@ +Energy (J),144.635057,144.58341249999998 +Transfer (B),50000,50004 +Storage (B),637549590,637549590 diff --git a/ecocode-rules-specifications/src/main/rules/EC69/php/EC69.asciidoc b/ecocode-rules-specifications/src/main/rules/EC69/php/EC69.asciidoc index c71d96a37..0da3cde83 100644 --- a/ecocode-rules-specifications/src/main/rules/EC69/php/EC69.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC69/php/EC69.asciidoc @@ -1,12 +1,16 @@ -

Do not call a function when declaring a for-type loop in order to avoid function calls each iteration. It saves CPU cycles.

-

Noncompliant Code Example

-
+Do not call a function when declaring a for-type loop in order to avoid function calls each iteration. It saves CPU cycles.
+
+## Noncompliant Code Example
+
+```php
 for ($i = 0; $i <= foo(); $i++) {  // Noncompliant
 	// ......
 }
-
-

Compliant Solution

-
+```
+
+## Compliant Solution
+
+```php
 $maxI = foo();
 for ($i = 0; $i <= $maxI; $i++) {
   .....
@@ -17,88 +21,18 @@ for ($i = 0; $i <= $maxI; $i++) {
 for ($i = 0, $maxI = foo(); $i <= $maxI; $i++) {
   .....
 }
-}
+} +``` + +include::../../etsdiff-methodology.asciidoc[] + +## Case for a 1GB database: -

The three sources of impacts of a code identified are:

-- Energy (measured in joules) -
- Transfer (measured in Bytes) -
- Storage (measured in Bytes)
-
The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption. -
The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment). -
The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed. -
+image::https://live.staticflickr.com/65535/52622634654_bf3c3d9ba8_o.png[ETSdiff percent comparison] -

Case for a 1GB database:

-
- ETSdiff percent comparison -
+[format=csv,cols="1h,1,1"] +|=== +Source of impacts,Compliant,Non-compliant -

Total:

- - - - - - - - - - - - - - - - - - - - - - - -
-
-
Compliant
-
-
-
-
Non-compliant
-
-
-
-
Energy
-
-
-
-
144.635057
-
-
-
-
144.58341249999998
-
-
-
-
Transfer
-
-
-
-
50000
-
-
-
-
50004
-
-
-
-
Storage
-
-
-
-
637549590
-
-
-
-
637549590
-
-
+include::1GB.etsdiff.csv[] +|=== diff --git a/ecocode-rules-specifications/src/main/rules/EC69/python/EC69.asciidoc b/ecocode-rules-specifications/src/main/rules/EC69/python/EC69.asciidoc index 2c3c64ff1..3d73ce30a 100644 --- a/ecocode-rules-specifications/src/main/rules/EC69/python/EC69.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC69/python/EC69.asciidoc @@ -1,13 +1,17 @@ -

Do not call a function when declaring a for-type loop in order to avoid function calls each iteration. It saves CPU cycles.

-

Noncompliant Code Example

-
+Do not call a function when declaring a for-type loop in order to avoid function calls each iteration. It saves CPU cycles.
+
+## Noncompliant Code Example
+
+```python
 for i in my_function():  # Noncompliant
     ......
+```
 
-
-

Compliant Solution

-
+## Compliant Solution
+
+```python
 limit = my_function()
 for i in limit:
     ......
-
+``` + diff --git a/ecocode-rules-specifications/src/main/rules/EC7/python/EC7.asciidoc b/ecocode-rules-specifications/src/main/rules/EC7/python/EC7.asciidoc index dba3dd847..4ca4240e5 100644 --- a/ecocode-rules-specifications/src/main/rules/EC7/python/EC7.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC7/python/EC7.asciidoc @@ -1,6 +1,8 @@ -

Avoid using getters and setters in a class, as they increase unnecessary RAM memory usage.

-

Noncompliant Code Example

-
+Avoid using getters and setters in a class, as they increase unnecessary RAM memory usage.
+
+## Noncompliant Code Example
+
+```python
 class Client():
 
     def __init__(self, age):
@@ -15,10 +17,11 @@ class Client():
 client = Client(25)
 client.get_age() # Getter inutile
 client.set_age(25) # Setter inutile
+```
 
-
-

Compliant Solution

-
+## Compliant Solution
+
+```python
 class Client():
 
     def __init__(self, age):
@@ -33,4 +36,5 @@ class Client():
 client = Client(25)
 client.age # Récupérer l'attribut age
 client.age = 26 # Modifier l'attribut age
-
+``` + diff --git a/ecocode-rules-specifications/src/main/rules/EC72/java/EC72.asciidoc b/ecocode-rules-specifications/src/main/rules/EC72/java/EC72.asciidoc index 7c6588afb..7032eb960 100644 --- a/ecocode-rules-specifications/src/main/rules/EC72/java/EC72.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC72/java/EC72.asciidoc @@ -1,41 +1,17 @@ -

Executing SQL queries in loop induced unnecessary calculation by the cpu, RAM usage and network transfer.

-

Noncompliant Code Example

-
-    public void foo() {
-        ...
-        String baseQuery = "SELECT name FROM users where id = ";
-
-        for (int i = 0; i < 20; i++) {
-
-            String query  = baseQuery.concat("" + i);
-            Statement st = conn.createStatement();
-            ResultSet rs = st.executeQuery(query); // Noncompliant
-
-            // iterate through the java resultset
-            while (rs.next()) {
-                String name = rs.getString("name");
-                System.out.println(name);
-            }
-            st.close();
-        }
-        ...
-    }
+Executing SQL queries in loop induced unnecessary calculation by the CPU, RAM usage and network transfer.
 
-
-

Compliant Solution

-
+## Noncompliant Code Example
 
-    public void foo() {
-        ...
-        String query = "SELECT name FROM users where id in (0 ";
-        for (int i = 1; i < 20; i++) {
+```java
+public void foo() {
+    // ...
+    String baseQuery = "SELECT name FROM users where id = ";
 
-            query  = baseQuery.concat("," + i);
-        }
+    for (int i = 0; i < 20; i++) {
 
-        query  = baseQuery.concat(")");
+        String query  = baseQuery.concat("" + i);
         Statement st = conn.createStatement();
-        ResultSet rs = st.executeQuery(query); // compliant
+        ResultSet rs = st.executeQuery(query); // Noncompliant
 
         // iterate through the java resultset
         while (rs.next()) {
@@ -43,7 +19,32 @@
             System.out.println(name);
         }
         st.close();
-        ...
-   }
+    }
+    // ...
+}
+```
+
+## Compliant Solution
 
-
+```java +public void foo() { + // ... + String query = "SELECT name FROM users where id in (0 "; + for (int i = 1; i < 20; i++) { + + query = baseQuery.concat("," + i); + } + + query = baseQuery.concat(")"); + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(query); // compliant + + // iterate through the java resultset + while (rs.next()) { + String name = rs.getString("name"); + System.out.println(name); + } + st.close(); + // ... +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC72/php/1GB.etsdiff.csv b/ecocode-rules-specifications/src/main/rules/EC72/php/1GB.etsdiff.csv new file mode 100644 index 000000000..b006091ac --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC72/php/1GB.etsdiff.csv @@ -0,0 +1,3 @@ +Energy (J),73.907586,82.15627099999998 +Transfer (B),49526,221836 +Storage (B),637549572,637549572 diff --git a/ecocode-rules-specifications/src/main/rules/EC72/php/2GB.etsdiff.csv b/ecocode-rules-specifications/src/main/rules/EC72/php/2GB.etsdiff.csv new file mode 100644 index 000000000..73f5bac22 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC72/php/2GB.etsdiff.csv @@ -0,0 +1,3 @@ +Energy (J),159.4871645,169.746055 +Transfer (B),50385,228225 +Storage (B),1178614788,1178614788 diff --git a/ecocode-rules-specifications/src/main/rules/EC72/php/4GB.etsdiff.csv b/ecocode-rules-specifications/src/main/rules/EC72/php/4GB.etsdiff.csv new file mode 100644 index 000000000..4c253ef72 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC72/php/4GB.etsdiff.csv @@ -0,0 +1,3 @@ +Energy (J),395.7629349999999,404.37447649999996 +Transfer (B),51597,238884 +Storage (B),2357214212,2357214212 diff --git a/ecocode-rules-specifications/src/main/rules/EC72/php/8GB.etsdiff.csv b/ecocode-rules-specifications/src/main/rules/EC72/php/8GB.etsdiff.csv new file mode 100644 index 000000000..2a518e5eb --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC72/php/8GB.etsdiff.csv @@ -0,0 +1,3 @@ +Energy (J),992.128585,1005.4625534999999 +Transfer (B),52189,249499 +Storage (B),4685052932,4685052932 diff --git a/ecocode-rules-specifications/src/main/rules/EC72/php/EC72.asciidoc b/ecocode-rules-specifications/src/main/rules/EC72/php/EC72.asciidoc index 7973ec509..12fe0d7b7 100644 --- a/ecocode-rules-specifications/src/main/rules/EC72/php/EC72.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC72/php/EC72.asciidoc @@ -1,349 +1,91 @@ -

Executing SQL queries in loop induced unnecessary network transfert, calculation by the cpu and RAM usage.

-

Noncompliant Code Example

-
-    public function foo() {
-        ...
-        $baseQuery = "SELECT name FROM users where id = ";
+Executing SQL queries in loop induced unnecessary network transfert, calculation by the cpu and RAM usage.
+
+## Noncompliant Code Example
+
+```php
+public function foo() {
+    ...
+    $baseQuery = "SELECT name FROM users where id = ";
 
-        for ($i = 0; $i < 20; ++$i) {
+    for ($i = 0; $i < 20; ++$i) {
 
-            $query = $baseQuery . $i;
-            $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
-		        mysql_select_db($dbname) or die("Could not open the db '$dbname'");
-            $result = mysql_query($this->Query);// Noncompliant
+        $query = $baseQuery . $i;
+        $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
+            mysql_select_db($dbname) or die("Could not open the db '$dbname'");
+        $result = mysql_query($this->Query);// Noncompliant
 
-            // iterate through the result
-            ...
-            mysql_close($connection);
-        }
+        // iterate through the result
         ...
+        mysql_close($connection);
     }
+    ...
+}
+```
 
-
-

Compliant Solution

-
+## Compliant Solution
 
-    public function foo() {
-        ...
-        $query = "SELECT name FROM users where id in (";
+```php
+public function foo() {
+    ...
+    $query = "SELECT name FROM users where id in (";
 
-        for ($i = 0; $i < 20; ++$i) {
-            $query .= ',' . $i;
-        }
-        $query .= ')';
+    for ($i = 0; $i < 20; ++$i) {
+        $query .= ',' . $i;
+    }
+    $query .= ')';
 
-        $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
-        mysql_select_db($dbname) or die("Could not open the db '$dbname'");
-        $result = mysql_query($this->Query); // compliant
+    $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
+    mysql_select_db($dbname) or die("Could not open the db '$dbname'");
+    $result = mysql_query($this->Query); // compliant
 
-        // iterate through the result
-        ...
-        mysql_close($connection);
-   }
+    // iterate through the result
+    ...
+    mysql_close($connection);
+}
+```
+
+include::../../etsdiff-methodology.asciidoc[]
+
+## Case for a 1GB database:
+
+image::https://live.staticflickr.com/65535/52622813465_9c453a43b1_w.jpg[ETSdiff percent comparison" style="padding: 1rem;]
+
+[format=csv,cols="1h,1,1"]
+|===
+Source of impacts,Compliant,Non-compliant
+
+include::1GB.etsdiff.csv[]
+|===
+
+## Case for a 2GB database:
+
+image::https://live.staticflickr.com/65535/52622862388_720fd219ba_o.png[ETSdiff percent comparison" style="padding: 1rem;]
+
+[format=csv,cols="1h,1,1"]
+|===
+Source of impacts,Compliant,Non-compliant
+
+include::2GB.etsdiff.csv[]
+|===
+
+## Case for a 4GB database:
+
+image::https://live.staticflickr.com/65535/52622814395_f8aab7a5c0_o.png[ETSdiff percent comparison" style="padding: 1rem;]
 
-
+[format=csv,cols="1h,1,1"] +|=== +Source of impacts,Compliant,Non-compliant -

The three sources of impacts of a code identified are:

- - Energy (measured in joules) -
- Transfer (measured in Bytes) -
- Storage (measured in Bytes)
-
The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption. -
The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment). -
The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed. -
+include::4GB.etsdiff.csv[] +|=== -

Case for a 1GB database:

-
- ETSdiff percent comparison -
-

Total:

- - - - - - - - - - - - - - - - - - - - - - - -
-
-
Compliant
-
-
-
-
Non-compliant
-
-
-
-
Energy
-
-
-
-
73.907586
-
-
-
-
82.15627099999998
-
-
-
-
Transfer
-
-
-
-
49526
-
-
-
-
221836
-
-
-
-
Storage
-
-
-
-
637549572
-
-
-
-
637549572
-
-
+## Case for a 8GB database: -

Case for a 2GB database:

-
- ETSdiff percent comparison -
-

Total:

- - - - - - - - - - - - - - - - - - - - - - - -
-
-
Compliant
-
-
-
-
Non-compliant
-
-
-
-
Energy
-
-
-
-
159.4871645
-
-
-
-
169.746055
-
-
-
-
Transfer
-
-
-
-
50385
-
-
-
-
228225
-
-
-
-
Storage
-
-
-
-
1178614788
-
-
-
-
1178614788
-
-
+image::https://live.staticflickr.com/65535/52622635779_f1c5d9660e_o.png[ETSdiff percent comparison" style="padding: 1rem;] -

Case for a 4GB database:

-
- ETSdiff percent comparison -
-

Total:

- - - - - - - - - - - - - - - - - - - - - - - -
-
-
Compliant
-
-
-
-
Non-compliant
-
-
-
-
Energy
-
-
-
-
395.7629349999999
-
-
-
-
404.37447649999996
-
-
-
-
Transfer
-
-
-
-
51597
-
-
-
-
238884
-
-
-
-
Storage
-
-
-
-
2357214212
-
-
-
-
2357214212
-
-
+[format=csv,cols="1h,1,1"] +|=== +Source of impacts,Compliant,Non-compliant -

Case for a 8GB database:

-
- ETSdiff percent comparison -
-

Total:

- - - - - - - - - - - - - - - - - - - - - - - -
-
-
Compliant
-
-
-
-
Non-compliant
-
-
-
-
Energy
-
-
-
-
992.128585
-
-
-
-
1005.4625534999999
-
-
-
-
Transfer
-
-
-
-
52189
-
-
-
-
249499
-
-
-
-
Storage
-
-
-
-
4685052932
-
-
-
-
4685052932
-
-
\ No newline at end of file +include::8GB.etsdiff.csv[] +|=== diff --git a/ecocode-rules-specifications/src/main/rules/EC72/python/EC72.asciidoc b/ecocode-rules-specifications/src/main/rules/EC72/python/EC72.asciidoc index 2323c02f5..ae85dfe3e 100644 --- a/ecocode-rules-specifications/src/main/rules/EC72/python/EC72.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC72/python/EC72.asciidoc @@ -1,21 +1,23 @@ -

Executing SQL queries in loop induced unnecessary calculation by the cpu, RAM usage and network transfer.

-

Noncompliant Code Example

-
-    def foo():
-        ...
-        results = []
-        for id in range(20):
-          results.append(cursor.execute("SELECT name FROM users where id = ?", (id)).fetchone()) # Noncompliant {{Avoid performing SQL queries within a loop}}
-        ...
-
-

Compliant Solution

-
+Executing SQL queries in loop induced unnecessary calculation by the CPU, RAM usage and network transfer.
 
-    def foo():
-        ...
-        ids = range(20)
-        results = cursor.execute("SELECT name FROM users where id IN ({0})".format(', '.join("?" * len(ids))), ids).fetchmany() # Compliant
-        ...
-   }
+## Noncompliant Code Example
 
-
+```python +def foo(): + ... + results = [] + for id in range(20): + results.append(cursor.execute("SELECT name FROM users where id = ?", (id)).fetchone()) # Noncompliant {{Avoid performing SQL queries within a loop}} + ... +``` + +## Compliant Solution + +```python +def foo(): + ... + ids = range(20) + results = cursor.execute("SELECT name FROM users where id IN ({0})".format(', '.join("?" * len(ids))), ids).fetchmany() # Compliant + ... +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC74/java/EC74.asciidoc b/ecocode-rules-specifications/src/main/rules/EC74/java/EC74.asciidoc index 35ffbf7af..4f1299b18 100644 --- a/ecocode-rules-specifications/src/main/rules/EC74/java/EC74.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC74/java/EC74.asciidoc @@ -1,21 +1,21 @@ -

Database servers have to resolve schema fields when using asterisk symbol (*). Knowing and using the schema saves CPU cycles and network transfer.

-

Noncompliant Code Example

-
-    public void foo() {
-        ...
-        String baseQuery = "SELECT * FROM users"; // Noncompliant
+Database servers have to resolve schema fields when using asterisk symbol (`*`). Knowing and using the schema saves CPU cycles and network transfer.
 
-        ...
-    }
+## Noncompliant Code Example
 
-
-

Compliant Solution

-
+```java
+public void foo() {
+    // ...
+    String baseQuery = "SELECT * FROM users"; // Noncompliant
+    // ...
+}
+```
 
-    public void foo() {
-        ...
-        String query = "SELECT id, name, address FROM users ";
-        ...
-   }
+## Compliant Solution
 
-
+```java +public void foo() { + // ... + String query = "SELECT id, name, address FROM users"; + // ... +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC74/php/1GB.etsdiff.csv b/ecocode-rules-specifications/src/main/rules/EC74/php/1GB.etsdiff.csv new file mode 100644 index 000000000..06710e417 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC74/php/1GB.etsdiff.csv @@ -0,0 +1,3 @@ +Energy (J),0.040610499999999994,0.065223 +Transfer (B),779232,2697937 +Storage (B),637548827,637548827 diff --git a/ecocode-rules-specifications/src/main/rules/EC74/php/EC74.asciidoc b/ecocode-rules-specifications/src/main/rules/EC74/php/EC74.asciidoc index de53be78e..ffb380dd5 100644 --- a/ecocode-rules-specifications/src/main/rules/EC74/php/EC74.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC74/php/EC74.asciidoc @@ -1,101 +1,34 @@ -

Database servers have to resolve schema fields when using asterisk symbol (*). Knowing and using the schema saves CPU cycles and network transfer.

-

Noncompliant Code Example

-
-    public function foo() {
-        ...
-        $baseQuery = "SELECT * FROM users"; // Noncompliant
+Database servers have to resolve schema fields when using asterisk symbol (`*`). Knowing and using the schema saves CPU cycles and network transfer.
 
-        ...
-    }
-
-

Compliant Solution

-
-    public function foo() {
-        ...
-        $baseQuery = "SELECT id,name, address FROM users ";
-        ...
-   }
-
-

The three sources of impacts of a code identified are:

-- Energy (measured in joules) -
- Transfer (measured in Bytes) -
- Storage (measured in Bytes)
-
The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption. -
The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment). -
The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed. -
+## Noncompliant Code Example -

Case for a 1GB database:

-
- ETSdiff percent comparison -
+```php +public function foo() { + ... + $baseQuery = "SELECT * FROM users"; // Noncompliant + ... +} +``` -

Total:

- - - - - - - - - - - - - - - - - - - - - - - -
-
-
Compliant
-
-
-
-
Non-compliant
-
-
-
-
Energy
-
-
-
-
0.040610499999999994
-
-
-
-
0.065223
-
-
-
-
Transfer
-
-
-
-
779232
-
-
-
-
2697937
-
-
-
-
Storage
-
-
-
-
637548827
-
-
-
-
637548827
-
-
+## Compliant Solution + +```php +public function foo() { + ... + $baseQuery = "SELECT id,name, address FROM users "; + ... +} +``` + +include::../../etsdiff-methodology.asciidoc[] + +## Case for a 1GB database: + +image::https://live.staticflickr.com/65535/52622636584_52938fcf7e_o.png[ETSdiff percent comparison] + +[format=csv,cols="1h,1,1"] +|=== +Source of impacts,Compliant,Non-compliant + +include::1GB.etsdiff.csv[] +|=== diff --git a/ecocode-rules-specifications/src/main/rules/EC74/python/EC74.asciidoc b/ecocode-rules-specifications/src/main/rules/EC74/python/EC74.asciidoc index 35ffbf7af..3b9b41c0f 100644 --- a/ecocode-rules-specifications/src/main/rules/EC74/python/EC74.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC74/python/EC74.asciidoc @@ -1,21 +1,21 @@ -

Database servers have to resolve schema fields when using asterisk symbol (*). Knowing and using the schema saves CPU cycles and network transfer.

-

Noncompliant Code Example

-
-    public void foo() {
-        ...
-        String baseQuery = "SELECT * FROM users"; // Noncompliant
+Database servers have to resolve schema fields when using asterisk symbol (`*`). Knowing and using the schema saves CPU cycles and network transfer.
 
-        ...
-    }
+## Noncompliant Code Example
 
-
-

Compliant Solution

-
+```python
+public void foo() {
+    ...
+    String baseQuery = "SELECT * FROM users"; // Noncompliant
+    ...
+}
+```
 
-    public void foo() {
-        ...
-        String query = "SELECT id, name, address FROM users ";
-        ...
-   }
+## Compliant Solution
 
-
+```python +public void foo() { + ... + String query = "SELECT id, name, address FROM users "; + ... +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC75/java/EC75.asciidoc b/ecocode-rules-specifications/src/main/rules/EC75/java/EC75.asciidoc index 67c1c1ccf..6cbc5ed2a 100644 --- a/ecocode-rules-specifications/src/main/rules/EC75/java/EC75.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC75/java/EC75.asciidoc @@ -1,41 +1,38 @@ -

- Don't concatenate Strings in loop. User StringBuilder instead.
- Strings are immutable so each time you concatenate a String, a new String is created. This is a waste of memory and CPU. -

+Don't concatenate Strings in loop. Use `StringBuilder instead. -

Noncompliant Code Example

-
+Strings are immutable so each time you concatenate a String, a new String is created. This is a waste of memory and CPU.
 
-    public String concatenateStrings(String[] strings) {
-        String result = "";
+## Noncompliant Code Example
 
-        for (String string : strings) {
-            result += string; // Noncompliant
-        }
-        return result;
+```java
+public String concatenateStrings(String[] strings) {
+    String result = "";
+
+    for (String string : strings) {
+        result += string; // Noncompliant
     }
+    return result;
+}
 
-    public String concatenateStrings2() {
-        String result = "";
+public String concatenateStrings2() {
+    String result = "";
 
-        for (int i = 0; i < 1000; ++i) {
-            result += "another"; // Noncompliant
-        }
-        return result;
+    for (int i = 0; i < 1000; ++i) {
+        result += "another"; // Noncompliant
     }
+    return result;
+}
+```
 
-
- -

Compliant Solution

-
+## Compliant Solution
 
-    public String concatenateStrings(String[] strings) {
-        StringBuilder result = new StringBuilder();
+```java
+public String concatenateStrings(String[] strings) {
+    StringBuilder result = new StringBuilder();
 
-        for (String string : strings) {
-            result.append(string);
-        }
-        return result.toString();
+    for (String string : strings) {
+        result.append(string);
     }
-
-
+ return result.toString(); +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC76/java/EC76.asciidoc b/ecocode-rules-specifications/src/main/rules/EC76/java/EC76.asciidoc index 608b0285b..f21e5298c 100644 --- a/ecocode-rules-specifications/src/main/rules/EC76/java/EC76.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC76/java/EC76.asciidoc @@ -1,38 +1,36 @@ -

- Avoid usage of static collections.
- If you want to use static collections make them final and create for example a singleton if needed containing the collections.
- The static fields are more complicated for the Garbage Collector to manage and can lead to memory leaks. -

- -

Noncompliant Code Example

-
-
-    /**
-     * Not compliant
-     */
-    public class AvoidUsageOfStaticCollections {
-        public static final List<> LIST = new ArrayList<>();
-        public static final Set<> SET = new HashSet<>();
-        public static final Map<> MAP = new HashMap<>();
-    }
+Avoid usage of static collections.
 
-
+If you want to use static collections make them final and create for example a singleton if needed containing the collections. -

Compliant Solution

-
+The static fields are more complicated for the Garbage Collector to manage and can lead to memory leaks.
 
-    /**
-     * Compliant
-     */
-    public class GoodUsageOfStaticCollections {
-        public static volatile GoodUsageOfStaticCollections INSTANCE = new GoodUsageOfStaticCollections();
+## Noncompliant Code Example
 
-        public final List<> LIST = new ArrayList<>();
-        public final Set<> SET = new HashSet<>();
-        public final Map<> MAP = new HashMap<>();
+```java
+/**
+ * Not compliant
+ */
+public class AvoidUsageOfStaticCollections {
+    public static final List<> LIST = new ArrayList<>();
+    public static final Set<> SET = new HashSet<>();
+    public static final Map<> MAP = new HashMap<>();
+}
+```
 
-        private GoodUsageOfStaticCollections() {
-        }
-    }
+## Compliant Solution
+
+```java
+/**
+ * Compliant
+ */
+public class GoodUsageOfStaticCollections {
+    public static volatile GoodUsageOfStaticCollections INSTANCE = new GoodUsageOfStaticCollections();
 
-
\ No newline at end of file + public final List<> LIST = new ArrayList<>(); + public final Set<> SET = new HashSet<>(); + public final Map<> MAP = new HashMap<>(); + + private GoodUsageOfStaticCollections() { + } +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC77/java/EC77.asciidoc b/ecocode-rules-specifications/src/main/rules/EC77/java/EC77.asciidoc index f0aed4f4f..188b8f648 100644 --- a/ecocode-rules-specifications/src/main/rules/EC77/java/EC77.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC77/java/EC77.asciidoc @@ -1,67 +1,53 @@ -

- Avoid using Pattern.compile() in a non-static context. - This operation requires a significant amount of computational power, Using a single match saves CPU cycles and RAM consumption. -

+Avoid using `Pattern.compile()` in a non-static context. +This operation requires a significant amount of computational power, Using a single match saves CPU cycles and RAM consumption. -

Noncompliant Code Example

-
-
-    public class AvoidRegexPatternNotStatic {
-
-        public boolean foo() {
-            final Pattern pattern = Pattern.compile("foo"); // Noncompliant
-            return pattern.matcher("foo").find();
-        }
+## Noncompliant Code Example
 
+```java
+public class AvoidRegexPatternNotStatic {
+    public boolean foo() {
+        final Pattern pattern = Pattern.compile("foo"); // Noncompliant
+        return pattern.matcher("foo").find();
     }
+}
+```
 
-
- -

Compliant Solution N°1

-
-
-    public class ValidRegexPattern {
+## Compliant Solution N°1
 
-        private static final Pattern pattern = Pattern.compile("foo"); // Compliant
-
-        public boolean foo() {
-            return pattern.matcher("foo").find();
-        }
+```java
+public class ValidRegexPattern {
+    private static final Pattern pattern = Pattern.compile("foo"); // Compliant
 
+    public boolean foo() {
+        return pattern.matcher("foo").find();
     }
+}
+```
 
-
- -

Compliant Solution N°2

-
-
-    public class ValidRegexPattern2 {
+## Compliant Solution N°2
 
-        private final Pattern pattern = Pattern.compile("foo"); // Compliant
-
-        public boolean foo() {
-            return pattern.matcher("foo").find();
-        }
+```java
+public class ValidRegexPattern2 {
+    private final Pattern pattern = Pattern.compile("foo"); // Compliant
 
+    public boolean foo() {
+        return pattern.matcher("foo").find();
     }
+}
+```
 
-
- -

Compliant Solution N°3

-
-
-    public class ValidRegexPattern3 {
+## Compliant Solution N°3
 
-        private final Pattern pattern;
-
-        public ValidRegexPattern3() {
-            pattern = Pattern.compile("foo"); // Compliant
-        }
-
-        public boolean foo() {
-            return pattern.matcher("foo").find();
-        }
+```java
+public class ValidRegexPattern3 {
+    private final Pattern pattern;
 
+    public ValidRegexPattern3() {
+        pattern = Pattern.compile("foo"); // Compliant
     }
 
-
+ public boolean foo() { + return pattern.matcher("foo").find(); + } +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC78/java/EC78.asciidoc b/ecocode-rules-specifications/src/main/rules/EC78/java/EC78.asciidoc index 2b0c687b0..3153a8742 100644 --- a/ecocode-rules-specifications/src/main/rules/EC78/java/EC78.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC78/java/EC78.asciidoc @@ -1,30 +1,34 @@ -

Don't set const parameter in batch update => Put its in query. Creating this parameter and destroying it consumes CPU cycles and RAM unnecessarily.

-

Noncompliant Code Example

-
-    public void foo() {
-    	...
-    	String query = "insert into mytable values(?,?,?)";
-        ...
-        for(DummyClass o : list) {
-			stmt.setInt(1, 123);  // Noncompliant
-			stmt.setString(2, o.getName());
-			stmt.setDouble(3, o.getPrice());
-			stmt.addBatch();
-		}
-        ...
+Don't set a constant parameter in batch update. Instead, put it in query. Creating this parameter and destroying it consumes CPU cycles and RAM unnecessarily.
+
+## Noncompliant Code Example
+
+```java
+public void foo() {
+    // ...
+    String query = "insert into mytable values(?,?,?)";
+    // ...
+    for(DummyClass o : list) {
+        stmt.setInt(1, 123);  // Noncompliant
+        stmt.setString(2, o.getName());
+        stmt.setDouble(3, o.getPrice());
+        stmt.addBatch();
     }
-
-

Compliant Solution

-
-    public void foo() {
-    	...
-    	String query = "insert into mytable values(123,?,?)";
-        ...
-        for(DummyClass o : list) {
-			stmt.setString(1, o.getName());
-			stmt.setDouble(2, o.getPrice());
-			stmt.addBatch();
-		}
-        ...
+    // ...
+}
+```
+
+## Compliant Solution
+
+```java
+public void foo() {
+    // ...
+    String query = "insert into mytable values(123,?,?)";
+    // ...
+    for(DummyClass o : list) {
+        stmt.setString(1, o.getName());
+        stmt.setDouble(2, o.getPrice());
+        stmt.addBatch();
     }
-
+ // ... +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC79/java/EC79.asciidoc b/ecocode-rules-specifications/src/main/rules/EC79/java/EC79.asciidoc index f8801ef21..ef8bc2156 100644 --- a/ecocode-rules-specifications/src/main/rules/EC79/java/EC79.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC79/java/EC79.asciidoc @@ -1,24 +1,28 @@ -

try-with-resources Statement needs to be implemented for any object that implements the AutoCloseable interface, it save computer resources.

-

Noncompliant Code Example

-
-    private static void printFileJava7() throws IOException {
-        FileInputStream input = new FileInputStream("file.txt");
+Use `try-with-resources` statement for any object that implements the `AutoCloseable` interface, it save computer resources.
+
+## Noncompliant Code Example
+
+```java
+private static void printFileJava7() throws IOException {
+    FileInputStream input = new FileInputStream("file.txt");
+    int data = input.read();
+    while(data != -1){
+        System.out.print((char) data);
+        data = input.read();
+    }
+}
+```
+
+## Compliant Solution
+
+```java
+private static void printFileJava7() throws IOException {
+    try(FileInputStream input = new FileInputStream("file.txt")) {
         int data = input.read();
         while(data != -1){
             System.out.print((char) data);
             data = input.read();
         }
     }
-
-

Compliant Solution

-
-    private static void printFileJava7() throws IOException {
-        try(FileInputStream input = new FileInputStream("file.txt")) {
-            int data = input.read();
-            while(data != -1){
-                System.out.print((char) data);
-                data = input.read();
-            }
-        }
-    }
-
+} +``` diff --git a/ecocode-rules-specifications/src/main/rules/etsdiff-methodology.asciidoc b/ecocode-rules-specifications/src/main/rules/etsdiff-methodology.asciidoc new file mode 100644 index 000000000..3239fa7d4 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/etsdiff-methodology.asciidoc @@ -0,0 +1,11 @@ +## The three sources of impacts of a code identified are: + +- Energy: measured in joules (J) +- Transfer: measured in Bytes (B) +- Storage: measured in Bytes (B) + +The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption. + +The _ETSdiff_ tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment). + +The results generated by _ETSdiff_ must help define the interest of the rule reported by _SonarQube_ in the context of the code analyzed. From 7d3683b60e916cd1c8f883ee4fb9852892b2ea91 Mon Sep 17 00:00:00 2001 From: jycr Date: Mon, 10 Jul 2023 00:14:31 +0200 Subject: [PATCH 7/7] feat(rule): Add rules of green-code-initiative/ecoCode-javascript#14 --- ecocode-rules-specifications/pom.xml | 38 ++++++++++ .../src/main/assembly/js.xml | 18 +++++ .../src/main/assembly/ts.xml | 18 +++++ .../src/main/rules/EC11/EC11.json | 19 +++++ .../src/main/rules/EC11/js/EC11.asciidoc | 20 ++++++ .../src/main/rules/EC12/EC12.json | 19 +++++ .../src/main/rules/EC12/js/EC12.asciidoc | 33 +++++++++ .../src/main/rules/EC13/EC13.json | 19 +++++ .../src/main/rules/EC13/ts/EC13.asciidoc | 31 +++++++++ .../src/main/rules/EC8/EC8.json | 19 +++++ .../src/main/rules/EC8/js/EC8.asciidoc | 43 ++++++++++++ .../src/main/rules/EC9/EC9.json | 19 +++++ .../src/main/rules/EC9/js/EC9.asciidoc | 69 +++++++++++++++++++ 13 files changed, 365 insertions(+) create mode 100644 ecocode-rules-specifications/src/main/assembly/js.xml create mode 100644 ecocode-rules-specifications/src/main/assembly/ts.xml create mode 100644 ecocode-rules-specifications/src/main/rules/EC11/EC11.json create mode 100644 ecocode-rules-specifications/src/main/rules/EC11/js/EC11.asciidoc create mode 100644 ecocode-rules-specifications/src/main/rules/EC12/EC12.json create mode 100644 ecocode-rules-specifications/src/main/rules/EC12/js/EC12.asciidoc create mode 100644 ecocode-rules-specifications/src/main/rules/EC13/EC13.json create mode 100644 ecocode-rules-specifications/src/main/rules/EC13/ts/EC13.asciidoc create mode 100644 ecocode-rules-specifications/src/main/rules/EC8/EC8.json create mode 100644 ecocode-rules-specifications/src/main/rules/EC8/js/EC8.asciidoc create mode 100644 ecocode-rules-specifications/src/main/rules/EC9/EC9.json create mode 100644 ecocode-rules-specifications/src/main/rules/EC9/js/EC9.asciidoc diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index 30e3f07ce..6dee12f0d 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -133,6 +133,20 @@ + + + + + + + + + + + + + + @@ -190,6 +204,30 @@ + + assembly-js + prepare-package + + single + + + + ${project.basedir}/src/main/assembly/js.xml + + + + + assembly-ts + prepare-package + + single + + + + ${project.basedir}/src/main/assembly/ts.xml + + + true diff --git a/ecocode-rules-specifications/src/main/assembly/js.xml b/ecocode-rules-specifications/src/main/assembly/js.xml new file mode 100644 index 000000000..d5d72ca1e --- /dev/null +++ b/ecocode-rules-specifications/src/main/assembly/js.xml @@ -0,0 +1,18 @@ + + js + + jar + + false + + + ${project.build.outputDirectory} + + io/ecocode/rules/js/*.* + + + + + diff --git a/ecocode-rules-specifications/src/main/assembly/ts.xml b/ecocode-rules-specifications/src/main/assembly/ts.xml new file mode 100644 index 000000000..ec0100661 --- /dev/null +++ b/ecocode-rules-specifications/src/main/assembly/ts.xml @@ -0,0 +1,18 @@ + + ts + + jar + + false + + + ${project.build.outputDirectory} + + io/ecocode/rules/ts/*.* + + + + + diff --git a/ecocode-rules-specifications/src/main/rules/EC11/EC11.json b/ecocode-rules-specifications/src/main/rules/EC11/EC11.json new file mode 100644 index 000000000..878a34665 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC11/EC11.json @@ -0,0 +1,19 @@ +{ + "title": "Disallow multiple access of same DOM element.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "eco-design", + "performance", + "ecocode" + ], + "defaultSeverity": "Major", + "compatibleLanguages": [ + "JAVASCRIPT", + "TYPESCRIPT" + ] +} diff --git a/ecocode-rules-specifications/src/main/rules/EC11/js/EC11.asciidoc b/ecocode-rules-specifications/src/main/rules/EC11/js/EC11.asciidoc new file mode 100644 index 000000000..b5a2a8649 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC11/js/EC11.asciidoc @@ -0,0 +1,20 @@ +## Rule details + +This rule aims to reduce DOM access assigning its object to variable when access multiple time. It saves CPU cycles. + +## Examples + +Examples of **incorrect** code for this rule: + +```js +var el1 = document.getElementById("block1").test1; +var el2 = document.getElementById("block1").test2; +``` + +Examples of **correct** code for this rule: + +```js +var blockElement = document.getElementById("block1"); +var el1 = blockElement.test1; +var el2 = blockElement.test2; +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC12/EC12.json b/ecocode-rules-specifications/src/main/rules/EC12/EC12.json new file mode 100644 index 000000000..67c6010cf --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC12/EC12.json @@ -0,0 +1,19 @@ +{ + "title": "Disallow multiple style changes at once.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "10min" + }, + "tags": [ + "eco-design", + "performance", + "ecocode" + ], + "defaultSeverity": "Major", + "compatibleLanguages": [ + "JAVASCRIPT", + "TYPESCRIPT" + ] +} diff --git a/ecocode-rules-specifications/src/main/rules/EC12/js/EC12.asciidoc b/ecocode-rules-specifications/src/main/rules/EC12/js/EC12.asciidoc new file mode 100644 index 000000000..5bd836997 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC12/js/EC12.asciidoc @@ -0,0 +1,33 @@ +## Rule Details + +This rule aims to disallow batching multiple style changes at once. + +To limit the number of repaint/reflow, it is advised to batch style modifications by adding a class containing all style changes that will generate a unique reflow. + +## Examples + +Examples of **non-compliant** code for this rule: + +```html + +``` + +Examples of **compliant** code for this rule: + +```html + + + +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC13/EC13.json b/ecocode-rules-specifications/src/main/rules/EC13/EC13.json new file mode 100644 index 000000000..6ff60b4fa --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC13/EC13.json @@ -0,0 +1,19 @@ +{ + "title": "Prefer API collections with pagination.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "30min" + }, + "tags": [ + "eco-design", + "performance", + "ecocode" + ], + "defaultSeverity": "Minor", + "compatibleLanguages": [ + "JAVASCRIPT", + "TYPESCRIPT" + ] +} diff --git a/ecocode-rules-specifications/src/main/rules/EC13/ts/EC13.asciidoc b/ecocode-rules-specifications/src/main/rules/EC13/ts/EC13.asciidoc new file mode 100644 index 000000000..abc58bf6c --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC13/ts/EC13.asciidoc @@ -0,0 +1,31 @@ +## Rule details + +This rule aims to reduce the size and thus the network weight of API returns that may contain many elements. This rule is built for the https://nestjs.com[NestJS framework] but can work with a controller `@Controller()` and a decorated method `@Get()`. + +## Examples + +Examples of **non-compliant** code for this rule: + +```typescript +@Controller() +class Test { + @Get() + public find(): Promise {} +} +``` + +Examples of **compliant** code for this rule: + +```typescript +interface Pagination { + items: string[]; + currentPage: number; + totalPages: number; +} + +@Controller() +class Test { + @Get() + public find(): Promise {} +} +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC8/EC8.json b/ecocode-rules-specifications/src/main/rules/EC8/EC8.json new file mode 100644 index 000000000..232f1ce0a --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC8/EC8.json @@ -0,0 +1,19 @@ +{ + "title": "Avoid using high accuracy geolocation in web applications.", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "eco-design", + "performance", + "ecocode" + ], + "defaultSeverity": "Major", + "compatibleLanguages": [ + "JAVASCRIPT", + "TYPESCRIPT" + ] +} diff --git a/ecocode-rules-specifications/src/main/rules/EC8/js/EC8.asciidoc b/ecocode-rules-specifications/src/main/rules/EC8/js/EC8.asciidoc new file mode 100644 index 000000000..7528b2bb2 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC8/js/EC8.asciidoc @@ -0,0 +1,43 @@ +## Rule details + +This rule aims at reducing CPU consumption by telling the device to use a less accurate yet more eco-friendly geolocation, when geolocation API is used. + +## Examples + +Examples of **non-compliant** code for this rule: + +```js +var options = { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 }; +function success(pos) { + console.log(pos.coords); +} + +function error(err) { + console.warn(err); +} + +navigator.geolocation.getCurrentPosition(success, error, options); +``` + +Examples of **compliant** code for this rule: + +```js +// enableHighAccuracy is false by default, so not declaring it is correct +function success(pos) { + console.log(pos); +} +navigator.geolocation.getCurrentPosition(success); +``` + +```js +var options = { enableHighAccuracy: false, timeout: 5000, maximumAge: 0 }; +function success(pos) { + console.log(pos.coords); +} + +function error(err) { + console.warn(err); +} + +navigator.geolocation.getCurrentPosition(success, error, options); +``` diff --git a/ecocode-rules-specifications/src/main/rules/EC9/EC9.json b/ecocode-rules-specifications/src/main/rules/EC9/EC9.json new file mode 100644 index 000000000..99e524b50 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC9/EC9.json @@ -0,0 +1,19 @@ +{ + "title": "Should not import all from library", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "15min" + }, + "tags": [ + "eco-design", + "performance", + "ecocode" + ], + "defaultSeverity": "Major", + "compatibleLanguages": [ + "JAVASCRIPT", + "TYPESCRIPT" + ] +} diff --git a/ecocode-rules-specifications/src/main/rules/EC9/js/EC9.asciidoc b/ecocode-rules-specifications/src/main/rules/EC9/js/EC9.asciidoc new file mode 100644 index 000000000..c6be22d50 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC9/js/EC9.asciidoc @@ -0,0 +1,69 @@ +## Rule details + +This rule aims to reduce weight of programs by using only needed modules. Many libraries export only one module by default, but some of them are exporting ES modules or submodules. We should use them to select more precisly needed modules and avoid unnecessarily overloading files weight. + +*Example with the well-known https://lodash.com/[lodash] library, if you only need +`isEmpty` method.* + + +|=== +|Full lodash library |Only `isEmpty` method + +a|index.js - 531.46 KB + +* node_modules/lodash - 531.35 KB +** lodash.js - 531.35 KB + +a|index.js - 24.42 KB + +* node_modules/lodash - 24.31 KB +** isEmpty - 1.95 KB +** _nodeUtil.js - 995 B +** isArrayLike.js - 830 B +** ... + +|=== + +## Options + +You can externally add your own libraries to be checked. To add your own libraries you need to modify your .eslintrc.js by adding the following rule configuration: + +```js +module.exports = { + ...yourConf, + rules: { + "no-import-all-from-library": [ + "warn", + { + notAllowedLibraries: ["some-lib"], // will check for -> import someLib from "some-lib" + importByNamespaceNotAllowedLibraries: ["some-other-lib"], // will check for -> import * as someOtherLib from "some-other-lib" + }, + ], + }, +}; +``` + +## Examples + +Examples of **non-compliant** code for this rule: + +```js +// Example with lodash +import lodash from "lodash"; +import { isEmpty } from "lodash"; +import * as lodash from "lodash"; + +// Example with underscore +import _ from "underscore"; +``` + +Examples of **compliant** code for this rule: + +```js +// Example with lodash (uses submodules) +import isEmpty from "lodash/isEmpty"; +import intersect from "lodash/intersect"; + +// Example with underscore (uses esm modules) +import map from "underscore/modules/map.js"; +```