From d635e0dd865956fd7b1eabdf40c4b879aed46154 Mon Sep 17 00:00:00 2001 From: Damien Date: Wed, 29 May 2024 17:49:03 +0200 Subject: [PATCH] add no torch rule --- eslint-plugin/README.md | 1 + eslint-plugin/docs/rules/no-torch.md | 24 ++++++++ eslint-plugin/lib/rules/no-torch.js | 51 +++++++++++++++++ eslint-plugin/tests/lib/rules/no-torch.js | 56 +++++++++++++++++++ .../java/io/ecocode/javascript/CheckList.java | 3 +- .../io/ecocode/javascript/checks/NoTorch.java | 37 ++++++++++++ 6 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 eslint-plugin/docs/rules/no-torch.md create mode 100644 eslint-plugin/lib/rules/no-torch.js create mode 100644 eslint-plugin/tests/lib/rules/no-torch.js create mode 100644 sonar-plugin/src/main/java/io/ecocode/javascript/checks/NoTorch.java diff --git a/eslint-plugin/README.md b/eslint-plugin/README.md index a5e19f7..2ceb150 100644 --- a/eslint-plugin/README.md +++ b/eslint-plugin/README.md @@ -76,6 +76,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 | ✅ | diff --git a/eslint-plugin/docs/rules/no-torch.md b/eslint-plugin/docs/rules/no-torch.md new file mode 100644 index 0000000..ca068a4 --- /dev/null +++ b/eslint-plugin/docs/rules/no-torch.md @@ -0,0 +1,24 @@ +# Should not programmatically enable torch mode (`@ecocode/no-torch`) + +⚠️ This rule _warns_ in the ✅ `recommended` config. + + +## 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 \ No newline at end of file diff --git a/eslint-plugin/lib/rules/no-torch.js b/eslint-plugin/lib/rules/no-torch.js new file mode 100644 index 0000000..27d31c5 --- /dev/null +++ b/eslint-plugin/lib/rules/no-torch.js @@ -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 . + */ + +"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", + }); + } + }, + }; + }, +}; diff --git a/eslint-plugin/tests/lib/rules/no-torch.js b/eslint-plugin/tests/lib/rules/no-torch.js new file mode 100644 index 0000000..0e8ddd7 --- /dev/null +++ b/eslint-plugin/tests/lib/rules/no-torch.js @@ -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 . + */ + +"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], + }, + ], +}); diff --git a/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java b/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java index 6b79f22..d6a9671 100644 --- a/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java +++ b/sonar-plugin/src/main/java/io/ecocode/javascript/CheckList.java @@ -43,7 +43,8 @@ public static List> getAllChecks() { NoMultipleStyleChanges.class, PreferCollectionsWithPagination.class, PreferShorthandCSSNotations.class, - ProvidePrintCSS.class + ProvidePrintCSS.class, + NoTorch.class ); } diff --git a/sonar-plugin/src/main/java/io/ecocode/javascript/checks/NoTorch.java b/sonar-plugin/src/main/java/io/ecocode/javascript/checks/NoTorch.java new file mode 100644 index 0000000..580686a --- /dev/null +++ b/sonar-plugin/src/main/java/io/ecocode/javascript/checks/NoTorch.java @@ -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 . + */ +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"; + } + +}