Skip to content

Commit

Permalink
add no torch rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirgon committed May 30, 2024
1 parent b80b84e commit d635e0d
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 1 deletion.
1 change: 1 addition & 0 deletions eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
24 changes: 24 additions & 0 deletions eslint-plugin/docs/rules/no-torch.md
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 @@ -43,7 +43,8 @@ public static List<Class<? extends JavaScriptCheck>> getAllChecks() {
NoMultipleStyleChanges.class,
PreferCollectionsWithPagination.class,
PreferShorthandCSSNotations.class,
ProvidePrintCSS.class
ProvidePrintCSS.class,
NoTorch.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";
}

}

0 comments on commit d635e0d

Please sign in to comment.