@kununu/eslint-config 5.2.3-beta-2 → 5.2.3-beta-3
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/index.js +1 -77
- package/package.json +1 -1
- package/rules/prefer-early-return.js +71 -0
package/index.js
CHANGED
|
@@ -1,78 +1,3 @@
|
|
|
1
|
-
// Define the rule separately
|
|
2
|
-
const preferEarlyReturnRule = {
|
|
3
|
-
meta: {
|
|
4
|
-
type: 'suggestion',
|
|
5
|
-
docs: {
|
|
6
|
-
description: 'Prefer early returns over logical expressions in return statements',
|
|
7
|
-
category: 'Best Practices',
|
|
8
|
-
recommended: true,
|
|
9
|
-
},
|
|
10
|
-
fixable: 'code',
|
|
11
|
-
},
|
|
12
|
-
create(context) {
|
|
13
|
-
return {
|
|
14
|
-
ReturnStatement(node) {
|
|
15
|
-
const argument = node.argument?.type === 'ParenthesizedExpression'
|
|
16
|
-
? node.argument.expression
|
|
17
|
-
: node.argument;
|
|
18
|
-
|
|
19
|
-
// Check if it's a sequence expression (comma-separated expressions)
|
|
20
|
-
if (argument?.type === 'SequenceExpression') {
|
|
21
|
-
const expressions = argument.expressions;
|
|
22
|
-
|
|
23
|
-
// Check if we have logical expressions with && in the sequence
|
|
24
|
-
const logicalExpressions = expressions.filter(expr =>
|
|
25
|
-
expr.type === 'LogicalExpression' && expr.operator === '&&'
|
|
26
|
-
);
|
|
27
|
-
|
|
28
|
-
if (logicalExpressions.length > 0) {
|
|
29
|
-
context.report({
|
|
30
|
-
node,
|
|
31
|
-
message: 'Prefer early returns over comma-separated logical expressions',
|
|
32
|
-
fix(fixer) {
|
|
33
|
-
const sourceCode = context.getSourceCode();
|
|
34
|
-
|
|
35
|
-
// Create a replacement string for the entire return
|
|
36
|
-
let replacement = '';
|
|
37
|
-
|
|
38
|
-
expressions.forEach((expr, index) => {
|
|
39
|
-
const isLast = index === expressions.length - 1;
|
|
40
|
-
|
|
41
|
-
if (expr.type === 'LogicalExpression' && expr.operator === '&&') {
|
|
42
|
-
const condition = sourceCode.getText(expr.left);
|
|
43
|
-
const value = sourceCode.getText(expr.right);
|
|
44
|
-
|
|
45
|
-
if (isLast && expressions.length > 1) {
|
|
46
|
-
// Last expression gets a simple return
|
|
47
|
-
replacement += `return ${value};`;
|
|
48
|
-
} else {
|
|
49
|
-
// Others get a conditional return
|
|
50
|
-
replacement += `if (${condition}) return ${value};\n`;
|
|
51
|
-
}
|
|
52
|
-
} else if (isLast) {
|
|
53
|
-
// Last non-logical expression gets a simple return
|
|
54
|
-
replacement += `return ${sourceCode.getText(expr)};`;
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
return fixer.replaceText(node, replacement);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
// Create and register our custom plugin inline
|
|
69
|
-
const customPlugin = {
|
|
70
|
-
rules: {
|
|
71
|
-
'prefer-early-return': preferEarlyReturnRule
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
require('eslint/lib/cli-engine/config/plugins').define('custom', customPlugin);
|
|
75
|
-
|
|
76
1
|
// we need to have the baseRules to have the same on the typescript override
|
|
77
2
|
// because it has extends the eslint does not take into consideration the ones from the base
|
|
78
3
|
const baseRules = {
|
|
@@ -279,7 +204,7 @@ const baseRules = {
|
|
|
279
204
|
// https://react.dev/blog/2024/04/25/react-19-upgrade-guide#removed-proptypes-and-defaultprops
|
|
280
205
|
'react/require-default-props': 0,
|
|
281
206
|
|
|
282
|
-
|
|
207
|
+
"prefer-early-return": require("./rules/prefer-early-return"),
|
|
283
208
|
};
|
|
284
209
|
|
|
285
210
|
module.exports = {
|
|
@@ -299,7 +224,6 @@ module.exports = {
|
|
|
299
224
|
'@babel',
|
|
300
225
|
'lodash',
|
|
301
226
|
'sort-destructure-keys',
|
|
302
|
-
'custom'
|
|
303
227
|
],
|
|
304
228
|
|
|
305
229
|
env: {
|
package/package.json
CHANGED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
meta: {
|
|
5
|
+
type: "suggestion",
|
|
6
|
+
docs: {
|
|
7
|
+
description: "Prefer early returns over logical expressions in return statements",
|
|
8
|
+
category: "Best Practices",
|
|
9
|
+
recommended: true,
|
|
10
|
+
},
|
|
11
|
+
fixable: "code",
|
|
12
|
+
},
|
|
13
|
+
create: (context) => {
|
|
14
|
+
return {
|
|
15
|
+
ReturnStatement: (node) => {
|
|
16
|
+
const argument =
|
|
17
|
+
node.argument?.type === "ParenthesizedExpression"
|
|
18
|
+
? node.argument.expression
|
|
19
|
+
: node.argument;
|
|
20
|
+
|
|
21
|
+
if (argument?.type === "SequenceExpression") {
|
|
22
|
+
const expressions = argument.expressions;
|
|
23
|
+
|
|
24
|
+
const logicalExpressions = expressions.filter(
|
|
25
|
+
(expr) =>
|
|
26
|
+
expr.type === "LogicalExpression" &&
|
|
27
|
+
expr.operator === "&&"
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
if (logicalExpressions.length > 0) {
|
|
31
|
+
context.report({
|
|
32
|
+
node,
|
|
33
|
+
message:
|
|
34
|
+
"Prefer early returns over comma-separated logical expressions",
|
|
35
|
+
fix: (fixer) => {
|
|
36
|
+
const sourceCode = context.getSourceCode();
|
|
37
|
+
let replacement = "";
|
|
38
|
+
|
|
39
|
+
expressions.forEach((expr, index) => {
|
|
40
|
+
const isLast = index === expressions.length - 1;
|
|
41
|
+
|
|
42
|
+
if (
|
|
43
|
+
expr.type === "LogicalExpression" &&
|
|
44
|
+
expr.operator === "&&"
|
|
45
|
+
) {
|
|
46
|
+
const condition = sourceCode.getText(
|
|
47
|
+
expr.left
|
|
48
|
+
);
|
|
49
|
+
const value = sourceCode.getText(expr.right);
|
|
50
|
+
|
|
51
|
+
if (isLast && expressions.length > 1) {
|
|
52
|
+
replacement += `return ${value};`;
|
|
53
|
+
} else {
|
|
54
|
+
replacement += `if (${condition}) return ${value};\n`;
|
|
55
|
+
}
|
|
56
|
+
} else if (isLast) {
|
|
57
|
+
replacement += `return ${sourceCode.getText(
|
|
58
|
+
expr
|
|
59
|
+
)};`;
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return fixer.replaceText(node, replacement);
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
},
|
|
71
|
+
};
|