@dineroregnskab/eslint-plugin-custom-rules 1.0.3 → 1.0.4
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
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export default {
|
|
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: function(context) {
|
|
12
|
+
return {
|
|
13
|
+
'CallExpression[callee.name="createReducer"]': function(node) {
|
|
14
|
+
node.arguments.forEach(function(arg) {
|
|
15
|
+
if (arg.type === 'CallExpression' && arg.callee.name === 'on') {
|
|
16
|
+
arg.arguments.slice(1).forEach(function(handler) {
|
|
17
|
+
const isFunction = handler.type === 'FunctionExpression' || handler.type === 'ArrowFunctionExpression';
|
|
18
|
+
const isBlockStatement = handler.body.type === 'BlockStatement';
|
|
19
|
+
const hasNoReturn = isBlockStatement && !handler.body.body.some(statement => statement.type === 'ReturnStatement');
|
|
20
|
+
|
|
21
|
+
if (isFunction && isBlockStatement && hasNoReturn) {
|
|
22
|
+
context.report({
|
|
23
|
+
node: handler,
|
|
24
|
+
message: 'Reducer function must return a value to avoid undefined state.'
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// tilføj fixer?
|
|
36
|
+
// tilføj lint til filerne her
|
|
37
|
+
// convert til ts?
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
// readme med guide til tilføjelse af regel (+ version patch & publish, adding the rule to html/ts, nameing, adding to projects (main app/admin))
|
|
@@ -1,35 +0,0 @@
|
|
|
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
|
-
};
|
|
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!');
|