Skip to content

Commit

Permalink
Merge branch 'main' into feature/no-torch
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn authored Jun 5, 2024
2 parents d635e0d + 4f8ec12 commit f085881
Show file tree
Hide file tree
Showing 14 changed files with 376 additions and 9 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- [#39](https://github.com/green-code-initiative/ecoCode-javascript/issues/39) Add rule `@ecocode/avoid-brightness-override` (EC522)
- Add support for SonarQube up to 10.5

### Changed

- Update Docker Compose configuration file to V2
- [#44](https://github.com/green-code-initiative/ecoCode-javascript/pull/44) Implement the rule EC523 for React Native

### Deleted

- [#44](https://github.com/green-code-initiative/ecoCode-javascript/pull/44) Merge the rule EC8 with EC523

## [1.5.0] - 2024-03-13

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This project proposes rules for the following technologies:
- TypeScript
- NestJS
- React (JSX)
- React Native / Expo

🔧 ESLint plugin
----------------
Expand Down
1 change: 1 addition & 0 deletions eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Add `@ecocode` to the `plugins` section of your `.eslintrc`, followed by rules c

| Name | Description | ⚠️ |
| :------------------------------------------------------------------------------------- | :--------------------------------------------------------- | :- |
| [avoid-brightness-override](docs/rules/avoid-brightness-override.md) | Should avoid to override brightness ||
| [avoid-css-animations](docs/rules/avoid-css-animations.md) | Avoid usage of CSS animations ||
| [avoid-high-accuracy-geolocation](docs/rules/avoid-high-accuracy-geolocation.md) | Avoid using high accuracy geolocation in web applications. ||
| [limit-db-query-results](docs/rules/limit-db-query-results.md) | Should limit the number of returns for a SQL query ||
Expand Down
36 changes: 36 additions & 0 deletions eslint-plugin/docs/rules/avoid-brightness-override.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Should avoid to override brightness (`@ecocode/avoid-brightness-override`)

⚠️ This rule _warns_ in the ✅ `recommended` config.

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

## Why is this an issue?

To avoid draining the battery, IOS and Android devices adapt the brightness of the screen depending on the environment light.

For some reasons, developers may disable this feature programmatically.

This feature was introduced to improve battery life, be careful when deactivating it.

Hence, keeping forcing the screen brightness on should be avoided, unless it is absolutely necessary.


## Example of non compliant code

```js
// Example with expo-brightness (Expo framework library)
import React, { useEffect } from 'react';
import { View, Text } from 'react-native';
import * as Brightness from 'expo-brightness';

export default function App() {
useEffect(() => {
(async () => { Brightness.setSystemBrightnessAsyn(1); })(); // Brightness is forced here
}, []);
return (
<View>
<Text>Brightness Module Example</Text>
</View>
);
}
```
16 changes: 16 additions & 0 deletions eslint-plugin/docs/rules/avoid-high-accuracy-geolocation.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ usage.
If the application or service does not critically require pinpoint accuracy, opting for a less accurate geolocation can
help minimize the strain on the device's CPU.

## Web
```js
var options = { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 }; // Non-compliant
navigator.geolocation.getCurrentPosition(
Expand All @@ -40,6 +41,21 @@ navigator.geolocation.getCurrentPosition(
);
```

## React Native
In this example, we ask the user to turn on high accuracy location mode which enables network provider that uses Google Play services to improve location accuracy and location-based services:
```js
import * as Location from 'expo-location';

Location.enableNetworkProviderAsync(); // Non-compliant
```

Prefer to ask the user to turn on lower-accuracy geolocation to conserve resources:
```js
import * as Location from 'expo-location';

Location.requestPermissionsAsync(); // Compliant
```

## Resources

### Documentation
Expand Down
76 changes: 76 additions & 0 deletions eslint-plugin/lib/rules/avoid-brightness-override.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* ecoCode JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

"use strict";

const brightnessLibrariesMethods = {
"expo-brightness": [
"setBrightnessAsync",
"setSystemBrightnessAsync",
"setSystemBrightnessAsync",
],
"react-native-device-brightness": ["setBrightnessLevel"],
"react-native-screen-brightness": ["setBrightness"],
"@capacitor-community/screen-brightness": ["setBrightness"],
};

/** @type {import("eslint").Rule.RuleModule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "Should avoid to override brightness",
category: "eco-design",
recommended: "warn",
},
messages: {
ShouldAvoidOverrideBrightness:
"Do not force Brightness in your code, unless absolutely necessary",
},
schema: [],
},
create: function (context) {
const librariesFoundInImports = [];

return {
ImportDeclaration(node) {
const currentLibrary = node.source.value;

if (brightnessLibrariesMethods[currentLibrary]) {
librariesFoundInImports.push(currentLibrary);
}
},
MemberExpression(node) {
if (librariesFoundInImports.length === 0) {
return;
}

if (
librariesFoundInImports.some((library) =>
brightnessLibrariesMethods[library].includes(node.property.name),
)
) {
context.report({
node,
messageId: "ShouldAvoidOverrideBrightness",
});
}
},
};
},
};
32 changes: 31 additions & 1 deletion eslint-plugin/lib/rules/avoid-high-accuracy-geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

"use strict";

const geolocationLibrariesMethods = {
"expo-location": ["enableNetworkProviderAsync"],
};

/** @type {import("eslint").Rule.RuleModule} */
module.exports = {
meta: {
Expand All @@ -33,15 +37,41 @@ module.exports = {
schema: [],
},
create: function (context) {
const librariesFoundInImports = [];

return {
ImportDeclaration(node) {
const currentLibrary = node.source.value;

if (geolocationLibrariesMethods[currentLibrary]) {
librariesFoundInImports.push(currentLibrary);
}
},
MemberExpression(node) {
if (librariesFoundInImports.length === 0) {
return;
}

if (
librariesFoundInImports.some((library) =>
geolocationLibrariesMethods[library].includes(node.property.name),
)
) {
reportAvoidUsingAccurateGeolocation(context, node);
}
},
Property(node) {
if (
node?.key.name === "enableHighAccuracy" &&
node?.value.value === true
) {
context.report({ node, messageId: "AvoidUsingAccurateGeolocation" });
reportAvoidUsingAccurateGeolocation(context, node);
}
},
};
},
};

function reportAvoidUsingAccurateGeolocation(context, node) {
context.report({ node, messageId: "AvoidUsingAccurateGeolocation" });
}
135 changes: 135 additions & 0 deletions eslint-plugin/tests/lib/rules/avoid-brightness-override.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* ecoCode JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const rule = require("../../../lib/rules/avoid-brightness-override");
const RuleTester = require("eslint").RuleTester;

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2021,
sourceType: "module",
ecmaFeatures: {
jsx: true,
},
},
});
const expectedError = {
messageId: "ShouldAvoidOverrideBrightness",
type: "MemberExpression",
};

ruleTester.run("avoid-brightness-override", rule, {
valid: [
`
import * as lodash from 'lodash';
lodash.isEmpty('');
`,
`
import { ScreenBrightness } from '@capacitor-community/screen-brightness';
// Get the current brightness:
const {brightness: currentBrightness} = ScreenBrightness.getBrightness();
`,
`
import DeviceBrightness from 'react-native-device-brightness';
DeviceBrightness.getBrightnessLevel()
.then(function (luminous) {
// Get current brightness level
// 0 ~ 1
console.log(luminous);
});
`,
`
import * as Brightness from 'expo-brightness';
Brightness.requestPermissionsAsync();
`,
`
import ScreenBrightness from 'react-native-screen-brightness';
ScreenBrightness.getBrightness().then(brightness => {
console.log('brightness', brightness);
});
`,
],

invalid: [
{
code: `
import { ScreenBrightness } from '@capacitor-community/screen-brightness';
// Set the brightness:
const brightness = 0.5;
ScreenBrightness.setBrightness({ brightness });
`,
errors: [expectedError],
},
{
code: `
import DeviceBrightness from 'react-native-device-brightness';
DeviceBrightness.setBrightnessLevel(0.5);
`,
errors: [expectedError],
},
{
code: `
import ScreenBrightness from 'react-native-screen-brightness';
ScreenBrightness.setBrightness(0.5);
`,
errors: [expectedError],
},
{
code: `
import React, { useEffect } from 'react';
import { StyleSheet, View, Text } from 'react-native';
import * as Brightness from 'expo-brightness';
export default function App() {
useEffect(() => {
(async () => {
const { status } = await Brightness.requestPermissionsAsync();
if (status === 'granted') {
Brightness.setSystemBrightnessAsync(1);
}
})();
}, []);
return (
<View style={styles.container}>
<Text>Brightness Module Example</Text>
</View>
);
}
`,
errors: [expectedError],
},
],
});
Loading

0 comments on commit f085881

Please sign in to comment.