@kununu/eslint-config 5.2.3-beta-2 → 5.2.3-beta-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.
@@ -0,0 +1,38 @@
1
+ module.exports = {
2
+ meta: {
3
+ type: 'suggestion',
4
+ docs: {
5
+ description: 'Prefer early returns over logical expressions in return statements',
6
+ category: 'Best Practices',
7
+ recommended: true,
8
+ },
9
+ fixable: 'code',
10
+ },
11
+ create(context) {
12
+ return {
13
+ ReturnStatement(node) {
14
+ const argument = node.argument?.type === 'ParenthesizedExpression'
15
+ ? node.argument.expression
16
+ : node.argument;
17
+
18
+ // Only handle && operators
19
+ if (argument?.type === 'LogicalExpression' && argument.operator === '&&') {
20
+ context.report({
21
+ node,
22
+ message: 'Prefer early returns over logical expressions in return statements',
23
+ fix(fixer) {
24
+ const sourceCode = context.getSourceCode();
25
+ const condition = sourceCode.getText(argument.left);
26
+ const returnValue = sourceCode.getText(argument.right);
27
+
28
+ return fixer.replaceText(
29
+ node,
30
+ `if (${condition}) return ${returnValue};`
31
+ );
32
+ }
33
+ });
34
+ }
35
+ }
36
+ };
37
+ }
38
+ };
package/index.js CHANGED
@@ -1,77 +1,22 @@
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
1
+ const customPlugin = require('./customRules');
2
+
3
+ // Register the custom plugin
4
+ try {
5
+ // This is the path in ESLint v8+
6
+ require('eslint/use-at-your-own-risk').builtinPlugins.define('kununu-custom', customPlugin);
7
+ } catch (e) {
8
+ try {
9
+ // This is the path in ESLint v7
10
+ require('eslint/lib/cli-engine/plugins').define('kununu-custom', customPlugin);
11
+ } catch (e) {
12
+ // Fallback for ESLint v6
13
+ try {
14
+ require('eslint/lib/util/plugins').define('kununu-custom', customPlugin);
15
+ } catch (e) {
16
+ console.warn('Failed to register custom ESLint plugin:', e.message);
17
+ }
72
18
  }
73
- };
74
- require('eslint/lib/cli-engine/config/plugins').define('custom', customPlugin);
19
+ }
75
20
 
76
21
  // we need to have the baseRules to have the same on the typescript override
77
22
  // because it has extends the eslint does not take into consideration the ones from the base
@@ -279,7 +224,7 @@ const baseRules = {
279
224
  // https://react.dev/blog/2024/04/25/react-19-upgrade-guide#removed-proptypes-and-defaultprops
280
225
  'react/require-default-props': 0,
281
226
 
282
- 'custom/prefer-early-return': preferEarlyReturnRule,
227
+ "kununu-custom/prefer-early-return": 'error',
283
228
  };
284
229
 
285
230
  module.exports = {
@@ -299,7 +244,7 @@ module.exports = {
299
244
  '@babel',
300
245
  'lodash',
301
246
  'sort-destructure-keys',
302
- 'custom'
247
+ 'kununu-custom'
303
248
  ],
304
249
 
305
250
  env: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kununu/eslint-config",
3
- "version": "5.2.3-beta-2",
3
+ "version": "5.2.3-beta-4",
4
4
  "description": "kununu's ESLint config",
5
5
  "main": "index.js",
6
6
  "repository": "kununu/eslint-config",