@kununu/eslint-config 5.2.3-beta-3 → 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,3 +1,23 @@
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
+ }
18
+ }
19
+ }
20
+
1
21
  // we need to have the baseRules to have the same on the typescript override
2
22
  // because it has extends the eslint does not take into consideration the ones from the base
3
23
  const baseRules = {
@@ -204,7 +224,7 @@ const baseRules = {
204
224
  // https://react.dev/blog/2024/04/25/react-19-upgrade-guide#removed-proptypes-and-defaultprops
205
225
  'react/require-default-props': 0,
206
226
 
207
- "prefer-early-return": require("./rules/prefer-early-return"),
227
+ "kununu-custom/prefer-early-return": 'error',
208
228
  };
209
229
 
210
230
  module.exports = {
@@ -224,6 +244,7 @@ module.exports = {
224
244
  '@babel',
225
245
  'lodash',
226
246
  'sort-destructure-keys',
247
+ 'kununu-custom'
227
248
  ],
228
249
 
229
250
  env: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kununu/eslint-config",
3
- "version": "5.2.3-beta-3",
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",
@@ -1,71 +0,0 @@
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
- };