Skip to content
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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Add `@ecocode` to the `plugins` section of your `.eslintrc`, followed by rules c
| :------------------------------------------------------------------------------------- | :--------------------------------------------------------- | :- |
| [avoid-css-animations](docs/rules/avoid-css-animations.md) | Avoid usage of CSS animations | ✅ |
| [avoid-high-accuracy-geolocation](docs/rules/avoid-high-accuracy-geolocation.md) | Avoid using high accuracy geolocation in web applications. | ✅ |
| [avoid-keep-awake](docs/rules/avoid-keep-awake.md) | Avoid screen keep awake | ✅ |
| [limit-db-query-results](docs/rules/limit-db-query-results.md) | Should limit the number of returns for a SQL query | ✅ |
| [no-empty-image-src-attribute](docs/rules/no-empty-image-src-attribute.md) | Disallow usage of image with empty source attribute | ✅ |
| [no-import-all-from-library](docs/rules/no-import-all-from-library.md) | Should not import all from library | ✅ |
Expand Down
33 changes: 33 additions & 0 deletions eslint-plugin/docs/rules/avoid-keep-awake.md
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
Copy link
Member

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

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

52 changes: 52 additions & 0 deletions eslint-plugin/lib/rules/avoid-keep-awake.js
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could check that this method comes from expo-keep-awake? This would prevent false positives if a developer uses this name in this JavaScript code

) {
context.report({ node, messageId: "AvoidKeepAwake" });
} else if (
node?.name === "activateKeepAwake" &&
Copy link
Member

Choose a reason for hiding this comment

The 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" });
}
},
};
},
};
125 changes: 125 additions & 0 deletions eslint-plugin/tests/lib/rules/avoid-keep-awake.js
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
Expand Up @@ -36,6 +36,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
return Arrays.asList(
AvoidCSSAnimations.class,
AvoidHighAccuracyGeolocation.class,
AvoidKeepAwake.class,
LimitDbQueryResult.class,
NoEmptyImageSrcAttribute.class,
NoImportAllFromLibrary.class,
Expand Down
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
Expand Up @@ -9,6 +9,7 @@
"EC25",
"EC26",
"EC29",
"EC30"
"EC30",
"EC505"
]
}
Loading