@kununu/eslint-config 5.2.3-beta-1 → 5.2.3-beta-2

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.
Files changed (2) hide show
  1. package/index.js +75 -67
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,5 +1,77 @@
1
- // Register our custom plugin
2
- require('eslint/lib/cli-engine/config/plugins').define('custom', require('./eslint-plugin-custom'));
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);
3
75
 
4
76
  // we need to have the baseRules to have the same on the typescript override
5
77
  // because it has extends the eslint does not take into consideration the ones from the base
@@ -207,71 +279,7 @@ const baseRules = {
207
279
  // https://react.dev/blog/2024/04/25/react-19-upgrade-guide#removed-proptypes-and-defaultprops
208
280
  'react/require-default-props': 0,
209
281
 
210
- 'custom/prefer-early-return': {
211
- meta: {
212
- type: 'suggestion',
213
- docs: {
214
- description: 'Prefer early returns over logical expressions in return statements',
215
- category: 'Best Practices',
216
- recommended: true,
217
- },
218
- fixable: 'code',
219
- },
220
- create(context) {
221
- return {
222
- ReturnStatement(node) {
223
- const argument = node.argument?.type === 'ParenthesizedExpression'
224
- ? node.argument.expression
225
- : node.argument;
226
-
227
- // Check if it's a sequence expression (comma-separated expressions)
228
- if (argument?.type === 'SequenceExpression') {
229
- const expressions = argument.expressions;
230
-
231
- // Check if we have logical expressions with && in the sequence
232
- const logicalExpressions = expressions.filter(expr =>
233
- expr.type === 'LogicalExpression' && expr.operator === '&&'
234
- );
235
-
236
- if (logicalExpressions.length > 0) {
237
- context.report({
238
- node,
239
- message: 'Prefer early returns over comma-separated logical expressions',
240
- fix(fixer) {
241
- const sourceCode = context.getSourceCode();
242
-
243
- // Create a replacement string for the entire return
244
- let replacement = '';
245
-
246
- expressions.forEach((expr, index) => {
247
- const isLast = index === expressions.length - 1;
248
-
249
- if (expr.type === 'LogicalExpression' && expr.operator === '&&') {
250
- const condition = sourceCode.getText(expr.left);
251
- const value = sourceCode.getText(expr.right);
252
-
253
- if (isLast && expressions.length > 1) {
254
- // Last expression gets a simple return
255
- replacement += `return ${value};`;
256
- } else {
257
- // Others get a conditional return
258
- replacement += `if (${condition}) return ${value};\n`;
259
- }
260
- } else if (isLast) {
261
- // Last non-logical expression gets a simple return
262
- replacement += `return ${sourceCode.getText(expr)};`;
263
- }
264
- });
265
-
266
- return fixer.replaceText(node, replacement);
267
- }
268
- });
269
- }
270
- }
271
- }
272
- };
273
- }
274
- },
282
+ 'custom/prefer-early-return': preferEarlyReturnRule,
275
283
  };
276
284
 
277
285
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kununu/eslint-config",
3
- "version": "5.2.3-beta-1",
3
+ "version": "5.2.3-beta-2",
4
4
  "description": "kununu's ESLint config",
5
5
  "main": "index.js",
6
6
  "repository": "kununu/eslint-config",