-
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.
- Loading branch information
Showing
6 changed files
with
171 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Should not programmatically enable torch mode (`@ecocode/no-torch`) | ||
|
||
⚠️ This rule _warns_ in the ✅ `recommended` config. | ||
|
||
<!-- end auto-generated rule header --> | ||
## Why is this an issue? | ||
|
||
As a developer, you should avoid programmatically enabling torch mode. | ||
|
||
The flashlight can significantly drain the device's battery. If it is turned on without the user's knowledge, it could lead to unwanted battery consumption. | ||
|
||
```js | ||
import Torch from 'react-native-torch'; // Not-compliant | ||
``` | ||
|
||
```js | ||
import axios from 'axios'; // Compliant | ||
``` | ||
|
||
## Resources | ||
|
||
### Documentation | ||
|
||
- [CNUMR best practices mobile](https://github.com/cnumr/best-practices-mobile) - Torch free |
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,51 @@ | ||
/* | ||
* 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: "Should not programmatically enable torch mode", | ||
category: "eco-design", | ||
recommended: "warn", | ||
}, | ||
messages: { | ||
ShouldNotProgrammaticallyEnablingTorchMode: | ||
"You should not programmatically enable torch mode", | ||
}, | ||
schema: [], | ||
}, | ||
create: function (context) { | ||
const reactNativeTorchLibrary = "react-native-torch"; | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
const currentLibrary = node.source.value; | ||
if (currentLibrary === reactNativeTorchLibrary) { | ||
context.report({ | ||
node, | ||
messageId: "ShouldNotProgrammaticallyEnablingTorchMode", | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
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,56 @@ | ||
/* | ||
* 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/no-torch"); | ||
const RuleTester = require("eslint").RuleTester; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 6, | ||
sourceType: "module", | ||
}, | ||
}); | ||
const expectedError = { | ||
messageId: "ShouldNotProgrammaticallyEnablingTorchMode", | ||
type: "ImportDeclaration", | ||
}; | ||
|
||
ruleTester.run("no-torch", rule, { | ||
valid: [ | ||
` | ||
import axios from 'axios'; | ||
`, | ||
], | ||
|
||
invalid: [ | ||
{ | ||
code: "import Torch from 'react-native-torch';", | ||
errors: [expectedError], | ||
}, | ||
], | ||
}); |
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
37 changes: 37 additions & 0 deletions
37
sonar-plugin/src/main/java/io/ecocode/javascript/checks/NoTorch.java
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,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 = NoTorch.RULE_KEY) | ||
public class NoTorch implements EslintBasedCheck { | ||
|
||
public static final String RULE_KEY = "EC530"; | ||
|
||
@Override | ||
public String eslintKey() { | ||
return "@ecocode/no-torch"; | ||
} | ||
|
||
} |