Skip to content

Commit bfc1a85

Browse files
committed
Apply PR feedbacks
1 parent 0e7649a commit bfc1a85

File tree

6 files changed

+49
-25
lines changed

6 files changed

+49
-25
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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ Add `@creedengo` to the `plugins` section of your `.eslintrc`, followed by rules
6161

6262
<!-- begin auto-generated configs list -->
6363

64-
| | Name |
65-
| :-- | :------------ |
64+
| | Name |
65+
| :- | :------------ |
6666
|| `recommended` |
6767

6868
<!-- end auto-generated configs list -->
@@ -74,12 +74,13 @@ Add `@creedengo` to the `plugins` section of your `.eslintrc`, followed by rules
7474
⚠️ Configurations set to warn in.\
7575
✅ Set in the `recommended` configuration.
7676

77-
| Name | Description | ⚠️ |
78-
| :--------------------------------------------------------------------------------------------- | :-------------------------------------------------------- | :-- |
77+
| Name | Description | ⚠️ |
78+
| :--------------------------------------------------------------------------------------------- | :-------------------------------------------------------- | :- |
7979
| [avoid-autoplay](docs/rules/avoid-autoplay.md) | Avoid autoplay for videos and audio content ||
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: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Avoid screen keep awake (`@ecocode/avoid-keep-awake`)
1+
# Avoid screen keep awake (`@creedengo/avoid-keep-awake`)
22

33
⚠️ This rule _warns_ in the ✅ `recommended` config.
44

@@ -9,25 +9,32 @@
99
To avoid draining the battery, an Android device that is left idle quickly falls asleep.
1010
Hence, keeping the screen on should be avoided, unless it is absolutely necessary.
1111

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+
1214
```js
15+
import { useKeepAwake } from "expo-keep-awake";
16+
1317
export default function KeepAwakeExample() {
1418
useKeepAwake(); // Non compliant
1519
return (
16-
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
20+
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
1721
<Text>This screen will never sleep!</Text>
1822
</View>
1923
);
2024
}
2125
```
2226

2327
```js
28+
import { activateKeepAwake } from "expo-keep-awake";
29+
2430
_activate = () => {
25-
activateKeepAwake(); // Non-compliant
26-
alert('Activated!');
27-
};
31+
activateKeepAwake(); // Non-compliant
32+
alert("Activated!");
33+
};
2834
```
2935

3036
## Resources
3137

3238
### Documentation
3339

40+
- [Expo Docs](https://docs.expo.dev/versions/latest/sdk/keep-awake/) - Expo KeepAwake

eslint-plugin/lib/rules/avoid-keep-awake.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
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)
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)
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -18,6 +18,10 @@
1818

1919
"use strict";
2020

21+
const keepAwakeLibrariesMethods = {
22+
"expo-keep-awake": ["activateKeepAwake", "useKeepAwake"],
23+
};
24+
2125
/** @type {import("eslint").Rule.RuleModule} */
2226
module.exports = {
2327
meta: {
@@ -33,16 +37,25 @@ module.exports = {
3337
schema: [],
3438
},
3539
create: function (context) {
40+
const librariesFoundInImports = [];
41+
3642
return {
37-
Identifier(node) {
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+
3855
if (
39-
node?.name === "useKeepAwake" &&
40-
node?.parent.type === "CallExpression"
41-
) {
42-
context.report({ node, messageId: "AvoidKeepAwake" });
43-
} else if (
44-
node?.name === "activateKeepAwake" &&
45-
node?.parent.type === "CallExpression"
56+
librariesFoundInImports.some((library) =>
57+
keepAwakeLibrariesMethods[library].includes(node.callee.name),
58+
)
4659
) {
4760
context.report({ node, messageId: "AvoidKeepAwake" });
4861
}

eslint-plugin/tests/lib/rules/avoid-keep-awake.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
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)
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)
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by
@@ -41,12 +41,12 @@ const ruleTester = new RuleTester({
4141

4242
const expectedErrorHook = {
4343
messageId: "AvoidKeepAwake",
44-
type: "Identifier",
44+
type: "CallExpression",
4545
};
4646

4747
const expectedErrorFunction = {
4848
messageId: "AvoidKeepAwake",
49-
type: "Identifier",
49+
type: "CallExpression",
5050
};
5151

5252
ruleTester.run("avoid-keep-awake", rule, {
@@ -68,10 +68,12 @@ ruleTester.run("avoid-keep-awake", rule, {
6868
{
6969
code: `
7070
import React from 'react';
71+
import { useKeepAwake } from 'other-library';
7172
import { Button, View } from 'react-native';
7273
7374
export default class ValidExample extends React.Component {
7475
render() {
76+
useKeepAwake();
7577
return (
7678
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
7779
</View>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
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)
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)
44
*
55
* This program is free software: you can redistribute it and/or modify
66
* it under the terms of the GNU General Public License as published by

0 commit comments

Comments
 (0)