@kununu/eslint-config 5.2.3-beta-1 → 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 CHANGED
@@ -1,6 +1,3 @@
1
- // Register our custom plugin
2
- require('eslint/lib/cli-engine/config/plugins').define('custom', require('./eslint-plugin-custom'));
3
-
4
1
  // we need to have the baseRules to have the same on the typescript override
5
2
  // because it has extends the eslint does not take into consideration the ones from the base
6
3
  const baseRules = {
@@ -207,71 +204,7 @@ const baseRules = {
207
204
  // https://react.dev/blog/2024/04/25/react-19-upgrade-guide#removed-proptypes-and-defaultprops
208
205
  'react/require-default-props': 0,
209
206
 
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
- },
207
+ "prefer-early-return": require("./rules/prefer-early-return"),
275
208
  };
276
209
 
277
210
  module.exports = {
@@ -291,7 +224,6 @@ module.exports = {
291
224
  '@babel',
292
225
  'lodash',
293
226
  'sort-destructure-keys',
294
- 'custom'
295
227
  ],
296
228
 
297
229
  env: {
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-3",
4
4
  "description": "kununu's ESLint config",
5
5
  "main": "index.js",
6
6
  "repository": "kununu/eslint-config",
@@ -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
+ };