-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feature/no-torch
- Loading branch information
Showing
14 changed files
with
376 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Should avoid to override brightness (`@ecocode/avoid-brightness-override`) | ||
|
||
⚠️ This rule _warns_ in the ✅ `recommended` config. | ||
|
||
<!-- end auto-generated rule header --> | ||
|
||
## Why is this an issue? | ||
|
||
To avoid draining the battery, IOS and Android devices adapt the brightness of the screen depending on the environment light. | ||
|
||
For some reasons, developers may disable this feature programmatically. | ||
|
||
This feature was introduced to improve battery life, be careful when deactivating it. | ||
|
||
Hence, keeping forcing the screen brightness on should be avoided, unless it is absolutely necessary. | ||
|
||
|
||
## Example of non compliant code | ||
|
||
```js | ||
// Example with expo-brightness (Expo framework library) | ||
import React, { useEffect } from 'react'; | ||
import { View, Text } from 'react-native'; | ||
import * as Brightness from 'expo-brightness'; | ||
|
||
export default function App() { | ||
useEffect(() => { | ||
(async () => { Brightness.setSystemBrightnessAsyn(1); })(); // Brightness is forced here | ||
}, []); | ||
return ( | ||
<View> | ||
<Text>Brightness Module Example</Text> | ||
</View> | ||
); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* 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"; | ||
|
||
const brightnessLibrariesMethods = { | ||
"expo-brightness": [ | ||
"setBrightnessAsync", | ||
"setSystemBrightnessAsync", | ||
"setSystemBrightnessAsync", | ||
], | ||
"react-native-device-brightness": ["setBrightnessLevel"], | ||
"react-native-screen-brightness": ["setBrightness"], | ||
"@capacitor-community/screen-brightness": ["setBrightness"], | ||
}; | ||
|
||
/** @type {import("eslint").Rule.RuleModule} */ | ||
module.exports = { | ||
meta: { | ||
type: "suggestion", | ||
docs: { | ||
description: "Should avoid to override brightness", | ||
category: "eco-design", | ||
recommended: "warn", | ||
}, | ||
messages: { | ||
ShouldAvoidOverrideBrightness: | ||
"Do not force Brightness in your code, unless absolutely necessary", | ||
}, | ||
schema: [], | ||
}, | ||
create: function (context) { | ||
const librariesFoundInImports = []; | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
const currentLibrary = node.source.value; | ||
|
||
if (brightnessLibrariesMethods[currentLibrary]) { | ||
librariesFoundInImports.push(currentLibrary); | ||
} | ||
}, | ||
MemberExpression(node) { | ||
if (librariesFoundInImports.length === 0) { | ||
return; | ||
} | ||
|
||
if ( | ||
librariesFoundInImports.some((library) => | ||
brightnessLibrariesMethods[library].includes(node.property.name), | ||
) | ||
) { | ||
context.report({ | ||
node, | ||
messageId: "ShouldAvoidOverrideBrightness", | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
eslint-plugin/tests/lib/rules/avoid-brightness-override.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
* 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-brightness-override"); | ||
const RuleTester = require("eslint").RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2021, | ||
sourceType: "module", | ||
ecmaFeatures: { | ||
jsx: true, | ||
}, | ||
}, | ||
}); | ||
const expectedError = { | ||
messageId: "ShouldAvoidOverrideBrightness", | ||
type: "MemberExpression", | ||
}; | ||
|
||
ruleTester.run("avoid-brightness-override", rule, { | ||
valid: [ | ||
` | ||
import * as lodash from 'lodash'; | ||
lodash.isEmpty(''); | ||
`, | ||
` | ||
import { ScreenBrightness } from '@capacitor-community/screen-brightness'; | ||
// Get the current brightness: | ||
const {brightness: currentBrightness} = ScreenBrightness.getBrightness(); | ||
`, | ||
` | ||
import DeviceBrightness from 'react-native-device-brightness'; | ||
DeviceBrightness.getBrightnessLevel() | ||
.then(function (luminous) { | ||
// Get current brightness level | ||
// 0 ~ 1 | ||
console.log(luminous); | ||
}); | ||
`, | ||
` | ||
import * as Brightness from 'expo-brightness'; | ||
Brightness.requestPermissionsAsync(); | ||
`, | ||
` | ||
import ScreenBrightness from 'react-native-screen-brightness'; | ||
ScreenBrightness.getBrightness().then(brightness => { | ||
console.log('brightness', brightness); | ||
}); | ||
`, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: ` | ||
import { ScreenBrightness } from '@capacitor-community/screen-brightness'; | ||
// Set the brightness: | ||
const brightness = 0.5; | ||
ScreenBrightness.setBrightness({ brightness }); | ||
`, | ||
errors: [expectedError], | ||
}, | ||
{ | ||
code: ` | ||
import DeviceBrightness from 'react-native-device-brightness'; | ||
DeviceBrightness.setBrightnessLevel(0.5); | ||
`, | ||
errors: [expectedError], | ||
}, | ||
{ | ||
code: ` | ||
import ScreenBrightness from 'react-native-screen-brightness'; | ||
ScreenBrightness.setBrightness(0.5); | ||
`, | ||
errors: [expectedError], | ||
}, | ||
{ | ||
code: ` | ||
import React, { useEffect } from 'react'; | ||
import { StyleSheet, View, Text } from 'react-native'; | ||
import * as Brightness from 'expo-brightness'; | ||
export default function App() { | ||
useEffect(() => { | ||
(async () => { | ||
const { status } = await Brightness.requestPermissionsAsync(); | ||
if (status === 'granted') { | ||
Brightness.setSystemBrightnessAsync(1); | ||
} | ||
})(); | ||
}, []); | ||
return ( | ||
<View style={styles.container}> | ||
<Text>Brightness Module Example</Text> | ||
</View> | ||
); | ||
} | ||
`, | ||
errors: [expectedError], | ||
}, | ||
], | ||
}); |
Oops, something went wrong.