-
Notifications
You must be signed in to change notification settings - Fork 28
EC505 avoid keep awake (react-native hook and function) #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
utarwyn
merged 8 commits into
green-code-initiative:main
from
JuBaas:EC505-avoid-keep-awake
Mar 13, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
826d231
Add Void Keep a wake
FanchGadjo c62646f
Add test when keep awake is used as function
d8f493b
Add non compliant examples in md
ca36959
fix: remove errors in valid code spec
FanchGadjo ceacfd3
docs: run update eslint-doc command
FanchGadjo cfec8cd
style: remove linting issue
FanchGadjo 0e7649a
Merge branch 'main' into EC505-avoid-keep-awake
utarwyn a6460d0
Apply PR feedbacks
utarwyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Avoid screen keep awake (`@creedengo/avoid-keep-awake`) | ||
|
|
||
| ⚠️ This rule _warns_ in the ✅ `recommended` config. | ||
|
|
||
| <!-- end auto-generated rule header --> | ||
|
|
||
| ## 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. | ||
|
|
||
| > **Note**: This rule currently only supports detecting `expo-keep-awake` package usage. Support for other keep-awake packages may be added in future versions. | ||
|
|
||
| ```js | ||
| import { useKeepAwake } from "expo-keep-awake"; | ||
|
|
||
| export default function KeepAwakeExample() { | ||
| useKeepAwake(); // Non compliant | ||
| return ( | ||
| <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}> | ||
| <Text>This screen will never sleep!</Text> | ||
| </View> | ||
| ); | ||
| } | ||
| ``` | ||
|
|
||
| ```js | ||
| import { activateKeepAwake } from "expo-keep-awake"; | ||
|
|
||
| _activate = () => { | ||
| activateKeepAwake(); // Non-compliant | ||
| alert("Activated!"); | ||
| }; | ||
| ``` | ||
|
|
||
| ## Resources | ||
|
|
||
| ### Documentation | ||
|
|
||
| - [Expo Docs](https://docs.expo.dev/versions/latest/sdk/keep-awake/) - Expo KeepAwake | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * creedengo JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs | ||
| * Copyright © 2023 Green Code Initiative (https://green-code-initiative.org) | ||
| * | ||
| * 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 keepAwakeLibrariesMethods = { | ||
| "expo-keep-awake": ["activateKeepAwake", "useKeepAwake"], | ||
| }; | ||
|
|
||
| /** @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) { | ||
| const librariesFoundInImports = []; | ||
|
|
||
| return { | ||
| ImportDeclaration(node) { | ||
| const currentLibrary = node.source.value; | ||
|
|
||
| if (keepAwakeLibrariesMethods[currentLibrary]) { | ||
| librariesFoundInImports.push(currentLibrary); | ||
| } | ||
| }, | ||
| CallExpression(node) { | ||
| if (librariesFoundInImports.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| librariesFoundInImports.some((library) => | ||
| keepAwakeLibrariesMethods[library].includes(node.callee.name), | ||
| ) | ||
| ) { | ||
| context.report({ node, messageId: "AvoidKeepAwake" }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * creedengo JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs | ||
| * Copyright © 2023 Green Code Initiative (https://green-code-initiative.org) | ||
| * | ||
| * 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-keep-awake"); | ||
| const RuleTester = require("eslint").RuleTester; | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // Tests | ||
| //------------------------------------------------------------------------------ | ||
|
|
||
| const ruleTester = new RuleTester({ | ||
| parserOptions: { | ||
| ecmaVersion: 2022, | ||
| sourceType: "module", | ||
| ecmaFeatures: { | ||
| jsx: true, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const expectedErrorHook = { | ||
| messageId: "AvoidKeepAwake", | ||
| type: "CallExpression", | ||
| }; | ||
|
|
||
| const expectedErrorFunction = { | ||
| messageId: "AvoidKeepAwake", | ||
| type: "CallExpression", | ||
| }; | ||
|
|
||
| ruleTester.run("avoid-keep-awake", rule, { | ||
| valid: [ | ||
| { | ||
| code: ` | ||
| import React from 'react'; | ||
| import { Text, View } from 'react-native'; | ||
|
|
||
| export default function ValidExample() { | ||
| return ( | ||
| <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> | ||
| <Text>This screen will sleep!</Text> | ||
| </View> | ||
| ); | ||
| } | ||
| `, | ||
| }, | ||
| { | ||
| code: ` | ||
| import React from 'react'; | ||
| import { useKeepAwake } from 'other-library'; | ||
| import { Button, View } from 'react-native'; | ||
|
|
||
| export default class ValidExample extends React.Component { | ||
| render() { | ||
| useKeepAwake(); | ||
| return ( | ||
| <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> | ||
| </View> | ||
| ); | ||
| } | ||
| } | ||
| `, | ||
| }, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: ` | ||
| import { useKeepAwake } from 'expo-keep-awake'; | ||
| import React from 'react'; | ||
| import { Text, View } from 'react-native'; | ||
|
|
||
| export default function KeepAwakeExample() { | ||
| useKeepAwake(); | ||
| return ( | ||
| <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> | ||
| <Text>This screen will never sleep!</Text> | ||
| </View> | ||
| ); | ||
| } | ||
| `, | ||
| 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 ( | ||
| <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> | ||
| <Button onPress={this._activate} title="Activate" /> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| _activate = () => { | ||
| activateKeepAwake(); | ||
| alert('Activated!'); | ||
| }; | ||
| }`, | ||
| errors: [expectedErrorFunction], | ||
| }, | ||
| ], | ||
| }); |
This file contains hidden or 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 hidden or 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
37 changes: 37 additions & 0 deletions
37
...gin/src/main/java/org/greencodeinitiative/creedengo/javascript/checks/AvoidKeepAwake.java
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Creedengo JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs | ||
| * Copyright © 2023 Green Code Initiative (https://green-code-initiative.org) | ||
| * | ||
| * 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/>. | ||
| */ | ||
| package org.greencodeinitiative.creedengo.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 = "GCI505"; | ||
|
|
||
| @Override | ||
| public String eslintKey() { | ||
| return "@creedengo/avoid-keep-awake"; | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ | |
| "GCI30", | ||
| "GCI31", | ||
| "GCI36", | ||
| "GCI505", | ||
| "GCI523", | ||
| "GCI530" | ||
| ] | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.