Skip to content

Commit 826d231

Browse files
committed
Add Void Keep a wake
1 parent b80b84e commit 826d231

File tree

6 files changed

+187
-1
lines changed

6 files changed

+187
-1
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Avoid using keepAwake in web applications (`@ecocode/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. 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.
11+
12+
## Resources
13+
14+
### Documentation
15+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* ecoCode JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs
3+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
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+
/** @type {import("eslint").Rule.RuleModule} */
22+
module.exports = {
23+
meta: {
24+
type: "suggestion",
25+
docs: {
26+
description: "Avoid screen keep awake",
27+
category: "eco-design",
28+
recommended: "warn",
29+
},
30+
messages: {
31+
AvoidKeepAwake: "Avoid screen keep awake",
32+
},
33+
schema: [],
34+
},
35+
create: function (context) {
36+
return {
37+
Identifier(node){
38+
if (
39+
node?.name === "useKeepAwake"
40+
&& node?.parent.type === "CallExpression"
41+
) {
42+
context.report({ node, messageId: "AvoidKeepAwake" });
43+
}
44+
},
45+
};
46+
},
47+
};
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* ecoCode JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs
3+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
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: 2015, //es6
35+
sourceType: "module",
36+
ecmaFeatures: {
37+
jsx: true,
38+
},
39+
},
40+
});
41+
const expectedError = {
42+
messageId: "AvoidKeepAwake",
43+
type: "Identifier",
44+
};
45+
46+
ruleTester.run("avoid-keep-awake", rule, {
47+
valid: [
48+
{
49+
code: `
50+
import React from 'react';
51+
import { Text, View } from 'react-native';
52+
53+
export default function ValidExample() {
54+
return (
55+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
56+
<Text>This screen will sleep!</Text>
57+
</View>
58+
);
59+
}
60+
`,
61+
errors: [expectedError],
62+
},
63+
,
64+
],
65+
invalid: [
66+
{
67+
code: `
68+
import { useKeepAwake } from 'expo-keep-awake';
69+
import React from 'react';
70+
import { Text, View } from 'react-native';
71+
72+
export default function KeepAwakeExample() {
73+
useKeepAwake();
74+
return (
75+
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
76+
<Text>This screen will never sleep!</Text>
77+
</View>
78+
);
79+
}
80+
`,
81+
errors: [expectedError],
82+
},
83+
,
84+
],
85+
});

sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
3636
return Arrays.asList(
3737
AvoidCSSAnimations.class,
3838
AvoidHighAccuracyGeolocation.class,
39+
AvoidKeepAwake.class,
3940
LimitDbQueryResult.class,
4041
NoEmptyImageSrcAttribute.class,
4142
NoImportAllFromLibrary.class,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* ecoCode JavaScript plugin - Provides rules to reduce the environmental footprint of your JavaScript programs
3+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
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 io.ecocode.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 = "EC505";
31+
32+
@Override
33+
public String eslintKey() {
34+
return "@ecocode/avoid-keep-awake";
35+
}
36+
37+
}

sonar-plugin/src/main/resources/io/ecocode/profiles/ecocode_javascript_profile.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"EC25",
1010
"EC26",
1111
"EC29",
12-
"EC30"
12+
"EC30",
13+
"EC505"
1314
]
1415
}

0 commit comments

Comments
 (0)