-
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 branch 'main' into feature/add-swift-rules
- Loading branch information
Showing
39 changed files
with
835 additions
and
245 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
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 |
---|---|---|
@@ -1,15 +1,45 @@ | ||
# ecoCode rules specification repository | ||
|
||
## Description | ||
This project contains the specifications of all ecoCode rules, for all languages. | ||
|
||
This project contains the specifications of the ecoCode rules. | ||
## Structure | ||
|
||
All the existing rules can be found in the [rules folder](src/main/rules). | ||
Rules are organized by folder based on their ID in the [root rules folder](src/main/rules). | ||
Each of these folders contains a file with the metadata of the rule, and description by language. | ||
|
||
The metadata file uses the format supported by | ||
the [SonarSource Analyzers Commons](https://github.com/SonarSource/sonar-analyzer-commons/tree/master/commons) library. | ||
To find out what values can be put there, we advise you to use the | ||
official [SonarQube documentation](https://docs.sonarsource.com/sonarqube/latest/user-guide/rules/overview/), and to | ||
rely on already existing files. | ||
|
||
Here is an example: | ||
|
||
```text | ||
src/main/rules | ||
├── EC104 | ||
│ ├── java | ||
│ │ ├── EC104.asciidoc | ||
│ │ ├── EC104.json | ||
│ ├── php | ||
│ │ ├── EC104.asciidoc | ||
│ ├── python | ||
│ │ ├── EC104.asciidoc | ||
│ └── EC104.json | ||
├── ... | ||
``` | ||
|
||
To specify metadata for a given language (for example deprecate a rule only for a single language), it is possible to | ||
create a json file in the language folder, and this will be merged with the common file during build. The keys in the | ||
specific file have priority and it is possible to add new ones but not to delete them from the global one. | ||
|
||
## 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). | ||
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/) |
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: 28 additions & 13 deletions
41
ecocode-rules-specifications/src/main/rules/EC11/javascript/EC11.asciidoc
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 |
---|---|---|
@@ -1,21 +1,36 @@ | ||
This rule aims to reduce DOM access assigning its object to variable when access multiple time. | ||
It saves CPU cycles. | ||
:!sectids: | ||
|
||
== Examples | ||
== Why is this an issue? | ||
|
||
Examples of **incorrect** code for this rule: | ||
Accessing the Document Object Model (DOM) is a relatively expensive operation in terms of performance. | ||
Each time you access the DOM, the browser needs to traverse the document tree to find the requested element. | ||
By assigning the DOM object to a variable when accessed multiple times, you avoid redundant traversals, leading to improved performance. | ||
|
||
[source,js] | ||
Assigning the DOM object to a variable not only improves performance but also enhances code readability. | ||
It makes the code more concise and self-explanatory. | ||
Developers reading the code can understand that the variable holds a reference to a specific DOM element, and its subsequent use is likely for multiple operations. | ||
|
||
Here's an example in JavaScript to illustrate this rule: | ||
|
||
[source,js,data-diff-id="2",data-diff-type="noncompliant"] | ||
---- | ||
var el1 = document.getElementById("block1").test1; | ||
var el2 = document.getElementById("block1").test2; | ||
const width = document.getElementById('block').clientWidth; | ||
const height = document.getElementById('block').clientHeight; // Non-compliant | ||
---- | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
[source,js] | ||
[source,js,data-diff-id="1",data-diff-type="noncompliant"] | ||
---- | ||
var blockElement = document.getElementById("block1"); | ||
var el1 = blockElement.test1; | ||
var el2 = blockElement.test2; | ||
const blockElement = document.getElementById('block'); // Compliant | ||
const width = blockElement.clientWidth; | ||
const height = blockElement.clientHeight; | ||
---- | ||
|
||
In the first example, getElementById is called twice, potentially resulting in two separate traversals of the DOM tree. | ||
In the second example, the DOM element reference is cached in the `blockElement` variable, and subsequent property accesses use this cached reference. | ||
|
||
== Resources | ||
|
||
=== Documentation | ||
|
||
- https://github.com/cnumr/best-practices/blob/main/chapters/BP_054_en.md[CNUMR best practices] - Reduce DOM access via JavaScript | ||
- https://developer.mozilla.org/en-US/docs/Learn/Performance/JavaScript#tips_for_writing_more_efficient_code[Mozilla Web Technology for Developers] - Tips for writing more efficient code |
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
Oops, something went wrong.