@dineroregnskab/eslint-plugin-custom-rules 1.0.3 → 1.0.5

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dineroregnskab/eslint-plugin-custom-rules",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "ESLint plugin with custom rules for Dineroregnskab",
5
5
  "main": "eslint-plugin-custom-rules.js",
6
6
  "scripts": {
@@ -10,7 +10,7 @@
10
10
  "author": "",
11
11
  "license": "ISC",
12
12
  "devDependencies": {
13
- "eslint": "^8.53.0"
13
+ "eslint": "^8.54.0"
14
14
  },
15
15
  "peerDependencies": {
16
16
  "eslint": ">=8.0.0"
@@ -1,35 +1,46 @@
1
1
  module.exports = {
2
2
  meta: {
3
- type: "problem",
3
+ type: 'problem',
4
4
  docs: {
5
- description: "Enforce that a ngrx reducer always returns a value, so the store state remains intact.",
5
+ description:
6
+ 'Enforce that a ngrx reducer always returns a value, so the store state remains intact.',
6
7
  },
7
- fixable: "code",
8
- schema: []
8
+ fixable: 'code',
9
+ schema: [],
9
10
  },
10
11
 
11
- create(context) {
12
+ create: function (context) {
12
13
  return {
13
- FunctionDeclaration(node) {
14
- if (node.id.name.endsWith('Reducer')) {
15
- let hasReturnStatement = false;
14
+ 'CallExpression[callee.name="createReducer"]': function (node) {
15
+ node.arguments.forEach(function (arg) {
16
+ if (
17
+ arg.type === 'CallExpression' &&
18
+ arg.callee.name === 'on'
19
+ ) {
20
+ arg.arguments.slice(1).forEach(function (handler) {
21
+ const isFunction =
22
+ handler.type === 'FunctionExpression' ||
23
+ handler.type === 'ArrowFunctionExpression';
24
+ const isBlockStatement =
25
+ handler.body.type === 'BlockStatement';
26
+ const hasNoReturn =
27
+ isBlockStatement &&
28
+ !handler.body.body.some(
29
+ (statement) =>
30
+ statement.type === 'ReturnStatement',
31
+ );
16
32
 
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.',
33
+ if (isFunction && isBlockStatement && hasNoReturn) {
34
+ context.report({
35
+ node: handler,
36
+ message:
37
+ 'Reducer function must return a value to avoid undefined state.',
38
+ });
39
+ }
27
40
  });
28
41
  }
29
- }
42
+ });
30
43
  },
31
- };
32
- }
44
+ };
45
+ },
33
46
  };
34
-
35
- // tilføj fixer?
@@ -1,42 +0,0 @@
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!');