Skip to content

Commit

Permalink
Merge branch 'main' into feature/migrate-mobile-rule
Browse files Browse the repository at this point in the history
  • Loading branch information
jycr committed Oct 27, 2023
2 parents a751983 + 47ae217 commit dc9ba2e
Show file tree
Hide file tree
Showing 19 changed files with 305 additions and 190 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#225](https://github.com/green-code-initiative/ecoCode/pull/225) Upgrade licence system and licence headers of Java files
- [#140](https://github.com/green-code-initiative/ecoCode/issues/140) Upgrade rule EC3 for Python : no implementation possible for python
- [#136](https://github.com/green-code-initiative/ecoCode/issues/136) Upgrade rule EC53 for Python : no implementation possible for python
- [#128](https://github.com/green-code-initiative/ecoCode/pull/128) Adding EC35 rule for Python and PHP : EC35 rule replaces EC34 with a specific use case ("file not found" sepcific)

### Deleted

Expand Down
87 changes: 44 additions & 43 deletions RULES.md

Large diffs are not rendered by default.

151 changes: 27 additions & 124 deletions ecocode-rules-specifications/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,22 @@
<artifactId>ecocode-rules-specifications</artifactId>

<name>ecoCode Rules Specifications repository</name>
<description>Repository that contains the specifications of every static-analysis rules available in ecoCode
plugins.
</description>
<description>Repository that contains the specifications of every static-analysis rules available in ecoCode plugins.</description>
<url>https://github.com/green-code-initiative/ecoCode/tree/main/ecocode-rules-specifications</url>

<dependencies>
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-plugin-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.sonarsource.analyzer-commons</groupId>
<artifactId>sonar-analyzer-commons</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<!--
<plugin><!--
This plugin convert ASCIIDOC rule specification in HTML format
ASCIIDOC format is used with custom features such as :
- syntax highlighting (see code blocks on ASCIIDOC rules)
- inclusions (see: php/EC74.asciidoc)
- table data generation from CSV (see: php/EC34.asciidoc)
NB: Current version has a bug which display following false positive warning :
[WARNING] Duplicated destination found: overwriting file ...
This issue is fixed in 3.x release of asciidoctor-maven-plugin
-->
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-maven-plugin</artifactId>
Expand All @@ -80,12 +40,13 @@
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/rules</sourceDirectory>
<outputDirectory>${project.build.directory}/rules</outputDirectory>
<outputDirectory>${project.build.directory}/rules-html</outputDirectory>
<attributes>
<source-highlighter>coderay</source-highlighter>
<coderay-css>style</coderay-css>
</attributes>
<preserveDirectories>true</preserveDirectories>
<embedAssets>true</embedAssets>
<headerFooter>false</headerFooter>
<relativeBaseDir>true</relativeBaseDir>
<logHandler>
Expand All @@ -97,79 +58,33 @@
</execution>
</executions>
</plugin>
<plugin>
<!--
Target resources tree need to be "flat".
Each metadata JSON file must be in the same tree as the HTML description file for the corresponding language.
The only way currently found to generate the correct file tree is to use antrun-plugin with `flattenmapper`
<plugin><!--
Prepare resources tree needed by language.
Each metadata JSON file must be in the same folder as the HTML description file for the corresponding language.
-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<groupId>com.github.johnpoth</groupId>
<artifactId>jshell-maven-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>process-resources</phase>
<id>prepare-rules-resources</id>
<phase>generate-resources</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/java">
<fileset dir="${project.build.directory}/rules">
<include name="*/java/EC*.html" />
<include name="*/EC*.json" />
</fileset>
<flattenmapper />
</copy>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/php">
<fileset dir="${project.build.directory}/rules">
<include name="*/php/EC*.html" />
<include name="*/EC*.json" />
</fileset>
<flattenmapper />
</copy>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/python">
<fileset dir="${project.build.directory}/rules">
<include name="*/python/EC*.html" />
<include name="*/EC*.json" />
</fileset>
<flattenmapper />
</copy>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/js">
<fileset dir="${project.build.directory}/rules">
<include name="*/js/EC*.html" />
<include name="*/EC*.json" />
</fileset>
<flattenmapper />
</copy>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/ts">
<fileset dir="${project.build.directory}/rules">
<include name="*/ts/EC*.html" />
<include name="*/EC*.json" />
</fileset>
<flattenmapper />
</copy>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/swift">
<fileset dir="${project.build.directory}/rules">
<include name="*/swift/EC*.html" />
<include name="*/EC*.json" />
</fileset>
<flattenmapper />
</copy>
<copy todir="${project.build.outputDirectory}/io/ecocode/rules/xml">
<fileset dir="${project.build.directory}/rules">
<include name="*/xml/EC*.html" />
<include name="*/EC*.json" />
</fileset>
<flattenmapper />
</copy>
</target>
<scripts>
<script>${project.basedir}/src/main/script/PrepareResources.jsh</script>
</scripts>
<options>
<option>-R "-DsourceDir=${project.build.directory}/rules-html"</option>
<option>-R "-DtargetDir=${project.build.outputDirectory}/io/ecocode/rules"</option>
</options>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!--
<plugin><!--
This module produce one artifact by language (with corresponding classifier)
For example, to add rule specifications for Python language, add following dependency in python sonarqube plugin:
Expand Down Expand Up @@ -221,26 +136,14 @@
</configuration>
</execution>
<execution>
<id>assembly-js</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/js.xml</descriptor>
</descriptors>
</configuration>
</execution>
<execution>
<id>assembly-ts</id>
<id>assembly-javascript</id>
<phase>prepare-package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/ts.xml</descriptor>
<descriptor>${project.basedir}/src/main/assembly/javascript.xml</descriptor>
</descriptors>
</configuration>
</execution>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.1 https://maven.apache.org/xsd/assembly-2.1.1.xsd">
<id>js</id>
<id>javascript</id>
<formats>
<format>jar</format>
</formats>
Expand All @@ -10,7 +10,7 @@
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<includes>
<include>io/ecocode/rules/js/*.*</include>
<include>io/ecocode/rules/javascript/*.*</include>
</includes>
<outputDirectory/>
</fileSet>
Expand Down
18 changes: 0 additions & 18 deletions ecocode-rules-specifications/src/main/assembly/ts.xml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Inside complex code parts (for exemple multiple loops, complex data constructions...), avoid using try...catch...finally.
Inside complex code parts (for exemple multiple loops, complex data constructions...), avoid using try...catch.

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.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Inside complex code parts (for example multiple loops, complex data constructions...), avoid using try...catch...finally.
Inside complex code parts (for example multiple loops, complex data constructions...), avoid using try...catch.

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.

Expand Down
15 changes: 15 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC35/EC35.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"title": "Avoid using try-catch statement with file open operation in try block",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"eco-design",
"performance",
"ecocode"
],
"defaultSeverity": "Minor"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Energy (J),515.855638,516.9188409999999
Transfer (B),1579453,1579457
Storage (B),637549804,637549804
46 changes: 46 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC35/php/EC35.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Inside complex code parts (for exemple multiple loops, complex data constructions...), avoid using try...catch.
For the moment, this rule only deals with "file open" use case in try...catch blocks.

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
}
catch(Exception $ex)
{
$msg = "Error opening $imgFile for Product $row['Identifier']";
throw new Exception($msg);
}
```

## Compliant Solution

```php
//try
if (file_exists($imgFile)) {
$picture = PDF_open_image_file($PDF, "jpeg", $imgFile, "", 0);
}

//catch
if (!$picture) {
$msg = "Error opening $imgFile for Product $row['Identifier']";
print $msg;
}
```

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[]
|===
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Inside complex code parts (for example multiple loops, complex data constructions...), avoid using try...catch.
For the moment, this rule only deals with "file open" use case in try...catch blocks.

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())
except:
print('No such file '+path
finally:
f.close()
```
## Compliant Solution
```python
if os.path.isfile(path):
fh = open(path, 'r')
print(fh.read())
fh.close
```

Loading

0 comments on commit dc9ba2e

Please sign in to comment.