From 826d23162435dbcaed65f27b7ee608c40490fbeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20KERISIT?= Date: Wed, 29 May 2024 17:26:50 +0200 Subject: [PATCH 1/7] Add Void Keep a wake --- eslint-plugin/docs/rules/avoid-keep-awake.md | 15 ++++ eslint-plugin/lib/rules/avoid-keep-awake.js | 47 ++++++++++ .../tests/lib/rules/avoid-keep-awake.js | 85 +++++++++++++++++++ .../java/io/ecocode/javascript/CheckList.java | 1 + .../javascript/checks/AvoidKeepAwake.java | 37 ++++++++ .../profiles/ecocode_javascript_profile.json | 3 +- 6 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 eslint-plugin/docs/rules/avoid-keep-awake.md create mode 100644 eslint-plugin/lib/rules/avoid-keep-awake.js create mode 100644 eslint-plugin/tests/lib/rules/avoid-keep-awake.js create mode 100644 sonar-plugin/src/main/java/io/ecocode/javascript/checks/AvoidKeepAwake.java diff --git a/eslint-plugin/docs/rules/avoid-keep-awake.md b/eslint-plugin/docs/rules/avoid-keep-awake.md new file mode 100644 index 0000000..b85e9bc --- /dev/null +++ b/eslint-plugin/docs/rules/avoid-keep-awake.md @@ -0,0 +1,15 @@ +# Avoid using keepAwake in web applications (`@ecocode/avoid-keep-awake`) + +⚠️ This rule _warns_ in the ✅ `recommended` config. + + + +## Why is this an issue? + +To avoid draining the battery, an Android device that is left idle quickly falls asleep. +Hence, keeping the screen on should be avoided, unless it is absolutely necessary. If so, developers typically use the FLAG_KEEP_SCREEN_ON in their activity. Another way to implement this is in their application's layout XML file, by using the android:keepScreenOn attribute. + +## Resources + +### Documentation + diff --git a/eslint-plugin/lib/rules/avoid-keep-awake.js b/eslint-plugin/lib/rules/avoid-keep-awake.js new file mode 100644 index 0000000..43b261b --- /dev/null +++ b/eslint-plugin/lib/rules/avoid-keep-awake.js @@ -0,0 +1,47 @@ +/* + * 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: "Avoid screen keep awake", + category: "eco-design", + recommended: "warn", + }, + messages: { + AvoidKeepAwake: "Avoid screen keep awake", + }, + schema: [], + }, + create: function (context) { + return { + Identifier(node){ + if ( + node?.name === "useKeepAwake" + && node?.parent.type === "CallExpression" + ) { + context.report({ node, messageId: "AvoidKeepAwake" }); + } + }, + }; + }, +}; diff --git a/eslint-plugin/tests/lib/rules/avoid-keep-awake.js b/eslint-plugin/tests/lib/rules/avoid-keep-awake.js new file mode 100644 index 0000000..84546fa --- /dev/null +++ b/eslint-plugin/tests/lib/rules/avoid-keep-awake.js @@ -0,0 +1,85 @@ +/* + * 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-keep-awake"); +const RuleTester = require("eslint").RuleTester; + +//------------------------------------------------------------------------------ +// Tests +//------------------------------------------------------------------------------ + +const ruleTester = new RuleTester({ + parserOptions: { + ecmaVersion: 2015, //es6 + sourceType: "module", + ecmaFeatures: { + jsx: true, + }, + }, +}); +const expectedError = { + messageId: "AvoidKeepAwake", + type: "Identifier", +}; + +ruleTester.run("avoid-keep-awake", rule, { + valid: [ + { + code: ` + import React from 'react'; + import { Text, View } from 'react-native'; + + export default function ValidExample() { + return ( + + This screen will sleep! + + ); + } + `, + errors: [expectedError], + }, + , + ], + invalid: [ + { + code: ` + import { useKeepAwake } from 'expo-keep-awake'; + import React from 'react'; + import { Text, View } from 'react-native'; + + export default function KeepAwakeExample() { + useKeepAwake(); + return ( + + This screen will never sleep! + + ); + } + `, + errors: [expectedError], + }, + , + ], +}); 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..c8bf6af 100644 --- a/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java +++ b/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java @@ -36,6 +36,7 @@ public static List> getAllChecks() { return Arrays.asList( AvoidCSSAnimations.class, AvoidHighAccuracyGeolocation.class, + AvoidKeepAwake.class, LimitDbQueryResult.class, NoEmptyImageSrcAttribute.class, NoImportAllFromLibrary.class, diff --git a/sonar-plugin/src/main/java/io/ecocode/javascript/checks/AvoidKeepAwake.java b/sonar-plugin/src/main/java/io/ecocode/javascript/checks/AvoidKeepAwake.java new file mode 100644 index 0000000..7c42506 --- /dev/null +++ b/sonar-plugin/src/main/java/io/ecocode/javascript/checks/AvoidKeepAwake.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 = AvoidKeepAwake.RULE_KEY) +public class AvoidKeepAwake implements EslintBasedCheck { + + public static final String RULE_KEY = "EC505"; + + @Override + public String eslintKey() { + return "@ecocode/avoid-keep-awake"; + } + +} diff --git a/sonar-plugin/src/main/resources/io/ecocode/profiles/ecocode_javascript_profile.json b/sonar-plugin/src/main/resources/io/ecocode/profiles/ecocode_javascript_profile.json index 2e3da99..cc57ac0 100644 --- a/sonar-plugin/src/main/resources/io/ecocode/profiles/ecocode_javascript_profile.json +++ b/sonar-plugin/src/main/resources/io/ecocode/profiles/ecocode_javascript_profile.json @@ -9,6 +9,7 @@ "EC25", "EC26", "EC29", - "EC30" + "EC30", + "EC505" ] } From c62646fd52b79e8772a925dbfcf840ebf2113926 Mon Sep 17 00:00:00 2001 From: "DESKTOP-J99B7TH\\Gilbert" Date: Thu, 30 May 2024 12:28:27 +0200 Subject: [PATCH 2/7] Add test when keep awake is used as function --- eslint-plugin/lib/rules/avoid-keep-awake.js | 6 +++ .../tests/lib/rules/avoid-keep-awake.js | 53 +++++++++++++++++-- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/eslint-plugin/lib/rules/avoid-keep-awake.js b/eslint-plugin/lib/rules/avoid-keep-awake.js index 43b261b..bdc2f2c 100644 --- a/eslint-plugin/lib/rules/avoid-keep-awake.js +++ b/eslint-plugin/lib/rules/avoid-keep-awake.js @@ -41,6 +41,12 @@ module.exports = { ) { context.report({ node, messageId: "AvoidKeepAwake" }); } + else if ( + node?.name === "activateKeepAwake" + && node?.parent.type === "CallExpression" + ) { + context.report({ node, messageId: "AvoidKeepAwake" }); + } }, }; }, diff --git a/eslint-plugin/tests/lib/rules/avoid-keep-awake.js b/eslint-plugin/tests/lib/rules/avoid-keep-awake.js index 84546fa..c6534a7 100644 --- a/eslint-plugin/tests/lib/rules/avoid-keep-awake.js +++ b/eslint-plugin/tests/lib/rules/avoid-keep-awake.js @@ -31,14 +31,20 @@ const RuleTester = require("eslint").RuleTester; const ruleTester = new RuleTester({ parserOptions: { - ecmaVersion: 2015, //es6 + ecmaVersion: 2022, sourceType: "module", ecmaFeatures: { jsx: true, }, }, }); -const expectedError = { + +const expectedErrorHook = { + messageId: "AvoidKeepAwake", + type: "Identifier", +}; + +const expectedErrorFunction = { messageId: "AvoidKeepAwake", type: "Identifier", }; @@ -58,7 +64,23 @@ ruleTester.run("avoid-keep-awake", rule, { ); } `, - errors: [expectedError], + errors: [expectedErrorHook], + }, + { + code: ` + import React from 'react'; + import { Button, View } from 'react-native'; + + export default class ValidExample extends React.Component { + render() { + return ( + + + ); + } + } + `, + errors: [expectedErrorFunction], }, , ], @@ -78,8 +100,29 @@ ruleTester.run("avoid-keep-awake", rule, { ); } `, - errors: [expectedError], + errors: [expectedErrorHook], + }, + { + code: ` + import { activateKeepAwake } from 'expo-keep-awake'; + import React from 'react'; + import { Button, View } from 'react-native'; + + export default class KeepAwakeExample extends React.Component { + render() { + return ( + +