From 2ceae633d50d7392d036c9535e287ca2d673f3c3 Mon Sep 17 00:00:00 2001 From: JuBaas Date: Wed, 29 May 2024 17:41:34 +0200 Subject: [PATCH 01/13] =?UTF-8?q?=E2=9C=A8=20avoid=20brightness=20override?= =?UTF-8?q?=20rules=20implementation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/rules/avoid-brightness-override.js | 73 ++++++++++ .../lib/rules/avoid-brightness-override.js | 135 ++++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 eslint-plugin/lib/rules/avoid-brightness-override.js create mode 100644 eslint-plugin/tests/lib/rules/avoid-brightness-override.js diff --git a/eslint-plugin/lib/rules/avoid-brightness-override.js b/eslint-plugin/lib/rules/avoid-brightness-override.js new file mode 100644 index 0000000..5b132c8 --- /dev/null +++ b/eslint-plugin/lib/rules/avoid-brightness-override.js @@ -0,0 +1,73 @@ +/* + * 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 . + */ + +"use strict"; + +/** @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: [ + { + type: "object", + additionalProperties: false, + }, + ], + }, + create: function (context) { + const brightnessLibrariesMethods = { + 'expo-brightness': ['setBrightnessAsync', 'setSystemBrightnessAsync', 'setSystemBrightnessAsync'], + 'react-native-device-brightness': ['setBrightnessLevel'], + 'react-native-screen-brightness': ['setBrightness'], + '@capacitor-community/screen-brightness': ['setBrightness'] + }; + + 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", + }); + } + }, + }; + }, +}; diff --git a/eslint-plugin/tests/lib/rules/avoid-brightness-override.js b/eslint-plugin/tests/lib/rules/avoid-brightness-override.js new file mode 100644 index 0000000..3e0e218 --- /dev/null +++ b/eslint-plugin/tests/lib/rules/avoid-brightness-override.js @@ -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 . + */ + +"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 ( + + Brightness Module Example + + ); + } + `, + errors: [expectedError] + } + ], +}); From 8c184a245c4b129aa5f81ea7213d76944cc97be3 Mon Sep 17 00:00:00 2001 From: JuBaas Date: Wed, 29 May 2024 17:42:19 +0200 Subject: [PATCH 02/13] =?UTF-8?q?=F0=9F=A7=B1=20Sonar=20connector=20for=20?= =?UTF-8?q?EC522=20rule?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../checks/AvoidBrightnessOverride.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 sonar-plugin/src/main/java/io/ecocode/javascript/checks/AvoidBrightnessOverride.java diff --git a/sonar-plugin/src/main/java/io/ecocode/javascript/checks/AvoidBrightnessOverride.java b/sonar-plugin/src/main/java/io/ecocode/javascript/checks/AvoidBrightnessOverride.java new file mode 100644 index 0000000..19868c6 --- /dev/null +++ b/sonar-plugin/src/main/java/io/ecocode/javascript/checks/AvoidBrightnessOverride.java @@ -0,0 +1,37 @@ +/* + * 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 . + */ +package io.ecocode.javascript.checks; + +import org.sonar.check.Rule; +import org.sonar.plugins.javascript.api.EslintBasedCheck; +import org.sonar.plugins.javascript.api.JavaScriptRule; +import org.sonar.plugins.javascript.api.TypeScriptRule; + +@JavaScriptRule +@TypeScriptRule +@Rule(key = AvoidBrightnessOverride.RULE_KEY) +public class AvoidBrightnessOverride implements EslintBasedCheck { + + public static final String RULE_KEY = "EC522"; + + @Override + public String eslintKey() { + return "@ecocode/avoid-brightness-override"; + } + +} From 137df6058c21da0e8c3009e345b3c4157671cb8e Mon Sep 17 00:00:00 2001 From: JuBaas Date: Thu, 30 May 2024 09:31:15 +0200 Subject: [PATCH 03/13] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20move=20brightness=20?= =?UTF-8?q?libraries=20methods=20to=20a=20global=20stage=20instead=20of=20?= =?UTF-8?q?local?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/rules/avoid-brightness-override.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/eslint-plugin/lib/rules/avoid-brightness-override.js b/eslint-plugin/lib/rules/avoid-brightness-override.js index 5b132c8..5a20510 100644 --- a/eslint-plugin/lib/rules/avoid-brightness-override.js +++ b/eslint-plugin/lib/rules/avoid-brightness-override.js @@ -18,6 +18,13 @@ "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: { @@ -39,13 +46,6 @@ module.exports = { ], }, create: function (context) { - const brightnessLibrariesMethods = { - 'expo-brightness': ['setBrightnessAsync', 'setSystemBrightnessAsync', 'setSystemBrightnessAsync'], - 'react-native-device-brightness': ['setBrightnessLevel'], - 'react-native-screen-brightness': ['setBrightness'], - '@capacitor-community/screen-brightness': ['setBrightness'] - }; - const librariesFoundInImports = []; return { From 103e7f4373aa45fbd2e30d20504869db4d9db633 Mon Sep 17 00:00:00 2001 From: JuBaas Date: Thu, 30 May 2024 09:41:43 +0200 Subject: [PATCH 04/13] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20fix=20warning=20on?= =?UTF-8?q?=20equality=20checking?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eslint-plugin/lib/rules/avoid-brightness-override.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eslint-plugin/lib/rules/avoid-brightness-override.js b/eslint-plugin/lib/rules/avoid-brightness-override.js index 5a20510..a7ab76a 100644 --- a/eslint-plugin/lib/rules/avoid-brightness-override.js +++ b/eslint-plugin/lib/rules/avoid-brightness-override.js @@ -57,7 +57,7 @@ module.exports = { } }, MemberExpression(node) { - if (librariesFoundInImports.length == 0) { + if (librariesFoundInImports.length === 0) { return; } From 4ff4c7a7508f8befb277f37a9c21391266d388a6 Mon Sep 17 00:00:00 2001 From: JuBaas Date: Thu, 30 May 2024 10:31:17 +0200 Subject: [PATCH 05/13] =?UTF-8?q?=F0=9F=93=9D=20add=20documentation=20of?= =?UTF-8?q?=20rule=20EC522?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../docs/rules/avoid-brightness-override.md | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 eslint-plugin/docs/rules/avoid-brightness-override.md diff --git a/eslint-plugin/docs/rules/avoid-brightness-override.md b/eslint-plugin/docs/rules/avoid-brightness-override.md new file mode 100644 index 0000000..ad3fc3a --- /dev/null +++ b/eslint-plugin/docs/rules/avoid-brightness-override.md @@ -0,0 +1,36 @@ +# Should avoid brightness overriding (`@ecocode/avoid-brightness-override`) + +⚠️ This rule _warns_ in the ✅ `recommended` config. + + + +## 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 ( + + Brightness Module Example + + ); +} +``` From 77ef5c06858d33da742a729ced35818e92d2dcfe Mon Sep 17 00:00:00 2001 From: JuBaas Date: Thu, 30 May 2024 11:34:16 +0200 Subject: [PATCH 06/13] update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a407930..b76ec19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- add brightness override rules for mobile app framework over js (capacitor, react native...) + ### Added - Add support for SonarQube up to 10.5 From 7924ad9307dbb3a853ea910a309d216dff5d63af Mon Sep 17 00:00:00 2001 From: JuBaas Date: Thu, 30 May 2024 11:36:34 +0200 Subject: [PATCH 07/13] update changelog with issue link --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b76ec19..8937e2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- add brightness override rules for mobile app framework over js (capacitor, react native...) +- [#39](https://github.com/green-code-initiative/ecoCode-javascript/issues/39) EC522: add brightness override rules for mobile app framework over js (capacitor, react native...) ### Added From 0a7ebe78462aee564f45a53e4cc6336989d7cde1 Mon Sep 17 00:00:00 2001 From: JuBaas Date: Thu, 30 May 2024 14:00:18 +0200 Subject: [PATCH 08/13] update documentation --- eslint-plugin/README.md | 1 + eslint-plugin/docs/rules/avoid-brightness-override.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/eslint-plugin/README.md b/eslint-plugin/README.md index a5e19f7..d19d4be 100644 --- a/eslint-plugin/README.md +++ b/eslint-plugin/README.md @@ -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 | ✅ | diff --git a/eslint-plugin/docs/rules/avoid-brightness-override.md b/eslint-plugin/docs/rules/avoid-brightness-override.md index ad3fc3a..d51fa24 100644 --- a/eslint-plugin/docs/rules/avoid-brightness-override.md +++ b/eslint-plugin/docs/rules/avoid-brightness-override.md @@ -1,4 +1,4 @@ -# Should avoid brightness overriding (`@ecocode/avoid-brightness-override`) +# Should avoid to override brightness (`@ecocode/avoid-brightness-override`) ⚠️ This rule _warns_ in the ✅ `recommended` config. From 62f81107bec25d6dc6efcba09f1eefc3bd6c78bd Mon Sep 17 00:00:00 2001 From: JuBaas Date: Thu, 30 May 2024 14:21:17 +0200 Subject: [PATCH 09/13] PR feedbakcs --- CHANGELOG.md | 3 +-- eslint-plugin/lib/rules/avoid-brightness-override.js | 7 +------ 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8937e2d..b8669cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,9 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- [#39](https://github.com/green-code-initiative/ecoCode-javascript/issues/39) EC522: add brightness override rules for mobile app framework over js (capacitor, react native...) - ### Added +- [#39](https://github.com/green-code-initiative/ecoCode-javascript/issues/39) EC522: add brightness override rules for mobile app framework over js (capacitor, react native...) - Add support for SonarQube up to 10.5 diff --git a/eslint-plugin/lib/rules/avoid-brightness-override.js b/eslint-plugin/lib/rules/avoid-brightness-override.js index a7ab76a..b54e82f 100644 --- a/eslint-plugin/lib/rules/avoid-brightness-override.js +++ b/eslint-plugin/lib/rules/avoid-brightness-override.js @@ -38,12 +38,7 @@ module.exports = { ShouldAvoidOverrideBrightness: "Do not force Brightness in your code, unless absolutely necessary", }, - schema: [ - { - type: "object", - additionalProperties: false, - }, - ], + schema: [], }, create: function (context) { const librariesFoundInImports = []; From 1bde7ad00a7d49bf9ea5ceff87b8e41a89ca3b25 Mon Sep 17 00:00:00 2001 From: Ju_Baas Date: Fri, 31 May 2024 19:47:09 +0200 Subject: [PATCH 10/13] =?UTF-8?q?=F0=9F=9A=A8=20fix=20linter?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lib/rules/avoid-brightness-override.js | 84 ++++++++++--------- .../lib/rules/avoid-brightness-override.js | 64 +++++++------- 2 files changed, 78 insertions(+), 70 deletions(-) diff --git a/eslint-plugin/lib/rules/avoid-brightness-override.js b/eslint-plugin/lib/rules/avoid-brightness-override.js index b54e82f..e2c881d 100644 --- a/eslint-plugin/lib/rules/avoid-brightness-override.js +++ b/eslint-plugin/lib/rules/avoid-brightness-override.js @@ -19,50 +19,58 @@ "use strict"; const brightnessLibrariesMethods = { - 'expo-brightness': ['setBrightnessAsync', 'setSystemBrightnessAsync', 'setSystemBrightnessAsync'], - 'react-native-device-brightness': ['setBrightnessLevel'], - 'react-native-screen-brightness': ['setBrightness'], - '@capacitor-community/screen-brightness': ['setBrightness'] + "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: [], + meta: { + type: "suggestion", + docs: { + description: "Should avoid to override brightness", + category: "eco-design", + recommended: "warn", }, - create: function (context) { - const librariesFoundInImports = []; + 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; + return { + ImportDeclaration(node) { + const currentLibrary = node.source.value; - if (brightnessLibrariesMethods[currentLibrary]) { - librariesFoundInImports.push(currentLibrary); - } - }, - MemberExpression(node) { - if (librariesFoundInImports.length === 0) { - return; - } + 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", - }); - } - }, - }; - }, + if ( + librariesFoundInImports.some((library) => + brightnessLibrariesMethods[library].includes(node.property.name), + ) + ) { + context.report({ + node, + messageId: "ShouldAvoidOverrideBrightness", + }); + } + }, + }; + }, }; diff --git a/eslint-plugin/tests/lib/rules/avoid-brightness-override.js b/eslint-plugin/tests/lib/rules/avoid-brightness-override.js index 3e0e218..eb6cfa5 100644 --- a/eslint-plugin/tests/lib/rules/avoid-brightness-override.js +++ b/eslint-plugin/tests/lib/rules/avoid-brightness-override.js @@ -30,22 +30,22 @@ const RuleTester = require("eslint").RuleTester; //------------------------------------------------------------------------------ const ruleTester = new RuleTester({ - parserOptions: { - ecmaVersion: 2021, - sourceType: "module", - ecmaFeatures: { - jsx: true - } + parserOptions: { + ecmaVersion: 2021, + sourceType: "module", + ecmaFeatures: { + jsx: true, }, + }, }); const expectedError = { - messageId: "ShouldAvoidOverrideBrightness", - type: "MemberExpression", + messageId: "ShouldAvoidOverrideBrightness", + type: "MemberExpression", }; ruleTester.run("avoid-brightness-override", rule, { - valid: [ - ` + valid: [ + ` import * as lodash from 'lodash'; lodash.isEmpty(''); `, @@ -55,7 +55,7 @@ ruleTester.run("avoid-brightness-override", rule, { // Get the current brightness: const {brightness: currentBrightness} = ScreenBrightness.getBrightness(); `, - ` + ` import DeviceBrightness from 'react-native-device-brightness'; DeviceBrightness.getBrightnessLevel() @@ -65,49 +65,49 @@ ruleTester.run("avoid-brightness-override", rule, { 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: ` + invalid: [ + { + code: ` import { ScreenBrightness } from '@capacitor-community/screen-brightness'; // Set the brightness: const brightness = 0.5; ScreenBrightness.setBrightness({ brightness }); `, - errors: [expectedError] - }, - { - code: ` + errors: [expectedError], + }, + { + code: ` import DeviceBrightness from 'react-native-device-brightness'; DeviceBrightness.setBrightnessLevel(0.5); `, - errors: [expectedError] - }, - { - code: ` + errors: [expectedError], + }, + { + code: ` import ScreenBrightness from 'react-native-screen-brightness'; ScreenBrightness.setBrightness(0.5); `, - errors: [expectedError] - }, - { - code: ` + errors: [expectedError], + }, + { + code: ` import React, { useEffect } from 'react'; import { StyleSheet, View, Text } from 'react-native'; import * as Brightness from 'expo-brightness'; @@ -129,7 +129,7 @@ ruleTester.run("avoid-brightness-override", rule, { ); } `, - errors: [expectedError] - } - ], + errors: [expectedError], + }, + ], }); From e3185e845f4b54f73343cc9d308a5784ad080b6b Mon Sep 17 00:00:00 2001 From: Ju_Baas Date: Tue, 4 Jun 2024 10:51:21 +0200 Subject: [PATCH 11/13] add rule class to the check list --- .../src/main/java/io/ecocode/javascript/CheckList.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java b/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java index 6b79f22..e7adca8 100644 --- a/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java +++ b/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java @@ -43,7 +43,8 @@ public static List> getAllChecks() { NoMultipleStyleChanges.class, PreferCollectionsWithPagination.class, PreferShorthandCSSNotations.class, - ProvidePrintCSS.class + ProvidePrintCSS.class, + AvoirBrightnessOverride.class ); } From 7208bcf1e094c0fa4e987ca024ebd14d3089d40e Mon Sep 17 00:00:00 2001 From: Ju_Baas Date: Tue, 4 Jun 2024 10:57:28 +0200 Subject: [PATCH 12/13] fix typo --- sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java b/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java index e7adca8..6b62a49 100644 --- a/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java +++ b/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java @@ -44,7 +44,7 @@ public static List> getAllChecks() { PreferCollectionsWithPagination.class, PreferShorthandCSSNotations.class, ProvidePrintCSS.class, - AvoirBrightnessOverride.class + AvoidBrightnessOverride.class ); } From dc9414c6c91456f0d864bacf0cc4d2a51f101b32 Mon Sep 17 00:00:00 2001 From: Maxime Malgorn <9255967+utarwyn@users.noreply.github.com> Date: Wed, 5 Jun 2024 11:09:59 +0000 Subject: [PATCH 13/13] Few changes --- CHANGELOG.md | 2 +- .../src/main/java/io/ecocode/javascript/CheckList.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8669cf..cc2110b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added -- [#39](https://github.com/green-code-initiative/ecoCode-javascript/issues/39) EC522: add brightness override rules for mobile app framework over js (capacitor, react native...) +- [#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 diff --git a/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java b/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java index 6b62a49..5230d8b 100644 --- a/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java +++ b/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java @@ -34,6 +34,7 @@ private CheckList() { public static List> getAllChecks() { return Arrays.asList( + AvoidBrightnessOverride.class, AvoidCSSAnimations.class, AvoidHighAccuracyGeolocation.class, LimitDbQueryResult.class, @@ -43,8 +44,7 @@ public static List> getAllChecks() { NoMultipleStyleChanges.class, PreferCollectionsWithPagination.class, PreferShorthandCSSNotations.class, - ProvidePrintCSS.class, - AvoidBrightnessOverride.class + ProvidePrintCSS.class ); }