Skip to content

Commit 36b9f82

Browse files
committed
pr feedbacks
1 parent 7923e0c commit 36b9f82

5 files changed

Lines changed: 24 additions & 64 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- [#40](https://github.com/green-code-initiative/creedengo-javascript/pull/40) Add rule `@creedengo/avoid-autoplay` (GCI36)
13+
- [#46](https://github.com/green-code-initiative/creedengo-javascript/pull/46) Add rule `@creedengo/prefer-lighter-formats-for-image-files` (GCI31)
1314

1415
## [2.0.0] - 2025-01-22
1516

eslint-plugin/docs/rules/prefer-lighter-formats-for-image-files.md

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ Some image formats are generally considered better for eco-friendly web design a
1212

1313
We recommend using the following formats:
1414

15-
- *WebP*, developed by Google, is a modern image format that provides high compression efficiency without significant loss of quality.
16-
- *AVIF* (AV1 Image File Format) is a relatively new and highly efficient image format that is based on the AV1 video codec.
17-
- *SVG* (Scalable Vector Graphics) is a vector image format that is based on XML.
18-
Files are lightweight and can be scaled without loss of quality.
15+
- **WebP**, developed by Google, is a modern image format that provides high compression efficiency without significant loss of quality.
16+
- **AVIF** (AV1 Image File Format) is a relatively new and highly efficient image format that is based on the AV1 video codec.
17+
- **SVG** (Scalable Vector Graphics) is a vector image format that is based on XML.
18+
Files are lightweight and can be scaled without loss of quality.
1919

2020
```html
21-
<img src="./assets/images/cat.jpg" alt="Unoptimized image of a cat"/> // Non-compliant
21+
<img src="./assets/images/cat.jpg" alt="Unoptimized image of a cat" /> //
22+
Non-compliant
2223

23-
<img src="./assets/images/cat.webp" alt="Optimized image of a cat"/> // Compliant
24+
<img src="./assets/images/cat.webp" alt="Optimized image of a cat" /> //
25+
Compliant
2426
```
2527

2628
Remember that the best image format may vary depending on the specific use case, content, and requirements of your website.
@@ -43,7 +45,7 @@ Format importantly impacts images size: on average, .webp images will be 30% lig
4345
images or .png images. .avif images can be up to 20% lighter than .webp image and 50% lighter than .jepg images.
4446

4547
Don't forget to pay attention to browser support. .webp images will not be recognized by
46-
old browsers and will not be displayed. It is possible to provide several formats for the same image
48+
old browsers and will not be displayed. It is possible to provide several formats for the same image
4749
to overcome this issue. Some server-side modules (such as Google's modPageSpeed, also available for Apache
4850
and Nginx) even allow you to provide the appropriate image for the browser that is calling the server.
4951

@@ -58,13 +60,13 @@ Many tools will help you minimize images size:
5860
### Example
5961

6062
In this example, the DOM <picture> element informs the browser that there are two images: a .webp image and a
61-
.jpg image, which is used by default. The browser will decide which image will be downloaded. If the .webp format
63+
.jpg image, which is used by default. The browser will decide which image will be downloaded. If the .webp format
6264
is supported, the image.webp image will be downloaded; otherwise, image.jpg image will be downloaded.
6365

6466
```html
6567
<picture>
66-
<source srcset="image.webp" type="image/webp">
67-
<img src="image.jpg" alt="..." loading="lazy">
68+
<source srcset="image.webp" type="image/webp" />
69+
<img src="image.jpg" alt="..." loading="lazy" />
6870
</picture>
6971
```
7072

@@ -76,25 +78,11 @@ To address this issue, you can supply multiple formats for the same image.
7678

7779
### Documentation
7880

79-
- https://github.com/cnumr/best-practices/blob/main/chapters/BP_080_en.md[CNUMR best practices] - Optimize images
81+
- [CNUMR best practices](https://github.com/cnumr/best-practices/blob/main/chapters/BP_080_en.md) - Optimize images
82+
- [WSG UX15-2](https://w3c.github.io/sustyweb/star.html#UX15-2) - Optimizing All Image Assets for a Variety of Different Resolutions
83+
- [RGESN 5.1](https://ecoresponsable.numerique.gouv.fr/publications/referentiel-general-ecoconception/critere/5.1/) - Référentiel général d'écoconception de services numériques 🇫🇷
8084

8185
### Articles & blog posts
8286

83-
- https://greenspector.com/en/which-image-format-to-choose-to-reduce-its-energy-consumption-and-its-environmental-impact/[greenspector.com - Which image format choose to reduce energy consumption and environmental impact?]
84-
- https://dodonut.com/blog/use-cases-of-web-image-formats/[dodonut.com - The Most Efficient Web Image Formats. Use Cases For Different Types Of Images.]
85-
86-
### WSG
87-
- [UX15-2: Optimizing All Image Assets for a Variety of Different Resolutions](https://w3c.github.io/sustyweb/star.html#UX15-2)
88-
89-
### RGESN 5.1
90-
91-
- https://ecoresponsable.numerique.gouv.fr/publications/referentiel-general-ecoconception/critere/5.1/
92-
93-
#### Objective
94-
Reduce the size of files downloaded by users.
95-
96-
#### How to implement
97-
Choose the image format best suited to the type of image and display context: use vector formats such as svg whenever possible (illustrations, icons, logos, graphs, etc.), jpg for photos and png for illustrations with solid colors.
98-
99-
#### Means of test or control
100-
Evaluate the relevance of the image format displayed.
87+
- [greenspector.com - Which image format choose to reduce energy consumption and environmental impact?](https://greenspector.com/en/which-image-format-to-choose-to-reduce-its-energy-consumption-and-its-environmental-impact/)
88+
- [dodonut.com - The Most Efficient Web Image Formats. Use Cases For Different Types Of Images.](https://dodonut.com/blog/use-cases-of-web-image-formats/)

eslint-plugin/lib/rules/prefer-lighter-formats-for-image-files.js

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ module.exports = {
2828
recommended: "warn",
2929
},
3030
messages: {
31-
DefineFormatsForImageFiles:
32-
"You should define a format for your image files and use light formats such as {{ eligibleExtensions }}",
3331
PreferLighterFormatsForImageFiles:
34-
"You should use lighter formats for image files such as : {{ eligibleExtensions }}",
32+
"You should use lighter formats for image files such as {{ eligibleExtensions }}",
3533
},
3634
schema: [],
3735
},
3836
create(context) {
37+
const eligibleExtensions = ["webp", "avif", "svg", "jxl"];
38+
3939
return {
4040
JSXOpeningElement(node) {
4141
const tagName = node.name.name;
@@ -44,8 +44,6 @@ module.exports = {
4444
const parentTagName = node.parent?.parent?.openingElement?.name?.name;
4545
if (parentTagName?.toLowerCase() === "picture") return;
4646

47-
const eligibleExtensions = ["webp", "avif", "svg", "jxl"];
48-
4947
const srcAttribut = node.attributes.find(
5048
(attr) => attr.name.name === "src",
5149
);
@@ -57,14 +55,7 @@ module.exports = {
5755
srcValue = srcValue.substring(srcValue.lastIndexOf("/") + 1);
5856
const dotIndex = srcValue.lastIndexOf(".");
5957

60-
if (dotIndex === -1) {
61-
context.report({
62-
node,
63-
messageId: "DefineFormatsForImageFiles",
64-
data: { eligibleExtensions: eligibleExtensions.join(", ") },
65-
});
66-
return;
67-
}
58+
if (dotIndex === -1) return;
6859

6960
const imgExtension = srcValue.substring(dotIndex + 1);
7061

eslint-plugin/tests/lib/rules/prefer-lighter-formats-for-image-files.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ const ruleTester = new RuleTester({
3838
},
3939
},
4040
});
41-
const defineFormatsForImageFilesError = {
42-
messageId: "DefineFormatsForImageFiles",
43-
type: "JSXOpeningElement",
44-
};
41+
4542
const preferLighterFormatsForImageFilesError = {
4643
messageId: "PreferLighterFormatsForImageFiles",
4744
type: "JSXOpeningElement",
@@ -82,23 +79,5 @@ ruleTester.run("prefer-lighter-formats-for-image-files", rule, {
8279
`,
8380
errors: [preferLighterFormatsForImageFilesError],
8481
},
85-
{
86-
code: `
87-
<img src="./assets/images/cat" alt="A cat"/>
88-
`,
89-
errors: [defineFormatsForImageFilesError],
90-
},
91-
{
92-
code: `
93-
<img src="assets/images.dir/cat" alt="A cat"/>
94-
`,
95-
errors: [defineFormatsForImageFilesError],
96-
},
97-
{
98-
code: `
99-
<img src="./assets/images.dir/cat" alt="A cat"/>
100-
`,
101-
errors: [defineFormatsForImageFilesError],
102-
},
10382
],
10483
});

sonar-plugin/src/main/resources/org/greencodeinitiative/creedengo/profiles/javascript_profile.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"GCI26",
1010
"GCI29",
1111
"GCI30",
12+
"GCI31",
1213
"GCI36",
1314
"GCI523",
1415
"GCI530"

0 commit comments

Comments
 (0)