-
Notifications
You must be signed in to change notification settings - Fork 17
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
EC505 avoid keep awake (react-native hook and function) #45
base: main
Are you sure you want to change the base?
Changes from all commits
826d231
c62646f
d8f493b
ca36959
ceacfd3
cfec8cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Avoid screen keep awake (`@ecocode/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. | ||
|
||
```js | ||
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 | ||
_activate = () => { | ||
activateKeepAwake(); // Non-compliant | ||
alert('Activated!'); | ||
}; | ||
``` | ||
|
||
## Resources | ||
|
||
### Documentation | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
"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" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you could check that this method comes from |
||
) { | ||
context.report({ node, messageId: "AvoidKeepAwake" }); | ||
} else if ( | ||
node?.name === "activateKeepAwake" && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment for this method |
||
node?.parent.type === "CallExpression" | ||
) { | ||
context.report({ node, messageId: "AvoidKeepAwake" }); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* 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 <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: "Identifier", | ||
}; | ||
|
||
const expectedErrorFunction = { | ||
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 ( | ||
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> | ||
<Text>This screen will sleep!</Text> | ||
</View> | ||
); | ||
} | ||
`, | ||
}, | ||
{ | ||
code: ` | ||
import React from 'react'; | ||
import { Button, View } from 'react-native'; | ||
|
||
export default class ValidExample extends React.Component { | ||
render() { | ||
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], | ||
}, | ||
], | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <http://www.gnu.org/licenses/>. | ||
*/ | ||
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"; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
"EC25", | ||
"EC26", | ||
"EC29", | ||
"EC30" | ||
"EC30", | ||
"EC505" | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that you should also include the
expo-keep-awake
dependency in exemple codes