-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #192 from green-code-initiative/PR_156_recup
[Rule EC203] Detect unoptimized file formats
- Loading branch information
Showing
14 changed files
with
190 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...ugin/src/main/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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; | ||
|
||
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"}) | ||
public class DetectUnoptimizedImageFormat extends PythonSubscriptionCheck { | ||
|
||
protected static final String RULE_KEY = "EC203"; | ||
protected static final String MESSAGERULE = "Detect unoptimized image format"; | ||
protected static final String MESSAGEERROR = "If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format."; | ||
protected static final Pattern IMGEXTENSION = Pattern.compile("\\.(bmp|ico|tiff|webp|png|jpg|jpeg|jfif|pjpeg|pjp|gif|avif|apng)"); | ||
|
||
@Override | ||
public void initialize(Context context) { | ||
context.registerSyntaxNodeConsumer(Tree.Kind.STRING_LITERAL, this::visitNodeString); | ||
} | ||
|
||
public void visitNodeString(SubscriptionContext ctx) { | ||
if (ctx.syntaxNode().is(Tree.Kind.STRING_LITERAL)) { | ||
final StringLiteral stringLiteral = (StringLiteral) ctx.syntaxNode(); | ||
final String strValue = stringLiteral.trimmedQuotesValue(); | ||
final Matcher matcher = IMGEXTENSION.matcher(strValue); | ||
if(matcher.find()) { | ||
ctx.addIssue(stringLiteral, MESSAGEERROR); | ||
} | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/EC203.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<p>If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.</p> | ||
<p>Because SVGs are generally smaller than other image format, they’re less taxing on your server despite needing to render on load.</p> | ||
<p>When to use SVG : | ||
<ul> | ||
<li>Your image is used for decorative website graphics, logos, icons, graphs and diagrams, and other simple images.</li> | ||
<li>You image require animation.</li> | ||
<li>You image need to be responsive and scale without lack of quality.</li> | ||
</ul> | ||
</p> | ||
<p>Some advantages of using SVG : | ||
<ul> | ||
<li>SVGs are scalable and will render pixel-perfect at any resolution whereas JPEGs, PNGs and GIFs will not.</li> | ||
<li>SVGs are vector images and therefore are usually much smaller in file-size than bitmap-based images.</li> | ||
<li>SVGs can be embedded into the HTML which means they can be cached, edited directly using CSS and indexed for greater accessibility.</li> | ||
<li>SVGs can be animated directly or by using CSS or JavaScript making it easy for web designers to add interactivity to a site.</li> | ||
</ul> | ||
</p> | ||
|
||
|
||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
... | ||
img_jpg = "image.jpg" | ||
... | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
... | ||
img_svg = "image.svg" | ||
... | ||
</pre> | ||
|
||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
public void foo() { | ||
... | ||
image_format = testImage("image.jpg") | ||
... | ||
} | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
public void foo() { | ||
... | ||
image_format = testImage("image.svg") | ||
... | ||
} | ||
</pre> | ||
|
||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
public void foo() { | ||
... | ||
return '<html><img src="xx/xx/image.bmp"></html>' | ||
... | ||
} | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
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>') | ||
... | ||
} | ||
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
.../src/test/java/fr/greencodeinitiative/python/checks/DetectUnoptimizedImageFormatTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package fr.greencodeinitiative.python.checks; | ||
|
||
import org.junit.Test; | ||
import org.sonar.python.checks.utils.PythonCheckVerifier; | ||
|
||
public class DetectUnoptimizedImageFormatTest { | ||
|
||
@Test | ||
public void test() { | ||
PythonCheckVerifier.verify("src/test/resources/checks/detectUnoptimizedImageFormat.py", new DetectUnoptimizedImageFormat()); | ||
PythonCheckVerifier.verifyNoIssue("src/test/resources/checks/detectUnoptimizedImageFormatCompliant.py", new DetectUnoptimizedImageFormat()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,4 +26,4 @@ def boo(): | |
if os.path.isfile(path): | ||
fh = open(path, 'r') | ||
print(fh.read()) | ||
fh.close | ||
fh.close |
36 changes: 36 additions & 0 deletions
36
python-plugin/src/test/resources/checks/detectUnoptimizedImageFormat.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
def testImage(image) : | ||
return "path/to/" + image | ||
|
||
|
||
def testImageFormat2() : | ||
|
||
img_bmp = "test/image.bmp" # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
img_ico = "image.ico" # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
img_tiff = "test/path/to/image.tiff" # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
img_webp = "test/path/to/" + "image.webp" # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
img_jpg = "image.jpg" # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
img_jpeg = "image.jpeg" # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
img_jfif = "image.jfif" # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
img_pjpeg = "image.pjpeg" # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
img_pjp = "image.pjp" # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
img_gif = "image.gif" # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
img_avif = "image.avif" # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
img_apng = "image.apng" # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
|
||
image_format = testImage("image.jpg") # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
|
||
return ('<html><img src="xx/xx/image.bmp" >' # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
+ '<img src="xx/xx/image.ico" >' # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
+ '<img src="xx/xx/image.tiff" >' # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
+ '<img src="xx/xx/image.webp" >' # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
+ '<img src="xx/xx/image.png" >' # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
+ '<img src="xx/xx/image.jpg" >' # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
+ '<img src="xx/xx/image.jpeg" >' # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
+ '<img src="xx/xx/image.jfif" >' # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
+ '<img src="xx/xx/image.pjpeg" >' # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
+ '<img src="xx/xx/image.pjp" >' # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
+ '<img src="xx/xx/image.gif" >' # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
+ '<img src="xx/xx/image.avif" >' # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
+ '<img src="xx/xx/image.apng" >' # Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}} | ||
+ '</html>' ) |
17 changes: 17 additions & 0 deletions
17
python-plugin/src/test/resources/checks/detectUnoptimizedImageFormatCompliant.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
def testImage(image) : | ||
return "path/to/" + image | ||
|
||
|
||
def testImageFormat2() : | ||
|
||
img_svg = "test/image.svg" # Compliant | ||
|
||
image_format = testImage("image.svg") # Compliant | ||
|
||
image_svg_html = ('<html><svg width="100" height="100">' + # Compliant | ||
'<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />' + | ||
'</svg></html>') | ||
|
||
return ('<html><img src="xx/xx/image.svg" >' # Compliant | ||
+ '</html>' ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,4 @@ def my_function(): | |
my_function() | ||
pass | ||
|
||
|
||
|
||
my_function() | ||
my_function() |