@kununu/eslint-config 5.2.3-beta-5 → 5.2.3-beta-7

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,67 +1,3 @@
1
- const preferEarlyReturnRule = {
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
- };
39
-
40
- // Create the plugin with our rule
41
- const customPlugin = {
42
- rules: {
43
- 'prefer-early-return': preferEarlyReturnRule
44
- }
45
- };
46
-
47
- // Register the custom plugin
48
- try {
49
- // This is the path in ESLint v8+
50
- require('eslint/use-at-your-own-risk').builtinPlugins.define('kununu-custom', customPlugin);
51
- } catch (e) {
52
- try {
53
- // This is the path in ESLint v7
54
- require('eslint/lib/cli-engine/plugins').define('kununu-custom', customPlugin);
55
- } catch (e) {
56
- // Fallback for ESLint v6
57
- try {
58
- require('eslint/lib/util/plugins').define('kununu-custom', customPlugin);
59
- } catch (e) {
60
- console.warn('Failed to register custom ESLint plugin:', e.message);
61
- }
62
- }
63
- }
64
-
65
1
  // we need to have the baseRules to have the same on the typescript override
66
2
  // because it has extends the eslint does not take into consideration the ones from the base
67
3
  const baseRules = {
@@ -268,9 +204,13 @@ const baseRules = {
268
204
  // https://react.dev/blog/2024/04/25/react-19-upgrade-guide#removed-proptypes-and-defaultprops
269
205
  'react/require-default-props': 0,
270
206
 
271
- "kununu-custom/prefer-early-return": 'error',
272
207
  };
273
208
 
209
+ const kununuPlugin = require('./plugins/kununu');
210
+
211
+ // Register it using this prefix
212
+ const pluginPrefix = '@kununu/eslint-config/plugins/kununu';
213
+
274
214
  module.exports = {
275
215
  extends: [
276
216
  'airbnb', // Many strict rules for ECMAScript and React
@@ -288,7 +228,7 @@ module.exports = {
288
228
  '@babel',
289
229
  'lodash',
290
230
  'sort-destructure-keys',
291
- 'kununu-custom'
231
+ pluginPrefix
292
232
  ],
293
233
 
294
234
  env: {
@@ -298,7 +238,10 @@ module.exports = {
298
238
  es6: true,
299
239
  },
300
240
 
301
- rules: baseRules,
241
+ rules: {
242
+ ...baseRules,
243
+ [`${pluginPrefix}/prefer-early-return`]: 'error'
244
+ },
302
245
 
303
246
  overrides: [{
304
247
  files: [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kununu/eslint-config",
3
- "version": "5.2.3-beta-5",
3
+ "version": "5.2.3-beta-7",
4
4
  "description": "kununu's ESLint config",
5
5
  "main": "index.js",
6
6
  "repository": "kununu/eslint-config",
@@ -0,0 +1,51 @@
1
+ module.exports = {
2
+ rules: {
3
+ 'prefer-early-return': {
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
+ schema: [] // No options
13
+ },
14
+ create: function(context) {
15
+ return {
16
+ ReturnStatement: function(node) {
17
+ // Handle parenthesized expressions in a way compatible with ESLint 5+
18
+ let argument = node.argument;
19
+ if (argument && argument.type === 'ParenthesizedExpression') {
20
+ argument = argument.expression;
21
+ }
22
+
23
+ // Only handle && operators
24
+ if (argument &&
25
+ argument.type === 'LogicalExpression' &&
26
+ argument.operator === '&&') {
27
+ context.report({
28
+ node: node,
29
+ message: 'Prefer early returns over logical expressions in return statements. Use if (condition) return value; instead.',
30
+ fix: function(fixer) {
31
+ // Get source code in a way that works in ESLint 5+
32
+ const sourceCode = context.getSourceCode ?
33
+ context.getSourceCode() :
34
+ context.getSource;
35
+
36
+ const condition = sourceCode.getText(argument.left);
37
+ const returnValue = sourceCode.getText(argument.right);
38
+
39
+ return fixer.replaceText(
40
+ node,
41
+ "if (" + condition + ") return " + returnValue + ";"
42
+ );
43
+ }
44
+ });
45
+ }
46
+ }
47
+ };
48
+ }
49
+ }
50
+ }
51
+ };