Skip to content

Commit b6cf0dc

Browse files
FanchGadjoDESKTOP-J99B7TH\Gilbertutarwyn
authored
Add rule GCI505 "Avoid keep awake" (#45)
Co-authored-by: DESKTOP-J99B7TH\Gilbert <[email protected]> Co-authored-by: utarwyn <[email protected]>
1 parent 3360ca3 commit b6cf0dc

File tree

9 files changed

+274
-1
lines changed

9 files changed

+274
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- [#40](https://github.com/green-code-initiative/creedengo-javascript/pull/40) Add rule `@creedengo/avoid-autoplay` (GCI36)
13+
- [#45](https://github.com/green-code-initiative/creedengo-javascript/pull/45) Add rule `@creedengo/avoid-keep-awake` (GCI505)
1314
- [#46](https://github.com/green-code-initiative/creedengo-javascript/pull/46) Add rule `@creedengo/prefer-lighter-formats-for-image-files` (GCI31)
1415

1516
## [2.0.0] - 2025-01-22

eslint-plugin/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ Add `@creedengo` to the `plugins` section of your `.eslintrc`, followed by rules
8080
| [avoid-brightness-override](docs/rules/avoid-brightness-override.md) | Should avoid to override brightness ||
8181
| [avoid-css-animations](docs/rules/avoid-css-animations.md) | Avoid usage of CSS animations ||
8282
| [avoid-high-accuracy-geolocation](docs/rules/avoid-high-accuracy-geolocation.md) | Avoid using high accuracy geolocation in web applications ||
83+
| [avoid-keep-awake](docs/rules/avoid-keep-awake.md) | Avoid screen keep awake ||
8384
| [limit-db-query-results](docs/rules/limit-db-query-results.md) | Should limit the number of returns for a SQL query ||
8485
| [no-empty-image-src-attribute](docs/rules/no-empty-image-src-attribute.md) | Disallow usage of image with empty source attribute ||
8586
| [no-import-all-from-library](docs/rules/no-import-all-from-library.md) | Should not import all from library ||
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Avoid screen keep awake (`@creedengo/avoid-keep-awake`)
2+
3+
⚠️ This rule _warns_ in the ✅ `recommended` config.
4+
5+
<!-- end auto-generated rule header -->
6+
7+
## Why is this an issue?
8+
9+
To avoid draining the battery, an Android device that is left idle quickly falls asleep.
10+
Hence, keeping the screen on should be avoided, unless it is absolutely necessary.
11+
12+
> **Note**: This rule currently only supports detecting `expo-keep-awake` package usage. Support for other keep-awake packages may be added in future versions.
13+
14+
```js
15+
import { useKeepAwake } from "expo-keep-awake";
16+
17+
export default function KeepAwakeExample() {
18+
useKeepAwake(); // Non compliant
19+
return (
20+
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
21+
<Text>This screen will never sleep!</Text>
22+
</View>
23+
);
24+
}
25+
```
26+
27+
```js
28+
import { activateKeepAwake } from "expo-keep-awake";
29+
30+
_activate = () => {
31+
activateKeepAwake(); // Non-compliant
32+
alert("Activated!");
33+
};
34+
```
35+
36+
## Resources
37+
38+
### Documentation
39+
40+
- [Expo Docs](https://docs.expo.dev/versions/latest/sdk/keep-awake/) - Expo KeepAwake
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* creedengo JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs
3+
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
"use strict";
20+
21+
const keepAwakeLibrariesMethods = {
22+
"expo-keep-awake": ["activateKeepAwake", "useKeepAwake"],
23+
};
24+
25+
/** @type {import("eslint").Rule.RuleModule} */
26+
module.exports = {
27+
meta: {
28+
type: "suggestion",
29+
docs: {
30+
description: "Avoid screen keep awake",
31+
category: "eco-design",
32+
recommended: "warn",
33+
},
34+
messages: {
35+
AvoidKeepAwake: "Avoid screen keep awake",
36+
},
37+
schema: [],
38+
},
39+
create: function (context) {
40+
const librariesFoundInImports = [];
41+
42+
return {
43+
ImportDeclaration(node) {
44+
const currentLibrary = node.source.value;
45+
46+
if (keepAwakeLibrariesMethods[currentLibrary]) {
47+
librariesFoundInImports.push(currentLibrary);
48+
}
49+
},
50+
CallExpression(node) {
51+
if (librariesFoundInImports.length === 0) {
52+
return;
53+
}
54+
55+
if (
56+
librariesFoundInImports.some((library) =>
57+
keepAwakeLibrariesMethods[library].includes(node.callee.name),
58+
)
59+
) {
60+
context.report({ node, messageId: "AvoidKeepAwake" });
61+
}
62+
},
63+
};
64+
},
65+
};
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* creedengo JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs
3+
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
"use strict";
20+
21+
//------------------------------------------------------------------------------
22+
// Requirements
23+
//------------------------------------------------------------------------------
24+
25+
const rule = require("../../../lib/rules/avoid-keep-awake");
26+
const RuleTester = require("eslint").RuleTester;
27+
28+
//------------------------------------------------------------------------------
29+
// Tests
30+
//------------------------------------------------------------------------------
31+
32+
const ruleTester = new RuleTester({
33+
parserOptions: {
34+
ecmaVersion: 2022,
35+
sourceType: "module",
36+
ecmaFeatures: {
37+
jsx: true,
38+
},
39+
},
40+
});
41+
42+
const expectedErrorHook = {
43+
messageId: "AvoidKeepAwake",
44+
type: "CallExpression",
45+
};
46+
47+
const expectedErrorFunction = {
48+
messageId: "AvoidKeepAwake",
49+
type: "CallExpression",
50+
};
51+
52+
ruleTester.run("avoid-keep-awake", rule, {
53+
valid: [
54+
{
55+
code: `
56+
import React from 'react';
57+
import { Text, View } from 'react-native';
58+
59+
export default function ValidExample() {
60+
return (
61+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
62+
<Text>This screen will sleep!</Text>
63+
</View>
64+
);
65+
}
66+
`,
67+
},
68+
{
69+
code: `
70+
import React from 'react';
71+
import { useKeepAwake } from 'other-library';
72+
import { Button, View } from 'react-native';
73+
74+
export default class ValidExample extends React.Component {
75+
render() {
76+
useKeepAwake();
77+
return (
78+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
79+
</View>
80+
);
81+
}
82+
}
83+
`,
84+
},
85+
],
86+
invalid: [
87+
{
88+
code: `
89+
import { useKeepAwake } from 'expo-keep-awake';
90+
import React from 'react';
91+
import { Text, View } from 'react-native';
92+
93+
export default function KeepAwakeExample() {
94+
useKeepAwake();
95+
return (
96+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
97+
<Text>This screen will never sleep!</Text>
98+
</View>
99+
);
100+
}
101+
`,
102+
errors: [expectedErrorHook],
103+
},
104+
{
105+
code: `
106+
import { activateKeepAwake } from 'expo-keep-awake';
107+
import React from 'react';
108+
import { Button, View } from 'react-native';
109+
110+
export default class KeepAwakeExample extends React.Component {
111+
render() {
112+
return (
113+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
114+
<Button onPress={this._activate} title="Activate" />
115+
</View>
116+
);
117+
}
118+
119+
_activate = () => {
120+
activateKeepAwake();
121+
alert('Activated!');
122+
};
123+
}`,
124+
errors: [expectedErrorFunction],
125+
},
126+
],
127+
});

sonar-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
<project.build.sourceEncoding>${encoding}</project.build.sourceEncoding>
5050
<project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding>
5151

52-
<version.creedengo-rules-specifications>2.2.1</version.creedengo-rules-specifications>
52+
<version.creedengo-rules-specifications>2.2.2</version.creedengo-rules-specifications>
5353
<version.sonarqube>9.14.0.375</version.sonarqube>
5454
<version.sonar-javascript>9.13.0.20537</version.sonar-javascript>
5555
<version.sonar-packaging>1.23.0.740</version.sonar-packaging>

sonar-plugin/src/main/java/org/greencodeinitiative/creedengo/javascript/CheckList.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
3838
AvoidBrightnessOverride.class,
3939
AvoidCSSAnimations.class,
4040
AvoidHighAccuracyGeolocation.class,
41+
AvoidKeepAwake.class,
4142
LimitDbQueryResult.class,
4243
NoEmptyImageSrcAttribute.class,
4344
NoImportAllFromLibrary.class,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Creedengo JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs
3+
* Copyright © 2023 Green Code Initiative (https://green-code-initiative.org)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.greencodeinitiative.creedengo.javascript.checks;
19+
20+
import org.sonar.check.Rule;
21+
import org.sonar.plugins.javascript.api.EslintBasedCheck;
22+
import org.sonar.plugins.javascript.api.JavaScriptRule;
23+
import org.sonar.plugins.javascript.api.TypeScriptRule;
24+
25+
@JavaScriptRule
26+
@TypeScriptRule
27+
@Rule(key = AvoidKeepAwake.RULE_KEY)
28+
public class AvoidKeepAwake implements EslintBasedCheck {
29+
30+
public static final String RULE_KEY = "GCI505";
31+
32+
@Override
33+
public String eslintKey() {
34+
return "@creedengo/avoid-keep-awake";
35+
}
36+
37+
}

sonar-plugin/src/main/resources/org/greencodeinitiative/creedengo/profiles/javascript_profile.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"GCI30",
1212
"GCI31",
1313
"GCI36",
14+
"GCI505",
1415
"GCI523",
1516
"GCI530"
1617
]

0 commit comments

Comments
 (0)