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

add no torch rule #41

Merged
merged 3 commits into from
Jun 7, 2024
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- [#39](https://github.com/green-code-initiative/ecoCode-javascript/issues/39) Add rule `@ecocode/avoid-brightness-override` (EC522)
- [#41](https://github.com/green-code-initiative/ecoCode-javascript/pull/41) Add rule `@ecocode/no-torch` (EC530)
- Add support for SonarQube up to 10.5

### 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
- Update Docker Compose configuration file to V2

### Deleted

Expand Down
1 change: 1 addition & 0 deletions eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Add `@ecocode` to the `plugins` section of your `.eslintrc`, followed by rules c
| [no-import-all-from-library](docs/rules/no-import-all-from-library.md) | Should not import all from library | ✅ |
| [no-multiple-access-dom-element](docs/rules/no-multiple-access-dom-element.md) | Disallow multiple access of same DOM element. | ✅ |
| [no-multiple-style-changes](docs/rules/no-multiple-style-changes.md) | Disallow multiple style changes at once. | ✅ |
| [no-torch](docs/rules/no-torch.md) | Should not programmatically enable torch mode | ✅ |
| [prefer-collections-with-pagination](docs/rules/prefer-collections-with-pagination.md) | Prefer API collections with pagination. | ✅ |
| [prefer-shorthand-css-notations](docs/rules/prefer-shorthand-css-notations.md) | Encourage usage of shorthand CSS notations | ✅ |
| [provide-print-css](docs/rules/provide-print-css.md) | Enforce providing a print stylesheet | ✅ |
Expand Down
24 changes: 24 additions & 0 deletions eslint-plugin/docs/rules/no-torch.md
utarwyn marked this conversation as resolved.
Show resolved Hide resolved
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
51 changes: 51 additions & 0 deletions eslint-plugin/lib/rules/no-torch.js
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",
});
}
},
};
},
};
56 changes: 56 additions & 0 deletions eslint-plugin/tests/lib/rules/no-torch.js
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],
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
NoImportAllFromLibrary.class,
NoMultipleAccessDomElement.class,
NoMultipleStyleChanges.class,
NoTorch.class,
PreferCollectionsWithPagination.class,
PreferShorthandCSSNotations.class,
ProvidePrintCSS.class
Expand Down
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";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"EC26",
"EC29",
"EC30",
"EC523"
"EC523",
"EC530"
]
}
Loading