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

✨ Implement avoid-high-accuracy-geolocation rule for React Native #44

Merged
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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
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"
]
}
Loading