Skip to content

Commit d97ed0d

Browse files
committed
Update documentation of eslint plugin rules
1 parent bfd92fc commit d97ed0d

10 files changed

+281
-177
lines changed

eslint-plugin/docs/rules/avoid-css-animations.md

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,38 @@
44

55
<!-- end auto-generated rule header -->
66

7-
## Rule Details
7+
## Why is this an issue?
88

9-
This rule aims to limit the usage of all types of CSS animations which can be very costly in terms of CPU and memory.
10-
They must only be used when they are indispensable and should be limited to the CSS properties `opacity` and `transform`
11-
with it's associated functions : `translate`, `rotate` and `scale`. You can also inform the navigator of upcoming
12-
changes with the `will-change` instruction for more optimization.
9+
CSS animations, especially complex or continuous animations, can impact the performance of a web page.
10+
They may consume significant browser resources, leading to slower page rendering and decreased user experience,
11+
particularly on devices with limited processing power.
1312

14-
## Examples
13+
On mobile devices, constant animations can contribute to increased power consumption, affecting the device's battery
14+
life.
15+
Limiting the usage of CSS animations helps in creating a more energy-efficient and mobile-friendly user experience.
1516

16-
Examples of **non compliant** code for this rule:
17+
```jsx
18+
<div style={{ border: "1px solid black", transition: "border 2s ease" }} /> // Non-compliant
19+
```
1720

18-
```js
19-
<div style={{ border: "1px solid black", transition: "border 2s ease" }}/>
21+
```jsx
22+
<div style={{ border: "1px solid black" }} /> // Compliant
2023
```
2124

22-
Examples of **compliant** code for this rule:
25+
It's important to note that while limiting animations is generally advisable for certain scenarios, there are cases
26+
where animations contribute positively to the user experience and overall design.
27+
In this case they should be limited to the CSS properties `opacity` and `transform` with it's associated
28+
functions : `translate`, `rotate` and `scale`.
29+
You can also inform the navigator of upcoming changes with the `will-change` instruction for more optimization.
2330

24-
```js
25-
<div style={{ border: "1px solid black" }}/>
26-
```
31+
## Resources
32+
33+
### Documentation
34+
35+
- [CNUMR best practices](https://github.com/cnumr/best-practices/blob/main/chapters/BP_039_en.md) - Avoid JavaScript /
36+
CSS animations
2737

28-
## Further details
38+
### Articles & blog posts
2939

30-
This recommendation is made by the
31-
CNUMR: [Avoid JavaScript / CSS animations](https://github.com/cnumr/best-practices/blob/main/chapters/BP_039_en.md)
40+
- [web.dev - Animations and performance](https://web.dev/articles/animations-and-performance)
41+
- [web.dev - How to create high-performance CSS animations](https://web.dev/articles/animations-guide)

eslint-plugin/docs/rules/avoid-high-accuracy-geolocation.md

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,45 @@
44

55
<!-- end auto-generated rule header -->
66

7-
## Rule details
7+
## Why is this an issue?
88

9-
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.
9+
High-precision geolocation typically requires more power from the device's GPS hardware.
10+
By requesting less accurate geolocation, you can reduce the power consumption, leading to extended battery life, which
11+
is crucial for mobile devices.
1012

11-
## Examples
12-
13-
Examples of **non compliant** code for this rule:
13+
Obtaining highly accurate geolocation often involves more complex calculations and processing, which can increase CPU
14+
usage.
15+
If the application or service does not critically require pinpoint accuracy, opting for a less accurate geolocation can
16+
help minimize the strain on the device's CPU.
1417

1518
```js
16-
var options = { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 };
17-
function success(pos) {
18-
console.log(pos.coords);
19-
}
20-
21-
function error(err) {
22-
console.warn(err);
23-
}
24-
25-
navigator.geolocation.getCurrentPosition(success, error, options);
19+
var options = { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 }; // Non-compliant
20+
navigator.geolocation.getCurrentPosition(
21+
(pos) => console.log(pos),
22+
(err) => console.warn(err),
23+
options
24+
);
2625
```
2726

28-
Examples of **compliant** code for this rule:
27+
In these examples, the enableHighAccuracy option is set to false (the default), indicating that the application prefers
28+
lower-accuracy geolocation to conserve resources:
2929

3030
```js
31-
// enableHighAccuracy is false by default, so not declaring it is correct
32-
function success(pos) {
33-
console.log(pos);
34-
}
35-
navigator.geolocation.getCurrentPosition(success);
31+
navigator.geolocation.getCurrentPosition((pos) => console.log(pos)); // Compliant by default
3632
```
3733

3834
```js
39-
var options = { enableHighAccuracy: false, timeout: 5000, maximumAge: 0 };
40-
function success(pos) {
41-
console.log(pos.coords);
42-
}
35+
var options = { enableHighAccuracy: false, timeout: 5000, maximumAge: 0 }; // Compliant
36+
navigator.geolocation.getCurrentPosition(
37+
(pos) => console.log(pos),
38+
(err) => console.warn(err),
39+
options
40+
);
41+
```
4342

44-
function error(err) {
45-
console.warn(err);
46-
}
43+
## Resources
4744

48-
navigator.geolocation.getCurrentPosition(success, error, options);
49-
```
45+
### Documentation
46+
47+
- [Mozilla Web Technology for Developers](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition) -
48+
getCurrentPosition() method

eslint-plugin/docs/rules/limit-db-query-results.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,37 @@
44

55
<!-- end auto-generated rule header -->
66

7-
## Rule Details
7+
## Why is this an issue?
88

9-
This rule aims at reducing CPU consumption by limiting the number of returns for a single SQL query.
9+
SQL queries often involve processing large amounts of data, and fetching a large number of rows can consume significant
10+
CPU resources.
11+
By limiting the number of rows returned, you reduce the amount of processing that needs to be done by the database
12+
engine, which in turn lowers CPU consumption.
1013

11-
## Examples
14+
Transmitting a large number of rows over a network can be resource-intensive.
15+
By restricting the result set size, you reduce the amount of data that needs to be transferred between the database and
16+
the application, improving network efficiency.
1217

13-
Examples of **non compliant** code for this rule:
18+
If you store data about customers, you certainly don’t need to retrieve information of all at once, because the larger
19+
the table will be, the more elements the query will return.
1420

1521
```js
16-
const query = "SELECT * FROM clients";
22+
const query = "SELECT * FROM customers"; // Non-compliant
1723
```
1824

19-
Examples of **compliant** code for this rule:
25+
It may therefore be a good idea to limit the results and use pagination, for example.
2026

2127
```js
22-
const query = "SELECT columns FROM table_name FETCH FIRST number ROWS ONLY";
28+
const query = "SELECT id,name,email FROM customers FETCH FIRST 10 ROWS ONLY"; // Compliant
2329
```
30+
31+
## Resources
32+
33+
### Documentation
34+
35+
- [MySQL Reference Manual](https://dev.mysql.com/doc/refman/8.0/en/limit-optimization.html) - LIMIT Query Optimization
36+
- [PostgreSQL: Documentation](https://www.postgresql.org/docs/current/queries-limit.html) - LIMIT and OFFSET
37+
38+
### Articles & blog posts
39+
40+
- [Query Performance Optimization](https://www.oreilly.com/library/view/high-performance-mysql/9780596101718/ch04.html)

eslint-plugin/docs/rules/no-empty-image-src-attribute.md

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,50 @@
44

55
<!-- end auto-generated rule header -->
66

7-
## Rule Details
8-
9-
This rule aims to prohibit the usage of <img/> without specifying its source attribute because it causes extra and unnecessary http requests. This rule is build for the [react library](https://react.dev/) and JSX.
7+
## Why is this an issue?
8+
9+
When the src attribute is missing, some browsers may still attempt to make a request to the current URL (the base URL of
10+
the document) in an attempt to fetch the image.
11+
This can lead to unnecessary server requests, impacting performance and potentially causing errors.
12+
13+
Proper use of the src attribute is essential for web accessibility.
14+
Screen readers and other assistive technologies rely on valid image sources to provide meaningful information to users
15+
with disabilities.
16+
A missing src attribute can result in confusion and hinder accessibility.
17+
18+
```jsx
19+
return (
20+
<>
21+
<img src="" /> // Non-compliant
22+
<img /> // Non-compliant
23+
</>
24+
)
25+
```
1026

11-
## Examples
27+
The HTML specification requires the src attribute for the <img> element, and not including it may lead to non-compliance
28+
with standards.
1229

13-
Examples of **non compliant** code for this rule:
30+
```jsx
31+
import myLogo from "./logo.svg"
1432

15-
```js
16-
<img src='' />
17-
<img/>
33+
return (
34+
<>
35+
<img src="./logo.svg" /> // Compliant
36+
<img src={myLogo} /> // Compliant
37+
</>
38+
)
1839
```
1940

20-
Examples of **compliant** code for this rule:
41+
This rule is build for [React](https://react.dev/) and JSX.
42+
43+
## Resources
44+
45+
### Documentation
46+
47+
- [Mozilla Web Technology for Developers](https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Images_in_HTML) -
48+
Images in HTML
49+
50+
### Articles & blog posts
51+
52+
- [Empty image src can destroy your site](https://humanwhocodes.com/blog/2009/11/30/empty-image-src-can-destroy-your-site/)
2153

22-
```js
23-
import imgSvg from './img.svg'
24-
<img src='./img.svg' />
25-
<img src={imgSvg} />
26-
```

eslint-plugin/docs/rules/no-import-all-from-library.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
<!-- end auto-generated rule header -->
66

7-
## Rule details
7+
## Why is this an issue?
88

9-
This rule aims to reduce weight of programs by using only needed modules. Many libraries export only one module by
10-
default, but some of them are exporting ES modules or submodules. We should use them to select more precisly needed
11-
modules and avoid unnecessarily overloading files weight.
9+
Including only the required modules decreases the overall size of the program.
10+
This, in turn, reduces the amount of memory and storage space needed to run and store the application.
11+
This is especially critical in environments with limited resources, such as on mobile devices or in web applications
12+
where bandwidth and download times matter.
13+
14+
Smaller programs generally have better runtime performance.
15+
Reducing the number of unnecessary modules minimizes the amount of code that needs to be interpreted or compiled,
16+
leading to faster execution and improved overall performance.
1217

1318
## Options
1419

@@ -32,7 +37,8 @@ module.exports = {
3237

3338
## Examples
3439

35-
Examples of **non-compliant** code for this rule:
40+
**Example with the well-known [lodash](https://lodash.com/) library, if you only need
41+
`isEmpty` method.**
3642

3743
```js
3844
// Example with lodash
@@ -73,3 +79,14 @@ Size of your bundle, if you use only the "isEmpty" method:
7379
* isArrayLike.js - 830 B
7480
* ...
7581
* index.js - 110 B
82+
83+
## Resources
84+
85+
### Documentation
86+
87+
- [Mozilla Web Technology for Developers](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import) -
88+
import
89+
90+
### Articles & blog posts
91+
92+
- [Importing modules in JavaScript, are we doing it right?](https://dev.to/dianjuar/importing-modules-in-javascript-are-we-doing-it-right-nc)

eslint-plugin/docs/rules/no-multiple-access-dom-element.md

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,40 @@
44

55
<!-- end auto-generated rule header -->
66

7-
## Rule details
7+
## Why is this an issue?
88

9-
This rule aims to reduce DOM access assigning its object to variable when access multiple time. It saves CPU cycles.
9+
Accessing the Document Object Model (DOM) is a relatively expensive operation in terms of performance.
10+
Each time you access the DOM, the browser needs to traverse the document tree to find the requested element.
11+
By assigning the DOM object to a variable when accessed multiple times, you avoid redundant traversals, leading to
12+
improved performance.
1013

11-
## Examples
14+
Assigning the DOM object to a variable not only improves performance but also enhances code readability.
15+
It makes the code more concise and self-explanatory.
16+
Developers reading the code can understand that the variable holds a reference to a specific DOM element, and its
17+
subsequent use is likely for multiple operations.
1218

13-
Examples of **incorrect** code for this rule:
19+
Here's an example in JavaScript to illustrate this rule:
1420

1521
```js
16-
var el1 = document.getElementById("block1").test1;
17-
var el2 = document.getElementById("block1").test2;
22+
const width = document.getElementById('block').clientWidth;
23+
const height = document.getElementById('block').clientHeight; // Non-compliant
1824
```
1925

20-
Examples of **correct** code for this rule:
21-
2226
```js
23-
var blockElement = document.getElementById("block1");
24-
var el1 = blockElement.test1;
25-
var el2 = blockElement.test2;
27+
const blockElement = document.getElementById('block'); // Compliant
28+
const width = blockElement.clientWidth;
29+
const height = blockElement.clientHeight;
2630
```
31+
32+
In the first example, getElementById is called twice, potentially resulting in two separate traversals of the DOM tree.
33+
In the second example, the DOM element reference is cached in the `blockElement` variable, and subsequent property
34+
accesses use this cached reference.
35+
36+
## Resources
37+
38+
### Documentation
39+
40+
- [CNUMR best practices](https://github.com/cnumr/best-practices/blob/main/chapters/BP_054_en.md) - Reduce DOM access
41+
via JavaScript
42+
- [Mozilla Web Technology for Developers](https://developer.mozilla.org/en-US/docs/Learn/Performance/JavaScript#tips_for_writing_more_efficient_code) -
43+
Tips for writing more efficient code

0 commit comments

Comments
 (0)