@kununu/eslint-config 5.2.1 → 5.2.2-beta-1

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 +45 -0
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -203,6 +203,51 @@ const baseRules = {
203
203
 
204
204
  // https://react.dev/blog/2024/04/25/react-19-upgrade-guide#removed-proptypes-and-defaultprops
205
205
  'react/require-default-props': 0,
206
+ // Enforce early return over nested return blocks
207
+ 'prefer-early-return': {
208
+ meta: {
209
+ type: 'suggestion',
210
+ docs: {
211
+ description: 'Prefer early returns over nested return blocks',
212
+ category: 'Best Practices',
213
+ recommended: true,
214
+ },
215
+ fixable: 'code',
216
+ },
217
+ create(context) {
218
+ return {
219
+ ReturnStatement(node) {
220
+ if (node.argument &&
221
+ node.argument.type === 'ObjectExpression' &&
222
+ node.argument.properties.some(p => p.type === 'LogicalExpression' || p.value?.type === 'LogicalExpression')) {
223
+
224
+ context.report({
225
+ node,
226
+ message: 'Prefer early returns over nested return blocks',
227
+ fix(fixer) {
228
+ const sourceCode = context.getSourceCode();
229
+ const properties = node.argument.properties;
230
+
231
+ // Convert logical expressions to if-returns
232
+ const fixes = properties.map(prop => {
233
+ if (prop.type === 'LogicalExpression' || prop.value?.type === 'LogicalExpression') {
234
+ const logicalExp = prop.type === 'LogicalExpression' ? prop : prop.value;
235
+ const condition = sourceCode.getText(logicalExp.left);
236
+ const returnValue = sourceCode.getText(logicalExp.right);
237
+ return `if (${condition}) return ${returnValue};`;
238
+ }
239
+ // If it's the last non-logical property, it becomes the default return
240
+ return `return ${sourceCode.getText(prop)};`;
241
+ });
242
+
243
+ return fixer.replaceText(node, fixes.join('\n'));
244
+ }
245
+ });
246
+ }
247
+ }
248
+ };
249
+ }
250
+ }
206
251
  };
207
252
 
208
253
  module.exports = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kununu/eslint-config",
3
- "version": "5.2.1",
3
+ "version": "5.2.2-beta-1",
4
4
  "description": "kununu's ESLint config",
5
5
  "main": "index.js",
6
6
  "repository": "kununu/eslint-config",