Skip to content

Commit

Permalink
Move all Javascript rules
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn committed Oct 27, 2023
1 parent 3a98026 commit 9ea9efa
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 70 deletions.
4 changes: 2 additions & 2 deletions ecocode-rules-specifications/src/main/rules/EC11/EC11.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"constantCost": "5min"
},
"tags": [
"ecocode",
"eco-design",
"performance",
"ecocode"
"performance"
],
"defaultSeverity": "Major",
"compatibleLanguages": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
## Rule details
This rule aims to reduce DOM access assigning its object to variable when access multiple time.
It saves CPU cycles.

This rule aims to reduce DOM access assigning its object to variable when access multiple time. It saves CPU cycles.

## Examples
== Examples

Examples of **incorrect** code for this rule:

```js
[source,js]
----
var el1 = document.getElementById("block1").test1;
var el2 = document.getElementById("block1").test2;
```
----

Examples of **correct** code for this rule:

```js
[source,js]
----
var blockElement = document.getElementById("block1");
var el1 = blockElement.test1;
var el2 = blockElement.test2;
```
----
4 changes: 2 additions & 2 deletions ecocode-rules-specifications/src/main/rules/EC12/EC12.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"constantCost": "10min"
},
"tags": [
"ecocode",
"eco-design",
"performance",
"ecocode"
"performance"
],
"defaultSeverity": "Major",
"compatibleLanguages": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
## Rule Details

This rule aims to disallow batching multiple style changes at once.

To limit the number of repaint/reflow, it is advised to batch style modifications by adding a class containing all style changes that will generate a unique reflow.

## Examples
== Examples

Examples of **non-compliant** code for this rule:

```html
[source,html]
----
<script>
element.style.height = "800px";
element.style.width = "600px";
element.style.color = "red";
</script>
```
----

Examples of **compliant** code for this rule:

```html
[source,html]
----
<style>
.in-error {
color: red;
Expand All @@ -30,4 +30,4 @@ Examples of **compliant** code for this rule:
<script>
element.addClass("in-error");
</script>
```
----
6 changes: 3 additions & 3 deletions ecocode-rules-specifications/src/main/rules/EC13/EC13.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"constantCost": "30min"
},
"tags": [
"ecocode",
"eco-design",
"performance",
"ecocode"
"nestjs",
"performance"
],
"defaultSeverity": "Minor",
"compatibleLanguages": [
"JAVASCRIPT",
"TYPESCRIPT"
]
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
## Rule details

This rule aims to reduce the size and thus the network weight of API returns that may contain many elements. This rule is built for the https://nestjs.com[NestJS framework] but can work with a controller `@Controller()` and a decorated method `@Get()`.

## Examples
== Examples

Examples of **non-compliant** code for this rule:

```typescript
[source,typescript]
----
@Controller()
class Test {
@Get()
public find(): Promise<string[]> {}
}
```
----

Examples of **compliant** code for this rule:

```typescript
[source,typescript]
----
interface Pagination {
items: string[];
currentPage: number;
Expand All @@ -28,4 +28,4 @@ class Test {
@Get()
public find(): Promise<Pagination> {}
}
```
----
4 changes: 2 additions & 2 deletions ecocode-rules-specifications/src/main/rules/EC8/EC8.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"constantCost": "5min"
},
"tags": [
"ecocode",
"eco-design",
"performance",
"ecocode"
"performance"
],
"defaultSeverity": "Major",
"compatibleLanguages": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
## Rule details

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.

## Examples
== Examples

Examples of **non-compliant** code for this rule:

```js
[source,js]
----
var options = { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 };
function success(pos) {
console.log(pos.coords);
Expand All @@ -17,19 +16,21 @@ function error(err) {
}
navigator.geolocation.getCurrentPosition(success, error, options);
```
----

Examples of **compliant** code for this rule:

```js
[source,js]
----
// enableHighAccuracy is false by default, so not declaring it is correct
function success(pos) {
console.log(pos);
}
navigator.geolocation.getCurrentPosition(success);
```
----

```js
[source,js]
----
var options = { enableHighAccuracy: false, timeout: 5000, maximumAge: 0 };
function success(pos) {
console.log(pos.coords);
Expand All @@ -40,4 +41,4 @@ function error(err) {
}
navigator.geolocation.getCurrentPosition(success, error, options);
```
----
4 changes: 2 additions & 2 deletions ecocode-rules-specifications/src/main/rules/EC9/EC9.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
"constantCost": "15min"
},
"tags": [
"ecocode",
"eco-design",
"performance",
"ecocode"
"performance"
],
"defaultSeverity": "Major",
"compatibleLanguages": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
## Rule details

This rule aims to reduce weight of programs by using only needed modules. Many libraries export only one module by default, but some of them are exporting ES modules or submodules. We should use them to select more precisly needed modules and avoid unnecessarily overloading files weight.
This rule aims to reduce weight of programs by using only needed modules.
Many libraries export only one module by default, but some of them are exporting ES modules or submodules.
We should use them to select more precisly needed modules and avoid unnecessarily overloading files weight.

*Example with the well-known https://lodash.com/[lodash] library, if you only need
`isEmpty` method.*


|===
|Full lodash library |Only `isEmpty` method

Expand All @@ -24,46 +23,29 @@ a|index.js - 24.42 KB
|===
## Options
You can externally add your own libraries to be checked. To add your own libraries you need to modify your .eslintrc.js by adding the following rule configuration:
```js
module.exports = {
...yourConf,
rules: {
"no-import-all-from-library": [
"warn",
{
notAllowedLibraries: ["some-lib"], // will check for -> import someLib from "some-lib"
importByNamespaceNotAllowedLibraries: ["some-other-lib"], // will check for -> import * as someOtherLib from "some-other-lib"
},
],
},
};
```
## Examples
== Examples
Examples of **non-compliant** code for this rule:
```js
[source,js]
----
// Example with lodash
import lodash from "lodash";
import { isEmpty } from "lodash";
import * as lodash from "lodash";
// Example with underscore
import _ from "underscore";
```
----
Examples of **compliant** code for this rule:
```js
[source,js]
----
// Example with lodash (uses submodules)
import isEmpty from "lodash/isEmpty";
import intersect from "lodash/intersect";
// Example with underscore (uses esm modules)
import map from "underscore/modules/map.js";
```
----

0 comments on commit 9ea9efa

Please sign in to comment.