Skip to content

Commit

Permalink
Merge branch 'main' into EC522-avoid-brightness-override
Browse files Browse the repository at this point in the history
  • Loading branch information
utarwyn authored Jun 4, 2024
2 parents e3185e8 + fefa128 commit 27f3a9f
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- Update Docker Compose configuration file to V2
- [#44](https://github.com/green-code-initiative/ecoCode-javascript/pull/44) Implement the rule EC523 for React Native

### Deleted

- [#44](https://github.com/green-code-initiative/ecoCode-javascript/pull/44) Merge the rule EC8 with EC523

## [1.5.0] - 2024-03-13

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This project proposes rules for the following technologies:
- TypeScript
- NestJS
- React (JSX)
- React Native / Expo

🔧 ESLint plugin
----------------
Expand Down
16 changes: 16 additions & 0 deletions eslint-plugin/docs/rules/avoid-high-accuracy-geolocation.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ usage.
If the application or service does not critically require pinpoint accuracy, opting for a less accurate geolocation can
help minimize the strain on the device's CPU.

## Web
```js
var options = { enableHighAccuracy: true, timeout: 5000, maximumAge: 0 }; // Non-compliant
navigator.geolocation.getCurrentPosition(
Expand All @@ -40,6 +41,21 @@ navigator.geolocation.getCurrentPosition(
);
```

## React Native
In this example, we ask the user to turn on high accuracy location mode which enables network provider that uses Google Play services to improve location accuracy and location-based services:
```js
import * as Location from 'expo-location';

Location.enableNetworkProviderAsync(); // Non-compliant
```

Prefer to ask the user to turn on lower-accuracy geolocation to conserve resources:
```js
import * as Location from 'expo-location';

Location.requestPermissionsAsync(); // Compliant
```

## Resources

### Documentation
Expand Down
32 changes: 31 additions & 1 deletion eslint-plugin/lib/rules/avoid-high-accuracy-geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

"use strict";

const geolocationLibrariesMethods = {
"expo-location": ["enableNetworkProviderAsync"],
};

/** @type {import("eslint").Rule.RuleModule} */
module.exports = {
meta: {
Expand All @@ -33,15 +37,41 @@ module.exports = {
schema: [],
},
create: function (context) {
const librariesFoundInImports = [];

return {
ImportDeclaration(node) {
const currentLibrary = node.source.value;

if (geolocationLibrariesMethods[currentLibrary]) {
librariesFoundInImports.push(currentLibrary);
}
},
MemberExpression(node) {
if (librariesFoundInImports.length === 0) {
return;
}

if (
librariesFoundInImports.some((library) =>
geolocationLibrariesMethods[library].includes(node.property.name),
)
) {
reportAvoidUsingAccurateGeolocation(context, node);
}
},
Property(node) {
if (
node?.key.name === "enableHighAccuracy" &&
node?.value.value === true
) {
context.report({ node, messageId: "AvoidUsingAccurateGeolocation" });
reportAvoidUsingAccurateGeolocation(context, node);
}
},
};
},
};

function reportAvoidUsingAccurateGeolocation(context, node) {
context.report({ node, messageId: "AvoidUsingAccurateGeolocation" });
}
34 changes: 30 additions & 4 deletions eslint-plugin/tests/lib/rules/avoid-high-accuracy-geolocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,20 @@ const RuleTester = require("eslint").RuleTester;
// Tests
//------------------------------------------------------------------------------

const ruleTester = new RuleTester();
const expectedError = {
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 6,
sourceType: "module",
},
});
const expectedErrorOnProperty = {
messageId: "AvoidUsingAccurateGeolocation",
type: "Property",
};
const expectedErrorOnMemberExpression = {
messageId: "AvoidUsingAccurateGeolocation",
type: "MemberExpression",
};

ruleTester.run("avoid-high-accuracy-geolocation", rule, {
valid: [
Expand All @@ -60,6 +69,15 @@ ruleTester.run("avoid-high-accuracy-geolocation", rule, {
`
navigator.geolocation.getCurrentPosition(success, error, {enableHighAccuracy: false});
`,

`
import axios from 'axios';
`,
`
import * as Location from 'expo-location';
Location.requestPermissionsAsync();
`,
],

invalid: [
Expand All @@ -77,13 +95,21 @@ ruleTester.run("avoid-high-accuracy-geolocation", rule, {
navigator.geolocation.getCurrentPosition(success, error, options);
`,
errors: [expectedError],
errors: [expectedErrorOnProperty],
},
{
code: `
navigator.geolocation.getCurrentPosition(success, error, {enableHighAccuracy: true});
`,
errors: [expectedError],
errors: [expectedErrorOnProperty],
},
{
code: `
import * as Location from 'expo-location';
Location.enableNetworkProviderAsync();
`,
errors: [expectedErrorOnMemberExpression],
},
],
});
2 changes: 1 addition & 1 deletion sonar-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<project.build.sourceEncoding>${encoding}</project.build.sourceEncoding>
<project.reporting.outputEncoding>${encoding}</project.reporting.outputEncoding>

<version.ecocode-rules-specifications>1.5.0</version.ecocode-rules-specifications>
<version.ecocode-rules-specifications>1.6.0</version.ecocode-rules-specifications>
<version.sonarqube>9.4.0.54424</version.sonarqube>
<version.sonar-javascript>9.13.0.20537</version.sonar-javascript>
<version.sonar-packaging>1.23.0.740</version.sonar-packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
import org.sonar.plugins.javascript.api.EslintBasedCheck;
import org.sonar.plugins.javascript.api.JavaScriptRule;
import org.sonar.plugins.javascript.api.TypeScriptRule;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

@JavaScriptRule
@TypeScriptRule
@Rule(key = AvoidHighAccuracyGeolocation.RULE_KEY)
@DeprecatedRuleKey(repositoryKey = "ecocode-javascript", ruleKey = "EC8")
public class AvoidHighAccuracyGeolocation implements EslintBasedCheck {

public static final String RULE_KEY = "EC8";
public static final String RULE_KEY = "EC523";

@Override
public String eslintKey() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "ecoCode",
"ruleKeys": [
"EC8",
"EC9",
"EC11",
"EC12",
"EC24",
"EC25",
"EC26",
"EC29",
"EC30"
"EC30",
"EC523"
]
}

0 comments on commit 27f3a9f

Please sign in to comment.