@dineroregnskab/eslint-plugin-custom-rules 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,4 @@
1
+ const returnReducerRule = require('./rules/reducers-should-always-return');
2
+
3
+ const customRulesplugin = { rules: { "reducers-should-always-return": returnReducerRule } };
4
+ module.exports = customRulesplugin;
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@dineroregnskab/eslint-plugin-custom-rules",
3
+ "version": "1.0.0",
4
+ "description": "ESLint plugin with custom rules for Dineroregnskab",
5
+ "main": "eslint-plugin-custom-rules.js",
6
+ "scripts": {
7
+ "test": "node rules/reducers-should-always-return.test.js"
8
+ },
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "ISC",
12
+ "devDependencies": {
13
+ "eslint": "^8.53.0"
14
+ },
15
+ "peerDependencies": {
16
+ "eslint": ">=8.0.0"
17
+ }
18
+ }
@@ -0,0 +1,33 @@
1
+ module.exports = {
2
+ meta: {
3
+ type: "problem",
4
+ docs: {
5
+ description: "Enforce that a ngrx reducer always returns a value, so the store state remains intact.",
6
+ },
7
+ fixable: "code",
8
+ schema: []
9
+ },
10
+
11
+ create(context) {
12
+ return {
13
+ FunctionDeclaration(node) {
14
+ if (node.id.name.endsWith('Reducer')) {
15
+ let hasReturnStatement = false;
16
+
17
+ node.body.body.forEach(statement => {
18
+ if (statement.type === 'ReturnStatement') {
19
+ hasReturnStatement = true;
20
+ }
21
+ });
22
+
23
+ if (!hasReturnStatement) {
24
+ context.report({
25
+ node,
26
+ message: 'Reducer function must return a value to avoid undefined state.',
27
+ });
28
+ }
29
+ }
30
+ },
31
+ };
32
+ }
33
+ };
@@ -0,0 +1,42 @@
1
+ const returnReducerRule = require('./reducers-should-always-return');
2
+ const { RuleTester } = require('eslint');
3
+
4
+ const ruleTester = new RuleTester({
5
+ parserOptions: { ecmaVersion: 2019 },
6
+ });
7
+
8
+ ruleTester.run('reducers-should-always-return', returnReducerRule, {
9
+ valid: [
10
+ // This reducer returns a value in all code paths
11
+ {
12
+ code:
13
+ `function myReducer(state, action) {
14
+ if (action.type === 'ACTION_TYPE') {
15
+ return { ...state, data: action.data };
16
+ }
17
+ return state;
18
+ }`,
19
+ },
20
+ ],
21
+
22
+ invalid: [
23
+ // This reducer doesn't return a value if action.type is not 'ACTION_TYPE'
24
+ {
25
+ code:
26
+ `function badReducer(state, action) {
27
+ if (action.type === 'ACTION_TYPE') {
28
+ return { ...state, data: action.data };
29
+ }
30
+ }`,
31
+ errors: [
32
+ {
33
+ message:
34
+ 'Reducer function must return a value to avoid undefined state.',
35
+ type: 'FunctionDeclaration',
36
+ },
37
+ ],
38
+ },
39
+ ],
40
+ });
41
+
42
+ console.log('All tests passed!');